Class: ActiveForm

Inherits:
Object
  • Object
show all
Includes:
Validatable
Defined in:
lib/active_form.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = nil) {|_self| ... } ⇒ ActiveForm

Returns a new instance of ActiveForm.

Yields:

  • (_self)

Yield Parameters:

  • _self (ActiveForm)

    the object that the method was called on



4
5
6
7
# File 'lib/active_form.rb', line 4

def initialize(attributes = nil)
  self.attributes=(attributes)
  yield self if block_given?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_id, *params) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/active_form.rb', line 29

def method_missing(method_id, *params)
  # Implement _before_type_cast accessors
  if md = /_before_type_cast$/.match(method_id.to_s)
    attr_name = md.pre_match
    return self[attr_name] if self.respond_to?(attr_name)
  end
  super
end

Class Method Details

.human_attribute_name(attribute_key_name) ⇒ Object



74
75
76
# File 'lib/active_form.rb', line 74

def human_attribute_name(attribute_key_name)
  attribute_key_name.humanize
end

.human_name(*args) ⇒ Object



70
71
72
# File 'lib/active_form.rb', line 70

def human_name(*args)
  name.humanize
end

.raise_not_implemented_error(*params) ⇒ Object

Raises:

  • (NotImplementedError)


78
79
80
# File 'lib/active_form.rb', line 78

def raise_not_implemented_error(*params)
  raise NotImplementedError
end

.self_and_descendants_from_active_recordObject



66
67
68
# File 'lib/active_form.rb', line 66

def self_and_descendants_from_active_record
  [self]
end

Instance Method Details

#[](key) ⇒ Object



21
22
23
# File 'lib/active_form.rb', line 21

def [](key)
  instance_variable_get("@#{key}")
end

#[]=(key, value) ⇒ Object



25
26
27
# File 'lib/active_form.rb', line 25

def []=(key, value)
  instance_variable_set("@#{key}", value)
end

#attributesObject



15
16
17
18
19
# File 'lib/active_form.rb', line 15

def attributes
  attributes = instance_variables
  attributes.delete("@errors")
  Hash[*attributes.collect { |attribute| [attribute[1..-1].to_sym, instance_variable_get(attribute)] }.flatten]
end

#attributes=(attributes) ⇒ Object



9
10
11
12
13
# File 'lib/active_form.rb', line 9

def attributes=(attributes)
  attributes.each do |key, value|
    self[key] = value
  end if attributes
end

#new_record?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/active_form.rb', line 45

def new_record?
  true
end

#raise_not_implemented_error(*params) ⇒ Object



49
50
51
# File 'lib/active_form.rb', line 49

def raise_not_implemented_error(*params)
  self.class.raise_not_implemented_error(params)
end

#save!Object



54
55
56
# File 'lib/active_form.rb', line 54

def raise_not_implemented_error(*params)
  self.class.raise_not_implemented_error(params)
end

#to_xml(options = {}) ⇒ Object



38
39
40
41
# File 'lib/active_form.rb', line 38

def to_xml(options = {})
  options[:root] ||= self.class.to_s.underscore
  attributes.to_xml(options)
end

#update_attributeObject



55
56
57
# File 'lib/active_form.rb', line 55

def raise_not_implemented_error(*params)
  self.class.raise_not_implemented_error(params)
end

#update_attributesObject



56
57
58
# File 'lib/active_form.rb', line 56

def raise_not_implemented_error(*params)
  self.class.raise_not_implemented_error(params)
end