Module: Parameters::Parser

Defined in:
lib/parameters/parser.rb

Class Method Summary

Class Method Details

+ (Object) each_format(&block) {|pattern, parser| ... }

Itereates over each parameter pattern and parser.

Yields:

  • (pattern, parser) — The block will be passed each parameter pattern and the associated parser.

Yield Parameters:

  • (Regexp, String) pattern — The pattern to match recognize parameter values with.
  • (Proc) parser — The parser for the parameter value.


26
27
28
29
30
# File 'lib/parameters/parser.rb', line 26

def Parser.each_format(&block)
  Parser.formats.each do |format|
    block.call(format[:pattern],format[:parser])
  end
end

+ (Array) formats

The parameter patterns and their parsers.

Returns:

  • (Array) — The parameter patterns and their parsers.


9
10
11
# File 'lib/parameters/parser.rb', line 9

def Parser.formats
  @@parameters_parser_formats ||= []
end

+ (Hash{Symbol => Object}) parse(names_and_values)

Parses one or more parameter strings of the form name=value.

Examples:

  Parser.parse(["x=2", "y=true"])
  # => {:x=>2, :y=>true}

Parameters:

  • (Array<String>) names_and_values — The names and values of the parameters, joined by the = character.

Returns:

  • (Hash{Symbol => Object}) — The names and values of the parameters.


115
116
117
118
119
120
121
122
123
# File 'lib/parameters/parser.rb', line 115

def Parser.parse(names_and_values)
  params = {}

  names_and_values.each do |name_and_value|
    params.merge!(Parser.parse_param(name_and_value))
  end

  return params
end

+ (Hash{Symbol => Object}) parse_param(name_and_value)

Parses the a parameter string of the form name=value.

Examples:

  Parser.parse_param('var=2')
  # => {:var=>2}

Parameters:

  • (String) name_and_value — The name and value parameter join with a = character.

Returns:

  • (Hash{Symbol => Object}) — A singleton Hash containing the parameter name and it’s value.

Since:

  • 0.1.8


94
95
96
97
98
99
# File 'lib/parameters/parser.rb', line 94

def Parser.parse_param(name_and_value)
  name, value = name_and_value.split('=',2)

  value = Parser.parse_value(value) if value
  return {name.to_sym => value}
end

+ (Object) parse_value(value)

Recognizes and parses the given parameter value.

Examples:

  Parser.parse_value("0x1")
  # => 1

  Parser.parse_value("'mesg'")
  # => "mesg"

Parameters:

  • (String) value — The parameter value to parse.

Returns:

  • (Object) — The parsed parameter value.


69
70
71
72
73
74
75
76
77
# File 'lib/parameters/parser.rb', line 69

def Parser.parse_value(value)
  Parser.each_format do |pattern,parser|
    if value.match(pattern)
      return parser.call(value)
    end
  end

  return value
end

+ (Object) recognize(pattern, &block) {|value| ... }

Adds a new parameter parser.

Parameters:

  • (Regexp, String) pattern — The pattern to recognize parameter values with.

Yields:

  • (value) — The block will be used as the parser for the recognized parameter values.

Yield Parameters:

  • (String) value — The parameter value to parser.


45
46
47
48
49
50
# File 'lib/parameters/parser.rb', line 45

def Parser.recognize(pattern,&block)
  Parser.formats.unshift({
    :pattern => pattern,
    :parser => block
  })
end