Project

breadboard

0.01
No commit activity in last 3 years
No release in over 3 years
Breadboard allows you to define what services your ActiveResource derived classes connect to based on your rails environment.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 0.10.0
~> 2.4.0

Runtime

 Project Readme

#Introduction

Change your ActiveResource service providers based on your environment.

##Install

Breadboard is available as a gem:

# gem install breadboard

##Usage

If you're using this in a Rails app, you'll want to configure Breadboard in an initializer.

In the simplest scenario, all of your ActiveResource models connect to the same service provider in all environments. In this case, you really don't need this gem, but if you're determined to use it just because you know it's the coolest thing ever, then we've got you covered:

Breadboard.configure do
  default do
    all "http://my.universal.service.provider.com"
  end
end

This means: for all ActiveResource models, in all environments, connect them to http://my.universal.service.provider.com

However, it's more likely that you're using this gem because you are in a situation where you need to connect your models to different services based on a rails environment. This is also trivial with breadboard:

Breadboard.configure do
  default do
    all "http://my.universal.service.provider.com"
    production "http://my.production.service.provider.com"
  end
end

Now suppose you have an 'Author' model that needs to connect to a completely different service from everything else, in all environments:

Breadboard.configure do
  default do
    all "http://my.universal.service.provider.com"
    production "http://my.production.service.provider.com"
  end
  
  author do 
    all "http://my.author.service.provider"
  end
end

You can use this alternate syntax, if you prefer:

Breadboard.configure do
  default do
    all "http://my.universal.service.provider.com"
    production "http://my.production.service.provider.com"
  end
  
  model Author do 
    all "http://my.author.service.provider"
  end
end

Now suppose you have a 'Book' model that needs to connect to a different service provider than the default when in 'production' mode:

Breadboard.configure do
  default do
    all "http://my.universal.service.provider.com"
    production "http://my.production.service.provider.com"
  end
  
  model Author do 
    all "http://my.author.service.provider"
  end

  model Book do
    production "http://my.book.production.service.provider"
  end
end

If you need to set user and password for a restful service, you can do so with a block (as of version 1.1.0.rc1)

Breadboard.configure do
  model Book do
    production do
      site     "http://my.book.production.service.provider"
      user     "user1"
      password "password"
    end
  end
end

Configuring the environment

By default, Breadboard will check Rails.env to see what environment your app is in.

If your using Breadboard in something other than a rails app, you can configure how Breadboard determines your app's environment:

Breadboard.configure do
  env do
    # your app environment lookup logic here
  end
end

Mutiple models with the same configuration

Imagine you have several models that all share the same app configuration. There's two ways to about this configuration:

  • Let all of your models inherit from the same parent class and configure the parent class in breadboard
  • use the models method

Inheritance

Let's imagine you have three models that should all share the same configuration: Article, Author, Comment. If they all inherited from the same parent, Publishing, then you could simply configure the parent class in breadboard:

Breadboard.configure do
  model Publishing do 
    all "http://my.publishing.service.provider"
  end
end

If any of the children needed some configuration override, that will take precedence

Breadboard.configure do
  model Publishing do 
    all "http://my.publishing.service.provider"
  end

  model Article do
    production "http://my.article.production.service.provider"
  end
end

models Method

If your Article, Author, and Comment models couldn't all inherit from the same parent, you can still easily provide the same configuration for them via the models method:

Breadboard.configure do
  models Article, Author, Comment do 
    all "http://my.publishing.service.provider"
    production "http://my.production.publishing.service.provider"
  end
end

block values

If you want a lazily evaluated site attribute (for example, if you need to add parameters into your site url at runtime), then simply set the site as a block:

Breadboard.configure do
  some_model do
    all -> { "http://somewhere/parents/#{Parent.instance.id}" }
  end
end