Skip to content

Commit

Permalink
Pickle::Ref#factory_name => Pickle::Ref#factory
Browse files Browse the repository at this point in the history
  • Loading branch information
ianwhite committed Aug 19, 2010
1 parent ec98899 commit 5a89b46
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 26 deletions.
13 changes: 8 additions & 5 deletions lib/pickle/ref.rb
@@ -1,16 +1,19 @@
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)
end

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
Expand All @@ -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

Expand Down
48 changes: 27 additions & 21 deletions spec/pickle/ref_spec.rb
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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')
#
Expand All @@ -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)
#
Expand Down

0 comments on commit 5a89b46

Please sign in to comment.