Class: FlyIO::Model
- Inherits:
-
Object
show all
- Defined in:
- lib/fly_io/model.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(attributes = nil, schema_name: if self.class.const_defined?(:SCHEMA_NAME,
false)
self.class::SCHEMA_NAME
end, **keyword_attributes) ⇒ Model
Returns a new instance of Model.
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
# File 'lib/fly_io/model.rb', line 9
def initialize(attributes = nil, schema_name: if self.class.const_defined?(:SCHEMA_NAME,
false)
self.class::SCHEMA_NAME
end,
**keyword_attributes)
if attributes && !keyword_attributes.empty?
raise FlyIO::ArgumentError, "provide model attributes as a Hash or keywords, not both"
end
attributes ||= keyword_attributes
raise FlyIO::ArgumentError, "model attributes must be a Hash" unless attributes.is_a?(Hash)
@schema_name = schema_name
@attributes = attributes.each_with_object({}) do |(key, value), result|
result[key.to_s] = SchemaRegistry.coerce_property(schema_name, key.to_s, value)
end.freeze
freeze
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *arguments) ⇒ Object
40
41
42
43
44
|
# File 'lib/fly_io/model.rb', line 40
def method_missing(name, *arguments)
return self[name] if arguments.empty? && key?(name)
super
end
|
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
7
8
9
|
# File 'lib/fly_io/model.rb', line 7
def attributes
@attributes
end
|
#schema_name ⇒ Object
Returns the value of attribute schema_name.
7
8
9
|
# File 'lib/fly_io/model.rb', line 7
def schema_name
@schema_name
end
|
Instance Method Details
#==(other) ⇒ Object
50
51
52
|
# File 'lib/fly_io/model.rb', line 50
def ==(other)
other.is_a?(Model) && other.schema_name == schema_name && other.attributes == attributes
end
|
#[](key) ⇒ Object
28
29
30
|
# File 'lib/fly_io/model.rb', line 28
def [](key)
attributes[key.to_s]
end
|
#key?(key) ⇒ Boolean
32
33
34
|
# File 'lib/fly_io/model.rb', line 32
def key?(key)
attributes.key?(key.to_s)
end
|
#respond_to_missing?(name, include_private = false) ⇒ Boolean
46
47
48
|
# File 'lib/fly_io/model.rb', line 46
def respond_to_missing?(name, include_private = false)
key?(name) || super
end
|
#to_h ⇒ Object
36
37
38
|
# File 'lib/fly_io/model.rb', line 36
def to_h
attributes.transform_values { |value| serialize(value) }
end
|