Class: ObjectJSONMapper::Base
Class Attribute Summary collapse
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
#destroy, included, #reload, #save, #update
included
Methods included from Errors
#load_errors, #valid?
#serializable_hash
Methods included from Conversion
#to_key, #to_model, #to_param, #to_partial_path
Methods included from Local
#find_by_local, included, #local
Constructor Details
#initialize(attributes = {}) ⇒ Base
Returns a new instance of Base.
21
22
23
24
|
# File 'lib/object_json_mapper/base.rb', line 21
def initialize(attributes = {})
self.attributes = attributes
@persisted = false
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
40
41
42
43
|
# File 'lib/object_json_mapper/base.rb', line 40
def method_missing(method_name, *args, &block)
return attributes.fetch(method_name) if respond_to_missing?(method_name)
super
end
|
Class Attribute Details
.associations ⇒ Object
Returns the value of attribute associations.
63
64
65
|
# File 'lib/object_json_mapper/base.rb', line 63
def associations
@associations
end
|
.relation ⇒ Object
Returns the value of attribute relation.
63
64
65
|
# File 'lib/object_json_mapper/base.rb', line 63
def relation
@relation
end
|
.root_url ⇒ Object
Returns the value of attribute root_url.
63
64
65
|
# File 'lib/object_json_mapper/base.rb', line 63
def root_url
@root_url
end
|
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
15
16
17
|
# File 'lib/object_json_mapper/base.rb', line 15
def attributes
@attributes
end
|
#persisted ⇒ Object
Also known as:
persisted?
Returns the value of attribute persisted.
15
16
17
|
# File 'lib/object_json_mapper/base.rb', line 15
def persisted
@persisted
end
|
Class Method Details
.attribute(name, type: nil, default: nil) ⇒ Object
93
94
95
96
97
98
99
100
101
102
103
104
|
# File 'lib/object_json_mapper/base.rb', line 93
def attribute(name, type: nil, default: nil)
define_method(name) do
return default.call if attributes.exclude?(name) && default
return type.call(attributes[name]) if type
attributes[name]
end
define_method("#{name}=") do |value|
attributes[name] = value
end
end
|
.client ⇒ Object
71
72
73
74
75
76
|
# File 'lib/object_json_mapper/base.rb', line 71
def client
RestClient::Resource.new(
URI.join(ObjectJSONMapper.base_url, root_url).to_s,
headers: ObjectJSONMapper.
)
end
|
78
79
80
|
# File 'lib/object_json_mapper/base.rb', line 78
def configure
yield self
end
|
Returns current model instance.
143
144
145
146
147
148
149
150
151
|
# File 'lib/object_json_mapper/base.rb', line 143
def find(id)
raise ActiveRecord::RecordNotFound if id.nil?
result = HTTP.parse_json(client[id].get.body)
persist(result)
rescue RestClient::ExceptionWithResponse
raise ActiveRecord::RecordNotFound
end
|
rubocop:disable Rails/FindBy
157
158
159
|
# File 'lib/object_json_mapper/base.rb', line 157
def find_by(conditions = {})
where(conditions).first
end
|
.inherited(base) ⇒ Object
65
66
67
68
69
|
# File 'lib/object_json_mapper/base.rb', line 65
def inherited(base)
base.root_url = base.name.underscore.pluralize
base.associations = Associations::Registry.new
base.relation = Relation.new(klass: base)
end
|
.name ⇒ Object
86
87
88
|
# File 'lib/object_json_mapper/base.rb', line 86
def name
@name || super
end
|
.name=(value) ⇒ Object
82
83
84
|
# File 'lib/object_json_mapper/base.rb', line 82
def name=(value)
@name = value.to_s
end
|
.none ⇒ Object
161
162
163
|
# File 'lib/object_json_mapper/base.rb', line 161
def none
NullRelation.new(klass: self)
end
|
Same as ‘new` but for persisted records
128
129
130
131
132
|
# File 'lib/object_json_mapper/base.rb', line 128
def persist(attributes = {})
new(attributes).tap do |base|
base.persisted = true
end
end
|
.root(value) ⇒ Object
118
119
120
121
122
123
|
# File 'lib/object_json_mapper/base.rb', line 118
def root(value)
clone.tap do |base|
base.name = name
base.root_url = value.to_s
end
end
|
.scope(name, block) ⇒ Object
108
109
110
111
112
113
114
115
116
|
# File 'lib/object_json_mapper/base.rb', line 108
def scope(name, block)
define_singleton_method(name) do
relation.deep_clone.instance_exec(&block)
end
relation.define_singleton_method(name) do
instance_exec(&block)
end
end
|
Returns collection of model instances.
136
137
138
|
# File 'lib/object_json_mapper/base.rb', line 136
def where(conditions = {})
relation.tap { |relation| relation.klass = self }.where(conditions)
end
|
Instance Method Details
#==(other) ⇒ Object
54
55
56
|
# File 'lib/object_json_mapper/base.rb', line 54
def ==(other)
attributes == other.attributes && persisted == other.persisted
end
|
#client ⇒ Object
58
59
60
|
# File 'lib/object_json_mapper/base.rb', line 58
def client
self.class.client[id]
end
|
#new_record? ⇒ Boolean
28
29
30
|
# File 'lib/object_json_mapper/base.rb', line 28
def new_record?
!persisted?
end
|
#persist ⇒ Object
32
33
34
|
# File 'lib/object_json_mapper/base.rb', line 32
def persist
@persisted = true
end
|
#reloadable? ⇒ Boolean
36
37
38
|
# File 'lib/object_json_mapper/base.rb', line 36
def reloadable?
to_key.any?
end
|
#respond_to_missing?(method_name) ⇒ Boolean
45
46
47
|
# File 'lib/object_json_mapper/base.rb', line 45
def respond_to_missing?(method_name, *)
attributes.key?(method_name)
end
|