Class: OpenActive::BaseModel
Class Method Summary
collapse
Instance Method Summary
collapse
#check_types, included
included, #to_h, #values
Constructor Details
#initialize(attributes = {}) ⇒ BaseModel
Returns a new instance of BaseModel.
8
9
10
11
12
|
# File 'lib/openactive/base_model.rb', line 8
def initialize(attributes = {})
assign_attributes(attributes) if attributes
super()
end
|
Class Method Details
.define_property(property, types: nil, as: nil, default: nil) ⇒ Object
20
21
22
23
24
25
26
27
28
29
30
31
|
# File 'lib/openactive/base_model.rb', line 20
def self.define_property(property, types: nil, as: nil, default: nil)
attr_accessor property
property property, as: as || property.to_s if as != false
validate_property property, types: types if types
if default
define_method property do
instance_variable_get("@#{property}") || default
end
end
property
end
|
.deserialize(value) ⇒ mixed
Returns a value from a given JSON-LD deserialized array.
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
# File 'lib/openactive/base_model.rb', line 71
def self.deserialize(value)
if value.is_a?(Hash)
if value.key?("@type") || value.key?("type")
type = value["@type"] || value["type"]
type = type.split(":")[1] if type.include?(':') && !type.start_with?("schema:")
klass = ::OpenActive::Models.const_get(type)
inst = klass.deserialize_class(value)
return inst
end
elsif value.is_a?(Array)
value = value.map do |item|
deserialize(item)
end
end
value
end
|
.deserialize_class(data) ⇒ object
Returns an object from a given JSON-LD representation.
54
55
56
57
58
59
60
61
62
63
64
65
|
# File 'lib/openactive/base_model.rb', line 54
def self.deserialize_class(data)
inst = new
return inst unless data.is_a?(Array) || data.is_a?(Hash)
data.each do |key, value|
inst.set_property(key, value)
end
inst
end
|
.serialize(obj, **kwargs) ⇒ string
Returns the JSON-LD representation of the given instance.
TODO: make this more Ruby-esque (to_h, to_hash, to_json)
Instance Method Details
#assign_attributes(attributes) ⇒ Object
14
15
16
17
18
|
# File 'lib/openactive/base_model.rb', line 14
def assign_attributes(attributes)
attributes.each do |key, value|
set_property(key, value)
end
end
|
#deserialize(*data) ⇒ Object
100
101
102
|
# File 'lib/openactive/base_model.rb', line 100
def deserialize(*data)
self.class.deserialize(*data)
end
|
#serialize(**kwargs) ⇒ Object
113
114
115
|
# File 'lib/openactive/base_model.rb', line 113
def serialize(**kwargs)
self.class.serialize(self, **kwargs)
end
|
#set_property(key, value) ⇒ Object
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
# File 'lib/openactive/base_model.rb', line 33
def set_property(key, value)
attr_name = key.to_s.underscore
attr_name = attr_name[1..] if attr_name.start_with?('@')
val = value
val = deserialize(value) if value.is_a?(Array) || value.is_a?(Hash)
return if ["context", "type"].include?(attr_name)
send("#{attr_name}=", val)
end
|
#to_json(schema: false, pretty: false) ⇒ Object
117
118
119
120
121
122
123
|
# File 'lib/openactive/base_model.rb', line 117
def to_json(schema: false, pretty: false)
serialized = serialize(schema: schema)
return JSON.pretty_generate(serialized) if pretty
serialized.to_json
end
|