class Formalist::Element::Attributes
Attributes
attrs[R]
Returns the attributes hash.
Public Class Methods
new(attrs = {})
click to toggle source
Creates an attributes object from the supplied hash.
@param attrs [Hash] hash of form element attributes
# File lib/formalist/element/attributes.rb, line 10 def initialize(attrs = {}) @attrs = attrs end
Public Instance Methods
to_ast()
click to toggle source
Returns the attributes as an abstract syntax tree.
@return [Array] the abstract syntax tree
# File lib/formalist/element/attributes.rb, line 17 def to_ast deep_to_ast(deep_simplify(attrs)) end
Private Instance Methods
deep_simplify(value)
click to toggle source
# File lib/formalist/element/attributes.rb, line 36 def deep_simplify(value) case value when Hash value.each_with_object({}) { |(k,v), output| output[k] = deep_simplify(v) } when Array value.map { |v| deep_simplify(v) } when String, Numeric, TrueClass, FalseClass, NilClass value else if value.respond_to?(:to_h) deep_simplify(value.to_h) else value.to_s end end end
deep_to_ast(value)
click to toggle source
# File lib/formalist/element/attributes.rb, line 23 def deep_to_ast(value) case value when Hash [:object, [value.map { |k,v| [k.to_sym, deep_to_ast(v)] }].reject(&:empty?).flatten(1)] when Array [:array, value.map { |v| deep_to_ast(v) }] when String, Numeric, TrueClass, FalseClass, NilClass [:value, [value]] else [:value, [value.to_s]] end end