Class: Occams::Content::ParamsParser

Inherits:
Object
  • Object
show all
Defined in:
lib/occams/content/params_parser.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

STRING_LITERAL =
%r{'[^']*'|"[^"]*"}.freeze
IDENTIFIER =
%r{[a-z0-9][\w\-/.]*}i.freeze
HASH_KEY =
%r{#{IDENTIFIER}:}.freeze
COMMA =
%r{,}.freeze
HASH_OPEN =
%r{\{}.freeze
HASH_CLOSE =
%r{\}}.freeze
ARRAY_OPEN =
%r{\[}.freeze
ARRAY_CLOSE =
%r{\]}.freeze
INTEGER =
%r{\b[0-9]+\b}i.freeze

Instance Method Summary collapse

Constructor Details

#initialize(string = '') ⇒ ParamsParser

Returns a new instance of ParamsParser.

Parameters:

  • string (String) (defaults to: '')


19
20
21
# File 'lib/occams/content/params_parser.rb', line 19

def initialize(string = '')
  @string = string
end

Instance Method Details

#paramsArray<String, {String => String}>

Takes CMS content tag parameters and converts them into array of strings, hashes and arrays.

Examples:

new("a, b, c").parse
#=> ["a", "b", "c"]

new("a, b: c, d: e").parse
#=> ["a", {"b" => "c", "d" => "e"}]

new("a, b: {c: [d, e]}").parse
#=> ["a", {"b" => {"c" => ["d", "e"]}}]

Returns:

  • (Array<String, {String => String}>)

Raises:

  • (Error)

    if the given ‘text` is malformed.



39
40
41
42
# File 'lib/occams/content/params_parser.rb', line 39

def params
  @tokens = tokenize(@string)
  parse_params
end