Class: RBlade::CompilesStatements::CompilesProps

Inherits:
Object
  • Object
show all
Defined in:
lib/rblade/compiler/statements/compiles_props.rb

Instance Method Summary collapse

Instance Method Details

#compileProps(args) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/rblade/compiler/statements/compiles_props.rb', line 6

def compileProps args
  if args.nil?
    raise StandardError.new "Props statement: wrong number of arguments (given #{args&.count}, expecting 1)"
  end

  props = extractProps args[0]
  props.map do |key, value|
    if value == "_required"
      "if !defined?(#{key})&&!attributes.has?(:'#{RBlade.escape_quotes(key)}');raise \"Props statement: #{key} is not defined\";end;#{key.sub(/[^a-zA-Z0-9_]/, "_")}=attributes.default(:'#{RBlade.escape_quotes(key)}');"
    else
      "#{key.sub(/[^a-zA-Z0-9_]/, "_")}=attributes.default(:'#{RBlade.escape_quotes(key)}',#{value});"
    end
  end.join
end

#extractProps(prop_string) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rblade/compiler/statements/compiles_props.rb', line 21

def extractProps prop_string
  if !prop_string.start_with?("{") || !prop_string.end_with?("}")
    raise StandardError.new "Props statement: expecting hash as parameter"
  end

  props = {}
  prop_strings = Tokenizer.extractCommaSeparatedValues prop_string[1..-2]

  prop_strings.each do |prop|
    prop.strip!

    key, value = prop.split(/^
      (?:
        ('.+'):
        |
        (".+"):
        |
        ([^ :]+):
        |
        :(?:
          '(.+?)'
          |
          "(.+?)"
          |
          ([^"' ]+)
        )\s*=>
      )
      \s*(.+?)$
    /x).reject(&:empty?)

    if key.nil? || value.nil?
      raise StandardError.new "Props statement: invalid property hash"
    end
    props[key] = value
  end

  props
end