From 5c73b73f7ca625c0d0e9720f0c981b6de7ea57bb Mon Sep 17 00:00:00 2001 From: Ian White Date: Wed, 25 Aug 2010 18:53:19 +0100 Subject: [PATCH] spec config labels and aliases --- lib/pickle/config.rb | 16 +++++++++++++-- spec/pickle/config_spec.rb | 40 +++++++++++++++++++++++++++++++++++--- 2 files changed, 51 insertions(+), 5 deletions(-) diff --git a/lib/pickle/config.rb b/lib/pickle/config.rb index 8eec7ac0..a0569690 100644 --- a/lib/pickle/config.rb +++ b/lib/pickle/config.rb @@ -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? @@ -87,7 +87,7 @@ 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 => ''" unless args.any? && options[:to].is_a?(String) + raise ArgumentError, "Usage: alias 'alias1' [, 'alias2', ...], :to => ''" unless args.any? && options[:to].is_a?(String) args.each {|aliaz| self.aliases[aliaz] = options[:to]} end @@ -95,6 +95,18 @@ 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 diff --git a/spec/pickle/config_spec.rb b/spec/pickle/config_spec.rb index d9e25f37..1b0a9ff5 100644 --- a/spec/pickle/config_spec.rb +++ b/spec/pickle/config_spec.rb @@ -2,7 +2,7 @@ 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] } @@ -10,14 +10,14 @@ 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, ]" do let(:adapter_class) { Class.new(Object) } before { subject.adapters = [:machinist, adapter_class] } - + it "should have :adapter_classes [Pickle::Adapter::Machinist, ]" do subject.adapter_classes.should == [Pickle::Adapter::Machinist, adapter_class] end @@ -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