Class: ActiveForm

Inherits:
Object
  • Object
show all
Includes:
ActiveRecord::Validations
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



2
3
4
5
6
7
8
9
10
# File 'lib/active_form.rb', line 2

def initialize(attributes = nil)
  # Mass Assignment implementation
  if attributes
    attributes.each do |key, value| 
      self[key] = value 
    end
  end
  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



20
21
22
23
24
25
26
27
# File 'lib/active_form.rb', line 20

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



58
59
60
# File 'lib/active_form.rb', line 58

def human_attribute_name(attribute_key_name)
  attribute_key_name.humanize
end

.human_name(*args) ⇒ Object



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

def human_name(*args)
  name.humanize
end

.raise_not_implemented_error(*params) ⇒ Object Also known as: create, create!, validates_acceptance_of, validates_uniqueness_of, validates_associated, validates_on_create, validates_on_update, save_with_validation

Raises:

  • (NotImplementedError)


62
63
64
# File 'lib/active_form.rb', line 62

def raise_not_implemented_error(*params)
  raise NotImplementedError
end

.self_and_descendants_from_active_recordObject



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

def self_and_descendants_from_active_record
  [self]
end

Instance Method Details

#[](key) ⇒ Object



12
13
14
# File 'lib/active_form.rb', line 12

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

#[]=(key, value) ⇒ Object



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

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

#new_record?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/active_form.rb', line 29

def new_record?
  true
end

#raise_not_implemented_error(*params) ⇒ Object



33
34
35
# File 'lib/active_form.rb', line 33

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

#save!Object



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

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

#update_attributeObject



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

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

#update_attributesObject



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

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