Class: MLS::Resource
- Inherits:
-
Object
show all
- Defined in:
- lib/mls/resource.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(attributes = {}, persisted = false) ⇒ Resource
Returns a new instance of Resource.
10
11
12
13
14
15
16
|
# File 'lib/mls/resource.rb', line 10
def initialize(attributes = {}, persisted = false)
@persisted = persisted
@errors = {}
set_default_values
update_attributes(attributes)
end
|
Instance Attribute Details
#errors ⇒ Object
Returns the value of attribute errors.
4
5
6
|
# File 'lib/mls/resource.rb', line 4
def errors
@errors
end
|
#persisted ⇒ Object
Returns the value of attribute persisted.
3
4
5
|
# File 'lib/mls/resource.rb', line 3
def persisted
@persisted
end
|
Class Method Details
.inherited(subclass) ⇒ Object
6
7
8
|
# File 'lib/mls/resource.rb', line 6
def self.inherited(subclass)
subclass.extend(MLS::Model)
end
|
Instance Method Details
#==(other) ⇒ Object
42
43
44
|
# File 'lib/mls/resource.rb', line 42
def ==(other)
self.class == other.class && properties_for_comparison == other.properties_for_comparison
end
|
#new_record? ⇒ Boolean
18
19
20
|
# File 'lib/mls/resource.rb', line 18
def new_record?
!@persisted
end
|
22
23
24
|
# File 'lib/mls/resource.rb', line 22
def persisted?
@persisted
end
|
#properties ⇒ Object
Properties ===================================================================================================
48
49
50
|
# File 'lib/mls/resource.rb', line 48
def properties
self.class.properties
end
|
#properties_excluded_from_comparison ⇒ Object
60
61
62
|
# File 'lib/mls/resource.rb', line 60
def properties_excluded_from_comparison
self.class.properties_excluded_from_comparison
end
|
#properties_for_comparison ⇒ Object
52
53
54
55
56
57
58
|
# File 'lib/mls/resource.rb', line 52
def properties_for_comparison
compare = {}
properties.reject{ |k, p| properties_excluded_from_comparison.include?(k) }.each do |k, p|
compare[k] = self.send(properties[k].name.to_sym)
end
compare
end
|
#save ⇒ Object
26
27
28
|
# File 'lib/mls/resource.rb', line 26
def save
new_record? ? create : update
end
|
#save! ⇒ Object
30
31
32
|
# File 'lib/mls/resource.rb', line 30
def save!
new_record? ? create! : update!
end
|
#set_default_values ⇒ Object
64
65
66
67
68
|
# File 'lib/mls/resource.rb', line 64
def set_default_values
properties.each do |name, property|
self.send("#{name}=".to_sym, property.default) if property.default
end
end
|
#to_hash ⇒ Object
Combo Breaker ================================================================================================
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
# File 'lib/mls/resource.rb', line 76
def to_hash
hash = {}
properties.each do |name, property|
serialize = property.options[:serialize]
serialize = :always if serialize.nil?
case serialize
when :always
hash[name] = property.dump(self.send(name))
when :if_present
hash[name] = property.dump(self.send(name)) if self.send(name)
end
end
hash
end
|
#to_key ⇒ Object
95
96
97
|
# File 'lib/mls/resource.rb', line 95
def to_key
persisted? ? [id] : nil
end
|
#update_attributes(attrs) ⇒ Object
70
71
72
|
# File 'lib/mls/resource.rb', line 70
def update_attributes(attrs)
attrs.each { |k, v| self.send("#{k}=".to_sym, v) } unless attrs.nil?
end
|