class DynaForm::Base

Public Class Methods

new(args = {}) click to toggle source
# File lib/dyna_form/base.rb, line 33
def initialize(args = {})
  @lazy_initialization = (args == {})
  args.each do |key, val|
    create_var(key, val)
  end
end
submit(*args, hash) click to toggle source
# File lib/dyna_form/base.rb, line 10
def submit(*args, hash)
  updated_hsh = hash.with_indifferent_access
  unless updated_hsh.length == SUBMIT_HASH_LIMIT && updated_hsh.has_key?(:to)
    raise "Invalid parameter list." 
  end

  raw_model = updated_hsh[:to].to_s
  words = raw_model.split("_")
  model_name = words.map { |w| w[0..0].capitalize + w[1..-1] }.join("")
  
  fields =  begin
              class_variable_get(:@@fields)
            rescue NameError
              class_variable_set(:@@fields, {})
              {}
            end

  fields[model_name] ||= []
  fields[model_name] += args
  class_variable_set(:@@fields, fields)
end

Public Instance Methods

method_missing(method, *arguments, &block) click to toggle source
Calls superclass method
# File lib/dyna_form/base.rb, line 40
def method_missing(method, *arguments, &block)
  @lazy_initialization ? create_var(method, arguments.first) : super
end
persisted?() click to toggle source
# File lib/dyna_form/base.rb, line 50
def persisted?
  false
end
submit() click to toggle source
# File lib/dyna_form/base.rb, line 44
def submit
  result = true
  submissions.each { |s| result && s.submit }
  result
end

Protected Instance Methods

attributes() click to toggle source
# File lib/dyna_form/base.rb, line 70
def attributes
  return @attributes unless @attributes.nil?

  @attributes = {}
  instance_variables.each do |var|
    attribute = var.to_s[1..-1].to_sym
    @attributes[attribute] = instance_variable_get(var)
  end
  @attributes
end
create_var(var_name, var_value) click to toggle source
# File lib/dyna_form/base.rb, line 81
def create_var(var_name, var_value)
  updated_var_name =  if var_name[-1, 1] == '='
                        var_name[0...-1]
                      else
                        var_name
                      end

  self.instance_variable_set("@#{updated_var_name}", var_value)

  self.class.instance_eval do
    attr_accessor "#{updated_var_name}"
  end
end
submissions() click to toggle source
# File lib/dyna_form/base.rb, line 56
def submissions
  return @submissions unless @submissions.nil?

  @submissions = []

  # TODO: we might be able to change this to just @@fields. More testing
  # will be required to ensure that that is the case.
  fields = self.class.class_variable_get(:@@fields)
  fields.each do |model, variables|
    @submissions << DynaForm::Submission.new(model, variables, attributes)
  end
  @submissions
end