Skip to content

Commit

Permalink
specs and implements parsing label in pickle ref
Browse files Browse the repository at this point in the history
  • Loading branch information
ianwhite committed Aug 19, 2010
1 parent be8a91a commit 3053c2f
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 16 deletions.
34 changes: 30 additions & 4 deletions lib/pickle/ref.rb
Expand Up @@ -8,12 +8,34 @@ def initialize(string)

protected
def parse_ref(string)
@index = parse_index(string)
@factory_name = (string =~ /^(?:#{match_prefix}|#{capture_index})?#{capture_factory_name}/ && $2)
@index = parse_index!(string)
@factory_name = parse_factory_name!(string)
@label = parse_label!(string)
end

def parse_index(string)
string =~ /^#{capture_index}#{capture_factory_name}/ && $1
# parse and remove the index from the given string
# @returns the index or nil
def parse_index!(string)
remove_from_and_return_1st_capture!(string, /^#{capture_index}/)
end

# 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)
remove_from_and_return_1st_capture!(string, /^#{match_prefix}?#{capture_factory_name}/)
end

# parse the label, removing it if found
# @returns the label or nil
def parse_label!(string)
remove_from_and_return_1st_capture!(string, /^#{capture_label}/)
end

def remove_from_and_return_1st_capture!(string, regexp)
if match_data = string.match(regexp)
string.sub!(match_data[0], '')
match_data[1]
end
end

def capture_factory_name
Expand All @@ -31,5 +53,9 @@ def match_ordinal
def capture_index
/(?:the )?(first|last|#{match_ordinal}) ?/
end

def capture_label
/(?:\: )?\"([\w ]+)\"/
end
end
end
54 changes: 42 additions & 12 deletions spec/pickle/ref_spec.rb
@@ -1,7 +1,7 @@
require 'spec_helper'

describe Pickle::Ref do
describe "(simple factory name) " do
describe "(factory name) " do
describe ".new 'colour'" do
subject { Pickle::Ref.new('colour') }

Expand All @@ -20,19 +20,49 @@
end
end

describe ".new('1st colour')" do
subject { Pickle::Ref.new('1st colour') }
describe "(index)" do
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(:attribute) { should be_nil }

['2nd', 'first', 'last', '3rd', '4th'].each do |index|
describe ".new('#{index} colour')" do
subject { Pickle::Ref.new("#{index} colour") }
its(:index) { should == '1st' }
its(:factory_name) { should == 'colour' }
its(:label) { should be_nil }
its(:attribute) { should be_nil }
['2nd', 'first', 'last', '3rd', '4th'].each do |index|
describe ".new('#{index} colour')" do
subject { Pickle::Ref.new("#{index} colour") }

its(:index) { should == index}
its(:index) { should == index}
end
end

describe "the 2nd colour" do
subject { Pickle::Ref.new('the 2nd colour') }

its(:index) { should == '2nd' }
its(:factory_name) { should == 'colour' }
end

describe "perverse use" do
it "'a 1st colour' is BAD"
end
end

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

its(:index) { should == nil }
its(:factory_name) { should == 'colour' }
its(:label) { should == 'red' }
its(:attribute) { should be_nil }

describe "perverse" do
it "'1st colour: \"red\"'"
end

describe "ok" do
it "'\"red\"'"
end
end
end
Expand Down

0 comments on commit 3053c2f

Please sign in to comment.