Class: ComfortableMexicanSofa::Content::ParamsParser

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

Defined Under Namespace

Classes: Error

Constant Summary collapse

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

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/comfortable_mexican_sofa/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/comfortable_mexican_sofa/content/params_parser.rb', line 39

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