Module: SimpleFormObject

Extended by:
ActiveSupport::Concern
Includes:
ActiveModel::Model
Defined in:
lib/simple_form_object.rb,
lib/simple_form_object/version.rb

Defined Under Namespace

Modules: ClassMethods Classes: Attribute

Constant Summary collapse

VERSION =
"0.0.8"

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/simple_form_object.rb', line 37

def method_missing(method, *args, &block)
  return super unless delegatable?(method)

  # TODO: Figure out why self.class.delegate(method, to: self.class._delegation_target)
  # doesn't work.

  self.class.send(:define_method, method) do |*args, &block|
    _delegation_target.send(method, *args, &block)
  end

  send(method, *args, &block)
end

Instance Method Details

#_delegation_targetObject



59
60
61
62
63
64
65
66
67
# File 'lib/simple_form_object.rb', line 59

def _delegation_target
  target = self.class._delegation_target

  if target.is_a? Symbol
    self.send(target)
  else
    target
  end
end

#attributesObject



84
85
86
87
88
89
90
# File 'lib/simple_form_object.rb', line 84

def attributes
  attribs = {}
  self.class._attributes.each do |a|
    attribs[a.name] = self.send(a.name)
  end
  attribs
end

#column_for_attribute(attribute) ⇒ Object



69
70
71
# File 'lib/simple_form_object.rb', line 69

def column_for_attribute(attribute)
  self.class._attribute(attribute).fake_column
end

#delegatable?(method) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
53
54
55
56
57
# File 'lib/simple_form_object.rb', line 50

def delegatable?(method)

  if !_delegation_target.nil?
    _delegation_target.respond_to?(method)
  else
    false
  end
end

#has_attribute?(attribute_name) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/simple_form_object.rb', line 73

def has_attribute?(attribute_name)
  self.class._attribute(attribute_name).present?
end

#initialize(attributes = {}) ⇒ Object



77
78
79
80
81
82
# File 'lib/simple_form_object.rb', line 77

def initialize(attributes={})
  super
  self.class._attributes.each do |attribute|
    attribute.apply_default_to(self)
  end
end