Class: SimpleModel::Base
- Inherits:
-
Object
show all
- Extended by:
- ActiveModel::Naming
- Includes:
- ActiveModel::Conversion, ActiveModel::Serializers::JSON, ActiveModel::Serializers::Xml, Association::Model
- Defined in:
- lib/simplemodel/base.rb,
lib/simplemodel/base.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
included
Constructor Details
#initialize(opts = {}) ⇒ Base
Returns a new instance of Base.
50
51
52
53
|
# File 'lib/simplemodel/base.rb', line 50
def initialize(opts = {})
@unknown_attributes = {}
self.attributes = opts
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_symbol, *arguments) ⇒ Object
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
# File 'lib/simplemodel/base.rb', line 87
def method_missing(method_symbol, *arguments) method_name = method_symbol.to_s
if method_name =~ /(=|\?)$/
case $1
when "="
if not self.raw_attributes.has_key?($`.to_s.to_sym)
self.attributes.delete($`)
self.unknown_attributes[$`] = arguments.first
else
@attributes[$`] = arguments.first
end
when "?"
@attributes.has_key?($`)
end
else
return attributes[method_name] if attributes.has_key?(method_name)
super
end
end
|
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
18
19
20
|
# File 'lib/simplemodel/base.rb', line 18
def attributes
@attributes
end
|
#unknown_attributes ⇒ Object
Returns the value of attribute unknown_attributes.
18
19
20
|
# File 'lib/simplemodel/base.rb', line 18
def unknown_attributes
@unknown_attributes
end
|
Class Method Details
.attributes(*attributes) ⇒ Object
13
14
15
|
# File 'lib/simplemodel/base.rb', line 13
def attributes(*attributes)
self.known_attributes |= attributes.map(&:to_s)
end
|
.define_attributes(*attr) ⇒ Object
7
8
9
10
11
|
# File 'lib/simplemodel/base.rb', line 7
def define_attributes(*attr)
self.raw_attributes = {}
attr.each{|a| self.raw_attributes[a] = nil}
attributes *attr
end
|
Instance Method Details
#has_attribute?(name) ⇒ Boolean
55
56
57
|
# File 'lib/simplemodel/base.rb', line 55
def has_attribute?(name)
self.attributes.has_key?(name)
end
|
#known_attributes ⇒ Object
30
31
32
|
# File 'lib/simplemodel/base.rb', line 30
def known_attributes
self.class.known_attributes | self.attributes.keys.map(&:to_s)
end
|
#raw_attributes ⇒ Object
34
35
36
|
# File 'lib/simplemodel/base.rb', line 34
def raw_attributes
self.class.raw_attributes
end
|
#refresh ⇒ Object
20
21
22
23
24
25
26
27
28
|
# File 'lib/simplemodel/base.rb', line 20
def refresh()
self.attributes.each do |name, attr|
res = self.send "#{name}=".to_sym, attr
if not self.raw_attributes.has_key?(name.to_s.to_sym)
self.attributes.delete(name)
end
end
return self
end
|
#respond_to?(method, include_priv = false) ⇒ Boolean
61
62
63
64
65
66
67
68
69
70
71
72
|
# File 'lib/simplemodel/base.rb', line 61
def respond_to?(method, include_priv = false)
method_name = method.to_s
if self.attributes.nil?
super
elsif known_attributes.include?(method_name)
true
elsif method_name =~ /(?:=|\?)$/ && self.attributes.include?($`)
true
else
super
end
end
|
#respond_to_without_attributes? ⇒ Object
59
|
# File 'lib/simplemodel/base.rb', line 59
alias_method :respond_to_without_attributes?, :respond_to?
|