class Pickle::Parser

Attributes

config[R]

Public Class Methods

new(options = {}) click to toggle source
# File lib/pickle/parser.rb, line 9
def initialize(options = {})
  @config = options[:config] || raise(ArgumentError, "Parser.new requires a :config")
end

Public Instance Methods

canonical(str) click to toggle source

returns really underscored name

# File lib/pickle/parser.rb, line 36
def canonical(str)
  str.to_s.underscore.gsub(' ','_').gsub('/','_')
end
parse_field(field) click to toggle source

given a string like 'foo: expr' returns {key => value}

# File lib/pickle/parser.rb, line 27
def parse_field(field)
  if field =~ /^#{capture_key_and_value_in_field}$/
    { $1 => eval($2) }
  else
    raise ArgumentError, "The field argument is not in the correct format.\n\n'#{field}' did not match: #{match_field}"
  end
end
parse_fields(fields) click to toggle source

given a string like 'foo: “bar”, bar: “baz”' returns {“foo” => “bar”, “bar” => “baz”}

# File lib/pickle/parser.rb, line 14
def parse_fields(fields)
  if fields.blank?
    {}
  elsif fields =~ /^#{match_fields}$/
    fields.scan(/(#{match_field})(?:,|$)/).inject({}) do |m, match|
      m.merge(parse_field(match[0]))
    end
  else
    raise ArgumentError, "The fields string is not in the correct format.\n\n'#{fields}' did not match: #{match_fields}"
  end
end
parse_index(index) click to toggle source
# File lib/pickle/parser.rb, line 50
def parse_index(index)
  case index
  when nil, '', 'last' then -1
  when /#{capture_number_in_ordinal}/ then $1.to_i - 1
  when 'first' then 0
  end
end
parse_model(model_name) click to toggle source

return [factory_name, name or integer index]

# File lib/pickle/parser.rb, line 41
def parse_model(model_name)
  apply_mappings!(model_name)
  if /#{capture_index} #{capture_factory}$/ =~ model_name
    [canonical($2), parse_index($1)]
  elsif /#{capture_factory}#{capture_name_in_label}?$/ =~ model_name
    [canonical($1), canonical($2)]
  end
end

Private Instance Methods

apply_mappings!(string) click to toggle source
# File lib/pickle/parser.rb, line 59
def apply_mappings!(string)
  config.mappings.each do |mapping|
    string.sub! /^#{mapping.search}$/, mapping.replacement
  end
end