Skip to content

Commit

Permalink
Remove match_label and capture_label, and allow for a pickle_ref to b…
Browse files Browse the repository at this point in the history
…e just a quoted string
  • Loading branch information
ianwhite committed Aug 20, 2010
1 parent 8b53760 commit df6871e
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 34 deletions.
12 changes: 2 additions & 10 deletions lib/pickle/parser/matchers.rb
Expand Up @@ -49,10 +49,6 @@ def match_index
/(?:(?:the )?#{match_index_word.source})/
end

def match_label
/(?:\:? ?#{match_quoted.source})/
end

def match_field
/(?:\w+\: [^,]+(?=$|,))/
end
Expand All @@ -66,21 +62,17 @@ def match_predicate
end

def match_model
/(?:(?:#{match_index.source} |#{match_prefix.source} )?#{match_factory.source}#{match_label.source}?)/
/(?:(?:#{match_index.source} |#{match_prefix.source} )?#{match_factory.source}(?:(?: |: )#{match_quoted.source})?)/
end

def match_pickle_ref
match_disjunction *(mapping_searches + [match_model.source])
/(?:#{mapping_searches ? match_disjunction(mapping_searches).source + '|' : ''}#{match_quoted.source}|#{match_model.source})/
end

def capture_index
/(?:(?:the )?(#{match_index_word.source}))/
end

def capture_label
/(?:\:? ?#{capture_quoted.source})/
end

def capture_factory
/(?:\b(\w\w+)\b)/
end
Expand Down
2 changes: 1 addition & 1 deletion lib/pickle/ref.rb
Expand Up @@ -40,7 +40,7 @@ def parse_factory!(string)
# parse the label, removing it if found
# @return the label or nil
def parse_label!(string)
remove_from_and_return_1st_capture!(string, /^#{capture_label}/)
remove_from_and_return_1st_capture!(string, /^(?: |: )?#{capture_quoted}/)
end

def remove_from_and_return_1st_capture!(string, regexp)
Expand Down
15 changes: 7 additions & 8 deletions spec/pickle/parser/matchers_spec.rb
Expand Up @@ -28,6 +28,10 @@ def capture(expected, options = {})
end
end

its(:match_quoted) { should match_all '""', '"gday mate"' }
its(:match_quoted) { should_not match_all '', "''", '"gday \" mate"' }
its(:capture_quoted) { should capture "gday mate", :from => '"gday mate"' }

its(:match_ordinal) { should match_all '1st', '2nd', '23rd', '104th' }
its(:match_ordinal) { should_not match_any '1', '2' }

Expand All @@ -38,14 +42,6 @@ def capture(expected, options = {})
its(:capture_index) { should capture "1st", :from => '1st' }
its(:capture_index) { should capture "first", :from => 'first' }

its(:match_label) { should match_all '"gday"', ' "gday"',': "gday"', ': "gday mate"' }
its(:match_label) { should_not match_any ': "gday""', ': gday' }

its(:capture_label) { should capture "fred", :from => '"fred"' }
its(:capture_label) { should capture "fred", :from => ' "fred"' }
its(:capture_label) { should capture "fred", :from => ': "fred"' }
its(:capture_label) { should capture "fred", :from => ': "fred"' }

its(:match_field) { should match_all 'foo: "this is the life"', 'bar_man: "and so is this"', 'boolean: false', 'boolean: true', 'numeric: 10', 'numeric: 12.5', 'numeric: +10', 'numeric: +12.5', 'numeric: -10', 'numeric: -12.5', 'nil_field: nil' }
its(:match_field) { should_not match_any 'foo bar: "this aint workin"', '"foo": "Bar"', ":foo => bar" }

Expand All @@ -66,6 +62,9 @@ def capture(expected, options = {})
its(:match_plural_factory) { should match_all 'users', 'fast_cars', 'cars' }
its(:match_plural_factory) { should_not match_any 'admin users', 'faster cars', 'events created' }

its(:match_pickle_ref) { should match_all 'a user', 'a user: "fred"', 'the 2nd user', 'the super_admin', '"fred"' }
its(:match_pickle_ref) { should_not match_any 'an admin user', 'a 2nd user', '' }

describe "with config.factories = ['admin user', 'funky thing']" do
before do
self.config = Pickle::Config.new {|c| c.factories = ["admin user", "funky thing"]}
Expand Down
30 changes: 15 additions & 15 deletions spec/pickle/ref_spec.rb
Expand Up @@ -50,23 +50,23 @@
its(:factory) { should == 'colour' }
end
end

describe "(label)" do
describe "'colour: \"red\"'" do
subject { Pickle::Ref.new('colour: "red"') }
end

describe "(label)" do
describe "'colour: \"red\"'" do
subject { Pickle::Ref.new('colour: "red"') }

its(:index) { should == nil }
its(:factory) { should == 'colour' }
its(:label) { should == 'red' }
end
describe "'\"red\"'" do
subject { Pickle::Ref.new('"red"') }
its(:index) { should == nil }
its(:factory) { should == 'colour' }
its(:label) { should == 'red' }
end

describe "'\"red\"'" do
subject { Pickle::Ref.new('"red"') }

its(:index) { should == nil }
its(:factory) { should == nil }
its(:label) { should == 'red' }
end
its(:index) { should == nil }
its(:factory) { should == nil }
its(:label) { should == 'red' }
end
end

Expand Down

0 comments on commit df6871e

Please sign in to comment.