From 5a89b4614e565498e00b071c596cc02de12af141 Mon Sep 17 00:00:00 2001 From: Ian White Date: Thu, 19 Aug 2010 15:45:19 +0100 Subject: [PATCH] Pickle::Ref#factory_name => Pickle::Ref#factory --- lib/pickle/ref.rb | 13 ++++++----- spec/pickle/ref_spec.rb | 48 +++++++++++++++++++++++------------------ 2 files changed, 35 insertions(+), 26 deletions(-) diff --git a/lib/pickle/ref.rb b/lib/pickle/ref.rb index 8d39e769..c7baf8cb 100644 --- a/lib/pickle/ref.rb +++ b/lib/pickle/ref.rb @@ -1,6 +1,9 @@ module Pickle + # parses a pickle ref string into its component parts: factory, index, and label + # + # raises an error if the pickle_ref is invalid class Ref - attr_reader :factory_name, :index, :label + attr_reader :factory, :index, :label def initialize(string) parse_ref(string) @@ -8,9 +11,9 @@ def initialize(string) protected def parse_ref(string) - @index = parse_index!(string) - @factory_name = parse_factory_name!(string) - @label = parse_label!(string) + @index = parse_index!(string) + @factory = parse_factory!(string) + @label = parse_label!(string) end # parse and remove the index from the given string @@ -21,7 +24,7 @@ def parse_index!(string) # parse the factory name from the given string, remove the factory name and optional prefix # @returns factory_name or nil - def parse_factory_name!(string) + def parse_factory!(string) remove_from_and_return_1st_capture!(string, /^#{match_prefix}?#{capture_factory_name}/) end diff --git a/spec/pickle/ref_spec.rb b/spec/pickle/ref_spec.rb index a879d5e7..f219bf55 100644 --- a/spec/pickle/ref_spec.rb +++ b/spec/pickle/ref_spec.rb @@ -5,16 +5,16 @@ describe ".new 'colour'" do subject { Pickle::Ref.new('colour') } - its(:index) { should be_nil } - its(:factory_name) { should == 'colour' } - its(:label) { should be_nil } + its(:index) { should be_nil } + its(:factory) { should == 'colour' } + its(:label) { should be_nil } end ['a', 'an', 'the', 'that', 'another'].each do |prefix| describe ".new '#{prefix} colour'" do subject { Pickle::Ref.new("#{prefix} colour") } - its(:factory_name) { should == 'colour' } + its(:factory) { should == 'colour' } end end end @@ -23,9 +23,9 @@ describe ".new('1st colour')" do subject { Pickle::Ref.new('1st colour') } - its(:index) { should == '1st' } - its(:factory_name) { should == 'colour' } - its(:label) { should be_nil } + its(:index) { should == '1st' } + its(:factory) { should == 'colour' } + its(:label) { should be_nil } ['2nd', 'first', 'last', '3rd', '4th'].each do |index| describe ".new('#{index} colour')" do @@ -38,8 +38,8 @@ describe "the 2nd colour" do subject { Pickle::Ref.new('the 2nd colour') } - its(:index) { should == '2nd' } - its(:factory_name) { should == 'colour' } + its(:index) { should == '2nd' } + its(:factory) { should == 'colour' } end describe "perverse use" do @@ -48,18 +48,24 @@ end describe "(label)" do - subject { Pickle::Ref.new('colour: "red"') } + describe "'colour: \"red\"'" do + subject { Pickle::Ref.new('colour: "red"') } - its(:index) { should == nil } - its(:factory_name) { should == 'colour' } - its(:label) { should == 'red' } + its(:index) { should == nil } + its(:factory) { should == 'colour' } + its(:label) { should == 'red' } + end - describe "perverse" do - it "'1st colour: \"red\"'" + describe "'\"red\"'" do + subject { Pickle::Ref.new('"red"') } + + its(:index) { should == nil } + its(:factory) { should == nil } + its(:label) { should == 'red' } end - describe "ok" do - it "'\"red\"'" + describe "perverse" do + it "'1st colour: \"red\"'" end end end @@ -94,7 +100,7 @@ # # Given a colour exists with hue: "blue" # create_model_in_scenario('color', 'hue: "blue"') - # factory_name = parse_factory_name_from_pickle_ref('color') #=> 'colour' + # factory = parse_factory_from_pickle_ref('color') #=> 'colour' # attrs = parse_attributes_from_fields('hue: "blue"') # => hash # # if label in pickle_ref use that @@ -110,7 +116,7 @@ # # Given a color exists with hue: "red" # create_model_in_scenario('color "red"') - # factory_name = ... # 'colour' + # factory = ... # 'colour' # obj = Colour.make(:hue => "red") # store_model(obj, :label => 'red') # @@ -129,9 +135,9 @@ #create_model_in_scenario 'color', 'hue: "blue"' #def create_model_in_scenario(pickle_ref, fields = nil) - # factory_name, label = *parse_pickle_ref(pickle_ref) #=> 'color', nil + # factory, label = *parse_pickle_ref(pickle_ref) #=> 'color', nil # - # factory = get_the_factory_using_possibly_aliased_factory_name(factory_name) + # factory = get_the_factory_using_possibly_aliased_factory(factory) # # attrs = parse_the_fields_converting_pickle_refs_to_models_and_also_applying_transforms(fields) #