Class: Rasti::Model
- Inherits:
-
Object
show all
- Extended by:
- MultiRequire
- Defined in:
- lib/rasti/model.rb,
lib/rasti/model/errors.rb,
lib/rasti/model/version.rb,
lib/rasti/model/attribute.rb
Defined Under Namespace
Classes: Attribute, NotAssignedAttributeError, UnexpectedAttributesError
Constant Summary
collapse
- VERSION =
'2.0.1'
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(attributes = {}) ⇒ Model
Returns a new instance of Model.
58
59
60
61
|
# File 'lib/rasti/model.rb', line 58
def initialize(attributes={})
@__attributes__ = attributes
validate_defined_attributes! attributes.keys.map(&:to_sym)
end
|
Class Method Details
.[](*args) ⇒ Object
13
14
15
16
17
18
19
20
21
|
# File 'lib/rasti/model.rb', line 13
def [](*args)
Class.new(self) do
if args.count == 1 && args.first.is_a?(Hash)
args.first.each { |name, type| attribute name, type }
else
args.each { |name| attribute name }
end
end
end
|
.attribute_names ⇒ Object
27
28
29
|
# File 'lib/rasti/model.rb', line 27
def attribute_names
@attibute_names ||= attributes.map(&:name)
end
|
.attributes ⇒ Object
23
24
25
|
# File 'lib/rasti/model.rb', line 23
def attributes
@attributes ||= []
end
|
.inspect ⇒ Object
38
39
40
|
# File 'lib/rasti/model.rb', line 38
def to_s
"#{model_name}[#{attribute_names.join(', ')}]"
end
|
.model_name ⇒ Object
31
32
33
|
# File 'lib/rasti/model.rb', line 31
def model_name
name || self.superclass.name
end
|
.to_s ⇒ Object
35
36
37
|
# File 'lib/rasti/model.rb', line 35
def to_s
"#{model_name}[#{attribute_names.join(', ')}]"
end
|
Instance Method Details
#==(other) ⇒ Object
113
114
115
|
# File 'lib/rasti/model.rb', line 113
def ==(other)
other.kind_of?(self.class) && to_h == other.to_h
end
|
#cast_attributes! ⇒ Object
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
# File 'lib/rasti/model.rb', line 67
def cast_attributes!
errors = {}
self.class.attributes.each do |attribute|
begin
if assigned_attribute?(attribute.name)
value = read_attribute attribute
value.cast_attributes! if value.is_a? Model
end
rescue Rasti::Types::CompoundError => ex
ex.errors.each do |key, messages|
errors["#{attribute.name}.#{key}"] = messages
end
rescue Rasti::Types::CastError => ex
errors[attribute.name] = [ex.message]
end
end
raise Rasti::Types::CompoundError.new(errors) unless errors.empty?
end
|
#eql?(other) ⇒ Boolean
109
110
111
|
# File 'lib/rasti/model.rb', line 109
def eql?(other)
instance_of?(other.class) && to_h.eql?(other.to_h)
end
|
#hash ⇒ Object
117
118
119
|
# File 'lib/rasti/model.rb', line 117
def hash
[self.class, to_h].hash
end
|
#merge(new_attributes) ⇒ Object
63
64
65
|
# File 'lib/rasti/model.rb', line 63
def merge(new_attributes)
self.class.new __attributes__.merge(new_attributes)
end
|
#to_h(options = {}) ⇒ Object
90
91
92
93
94
95
96
97
98
99
100
|
# File 'lib/rasti/model.rb', line 90
def to_h(options={})
if options.empty?
serialized_attributes
else
attributes_filter = {only: serialized_attributes.keys, except: []}.merge(options)
(attributes_filter[:only] - attributes_filter[:except]).each_with_object({}) do |name, hash|
hash[name] = serialized_attributes[name]
end
end
end
|
#to_s ⇒ Object
Also known as:
inspect
102
103
104
105
106
|
# File 'lib/rasti/model.rb', line 102
def to_s
cast_attributes!
"#{self.class.model_name}[#{__cache__.map { |n,v| "#{n}: #{v.inspect}" }.join(', ')}]"
end
|