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
20
21
22
23
24
25
26
# 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|
    compiled_code = ""
    if value == "_required"
      compiled_code << "if !attributes.has?(:'#{RBlade.escape_quotes(key)}');raise \"Props statement: #{key} is not defined\";end;"
    end
    if isValidVariableName key
      compiled_code << "#{key}=attributes[:'#{RBlade.escape_quotes(key)}'].nil? ? #{value} : attributes[:'#{RBlade.escape_quotes(key)}'];"
      compiled_code << "attributes.delete :'#{RBlade.escape_quotes(key)}';"
    else
      compiled_code << "attributes.default(:'#{RBlade.escape_quotes(key)}', #{value});"
    end

    compiled_code
  end.join
end