Skip to content

Commit

Permalink
spec config labels and aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
ianwhite committed Aug 25, 2010
1 parent a91fe74 commit 5c73b73
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 5 deletions.
16 changes: 14 additions & 2 deletions lib/pickle/config.rb
Expand Up @@ -2,7 +2,7 @@

module Pickle
class Config
attr_writer :adapters, :factories, :aliases, :labels, :mappings, :predicates
attr_writer :adapters, :factories, :mappings, :predicates

def initialize(&block)
configure(&block) if block_given?
Expand Down Expand Up @@ -87,14 +87,26 @@ def factories
# config.alias 'admin', 'admin user', :to => 'external_lib_admin_user' # where External::Lib::Admin::User is one of your models
def alias(*args)
options = args.extract_options!
raise ArgumentError, "Usage: alias 'alias1' [, 'alias2', ...] :to => '<factory_name>'" unless args.any? && options[:to].is_a?(String)
raise ArgumentError, "Usage: alias 'alias1' [, 'alias2', ...], :to => '<factory_name>'" unless args.any? && options[:to].is_a?(String)
args.each {|aliaz| self.aliases[aliaz] = options[:to]}
end

def aliases
@aliases ||= {}
end

# config.label_attribute 'user', :is => 'name'
# this uses the pickle ref label to set an attribute on create
def label_attribute_for(*args)
options = args.extract_options!
raise ArgumentError, "Usage: label_attribute_for 'factory1' [, 'factory2', ...], :with => 'attribute_name'" unless args.any? && options[:is].is_a?(String)
args.each {|factory| self.label_attributes[factory] = options[:is]}
end

def label_attributes
@labels ||= {}
end

class Mapping < Struct.new(:search, :replacement)
end

Expand Down
40 changes: 37 additions & 3 deletions spec/pickle/config_spec.rb
Expand Up @@ -2,22 +2,22 @@

describe Pickle::Config do
subject { Pickle::Config.new }

describe "new" do
its(:adapters) { should == [:machinist, :factory_girl, :orm] }
its(:adapter_classes) { should == [Pickle::Adapter::Machinist, Pickle::Adapter::FactoryGirl, Pickle::Adapter::Orm] }
its(:predicates) { should be_empty }
its(:mappings) { should be_empty }
its(:factories) { should be_empty }
its(:aliases) { should be_empty }
its(:labels) { should be_empty }
its(:label_attributes) { should be_empty }
end

describe "setting adapters to [:machinist, <adapter_class>]" do
let(:adapter_class) { Class.new(Object) }

before { subject.adapters = [:machinist, adapter_class] }

it "should have :adapter_classes [Pickle::Adapter::Machinist, <adapter_class>]" do
subject.adapter_classes.should == [Pickle::Adapter::Machinist, adapter_class]
end
Expand Down Expand Up @@ -50,4 +50,38 @@
c.foo :bar
end
end

describe "label" do
let(:config) {Pickle::Config.new}
subject { config }

describe "user, :is => 'name'" do
before { config.label_attribute_for('user', :is => 'name') }
its(:label_attributes) {should include('user' => 'name')}
end

describe "user, admin, supplier, :is => 'name'" do
before { config.label_attribute_for 'user','admin','supplier', :is => 'name' }
its(:label_attributes) {should include('user' => 'name')}
its(:label_attributes) {should include('admin' => 'name')}
its(:label_attributes) {should include('supplier' => 'name')}
end
end

describe "alias" do
let(:config) {Pickle::Config.new}
subject { config }

describe "user, :to => 'name'" do
before { config.alias('admin', :to => 'admin user') }
its(:aliases) {should include('admin' => 'admin user')}
end

describe "customer, supplier, reseller :to => 'trade user'" do
before { config.alias 'customer','supplier','reseller', :to => 'trade user' }
its(:aliases) {should include('customer' => 'trade user')}
its(:aliases) {should include('supplier' => 'trade user')}
its(:aliases) {should include('reseller' => 'trade user')}
end
end
end

0 comments on commit 5c73b73

Please sign in to comment.