class ToQuickform::Generator

Generator

Public Class Methods

new(attributes = {}) click to toggle source
# File lib/to_quickform/generator.rb, line 14
def initialize(attributes = {})
  if attributes.is_a?(Hash)
    (required_attributes + optional_attributes).each do |key|
      value = attributes[key]
      self.send "#{key}=", value
    end
  end
  attr_missing!
end

Public Instance Methods

to_add_rule() click to toggle source
# File lib/to_quickform/generator.rb, line 40
def to_add_rule
  elements.map do |element|
    if element.rule

      name = element.name
      element.rule.map do |rule|
        # Read template to be used
        template = "form_#{rule.type}_rule_template"
        template = nil unless respond_to? template

        erb = ToQuickform::ERBFactory.new(template).get_rule_instance(rule)
        erb.result(binding)
      end.join
    end
  end.join
end
to_create_element() click to toggle source
# File lib/to_quickform/generator.rb, line 29
def to_create_element
  elements.map do |element|
    # Read template to be used
    template = "form_#{element.type}_element_template"
    template = nil unless respond_to? template

    erb = ToQuickform::ERBFactory.new(template).get_element_instancue(element)
    erb.result(binding)
  end.join
end
to_set_defaults() click to toggle source
# File lib/to_quickform/generator.rb, line 24
def to_set_defaults
  erb = ToQuickform::ERBFactory.new(form_default_template).get_default_instance
  erb.result(binding)
end

Private Instance Methods

defaults() click to toggle source
# File lib/to_quickform/generator.rb, line 65
def defaults
  elements.select do |element|
    element.default
  end.inject({}) do |defaults, element|
    defaults.merge({ element.name => element.default })
  end
end
do_quote?(element, key) click to toggle source
# File lib/to_quickform/generator.rb, line 81
def do_quote?(element, key)
  method = "quote_" + key
  return true unless element.respond_to?(method)
  element.send(method)
end
elements() click to toggle source
# File lib/to_quickform/generator.rb, line 59
def elements
  data["element"].map do |element|
    ToQuickform::ElementFactory.new(element)
  end
end
output(element, key) click to toggle source
# File lib/to_quickform/generator.rb, line 75
def output(element, key)
  output = element.send(key).to_s
  output = "'#{output}'" if do_quote?(element, key)
  output
end