Class: ActiveModel

Inherits:
Object
  • Object
show all
Includes:
ActiveRecord::Callbacks, ActiveRecord::Validations
Defined in:
lib/active_model.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ ActiveModel

Returns a new instance of ActiveModel.



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

def initialize(attributes={})
  self.attributes = attributes if attributes
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



73
74
75
76
77
78
79
80
# File 'lib/active_model.rb', line 73

def method_missing(method_id, *args, &block)
  method_name = method_id.to_s
  if md = /(\?)$/.match(method_name)
    query_attribute(md.pre_match)
  else
    super
  end
end

Class Method Details

.attribute_namesObject



7
8
9
# File 'lib/active_model.rb', line 7

def attribute_names
  read_inheritable_attribute(:attribute_names) || []
end

.base_classObject



35
36
37
# File 'lib/active_model.rb', line 35

def base_class
  self
end

.human_attribute_name(attribute_key_name) ⇒ Object



24
25
26
# File 'lib/active_model.rb', line 24

def human_attribute_name(attribute_key_name)
  attribute_key_name.humanize
end

.human_nameObject



28
29
30
31
32
33
# File 'lib/active_model.rb', line 28

def human_name
  defaults = self_and_descendants_from_active_record.map do |klass|
    :"#{klass.name.underscore}"
  end 
  defaults << self.name.humanize
end

.instantiateObject



11
12
13
# File 'lib/active_model.rb', line 11

def instantiate
  invalid_method
end

.invalid_methodObject



120
121
122
# File 'lib/active_model.rb', line 120

def self.invalid_method
  raise "Invalid method for ActiveModel"
end

.model_attr(*attribute_names) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/active_model.rb', line 15

def model_attr(*attribute_names)
  attribute_names.collect!{|a| a.to_s}
  write_inheritable_array(:attribute_names, attribute_names)
  attribute_names.each do |a|
    attr_accessor a
    alias_method "#{a}_before_type_cast", a
  end
end

.self_and_descendants_from_active_recordObject



39
40
41
42
43
44
45
46
# File 'lib/active_model.rb', line 39

def self_and_descendants_from_active_record
  klass = self
  classes = [klass]
  while klass != klass.base_class  
    classes << klass = klass.superclass
  end
  classes
end

Instance Method Details

#[](attribute) ⇒ Object



89
90
91
# File 'lib/active_model.rb', line 89

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

#[]=(attribute, value) ⇒ Object



93
94
95
# File 'lib/active_model.rb', line 93

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

#attribute_namesObject



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

def attribute_names
  self.class.attribute_names
end

#attributesObject



58
59
60
61
62
63
# File 'lib/active_model.rb', line 58

def attributes
  self.attribute_names.inject({}) do |attrs, name|
    attrs[name] = send(name)
    attrs
  end
end

#attributes=(attributes) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/active_model.rb', line 65

def attributes=(attributes)
  return unless attributes
  attributes.stringify_keys!
  attributes.each do |k, v|
    send("#{k}=", v)
  end
end

#create_or_updateObject



87
# File 'lib/active_model.rb', line 87

def create_or_update; end

#invalid_methodObject Also known as: create, update, destroy, update_attribute



123
# File 'lib/active_model.rb', line 123

def invalid_method; self.class.invalid_method; end

#new_record?Boolean

Returns:

  • (Boolean)


130
131
132
# File 'lib/active_model.rb', line 130

def new_record?
  false
end

#query_attribute(attr_name) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/active_model.rb', line 97

def query_attribute(attr_name)
  attribute = self[attr_name]
  if attribute.kind_of?(Fixnum) && attribute == 0
    false
  elsif attribute.kind_of?(String) && attribute == "0"
    false
  elsif attribute.kind_of?(String) && attribute.empty?
    false
  elsif attribute.nil?
    false
  elsif attribute == false
    false
  elsif attribute == "f"
    false
  elsif attribute == "false"
    false
  else
    true
  end
end

#saveObject Also known as: save!



82
83
84
# File 'lib/active_model.rb', line 82

def save
  create_or_update
end