Class: BettyResource::Model
- Inherits:
-
Object
- Object
- BettyResource::Model
show all
- Defined in:
- lib/betty_resource/model.rb,
lib/betty_resource/model/record.rb,
lib/betty_resource/model/property.rb,
lib/betty_resource/model/property/types/has_many.rb,
lib/betty_resource/model/property/types/belongs_to.rb,
lib/betty_resource/model/property/types/association.rb
Defined Under Namespace
Classes: Property, Record
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(id, name, properties = []) ⇒ Model
Returns a new instance of Model.
14
15
16
|
# File 'lib/betty_resource/model.rb', line 14
def initialize(id, name, properties = [])
@id, @name, @properties = id, name, properties
end
|
Instance Attribute Details
#id ⇒ Object
Returns the value of attribute id.
6
7
8
|
# File 'lib/betty_resource/model.rb', line 6
def id
@id
end
|
#name ⇒ Object
Returns the value of attribute name.
6
7
8
|
# File 'lib/betty_resource/model.rb', line 6
def name
@name
end
|
#properties ⇒ Object
Returns the value of attribute properties.
6
7
8
|
# File 'lib/betty_resource/model.rb', line 6
def properties
@properties
end
|
Class Method Details
.parse(input) ⇒ Object
8
9
10
11
12
|
# File 'lib/betty_resource/model.rb', line 8
def self.parse(input)
input.inject({}) do |hash, row|
hash.merge(row['name'] => Model.new(row['id'], row['name'], Property.parse(row['id'], row['properties'])))
end
end
|
Instance Method Details
#all(options = {}) ⇒ Object
TODO: Refactor this method in order to handle formatted view JSON correctly
32
33
34
35
36
37
38
39
40
|
# File 'lib/betty_resource/model.rb', line 32
def all(options = {})
begin
response = Api.get("/models/#{id}/records", body: options).parsed_response
((view_id = options.delete(:view_id) || options.delete('view_id')).nil? ? response : response['records']).map do |data|
load data
end
rescue MultiJson::DecodeError
end
end
|
#attributes ⇒ Object
27
28
29
|
# File 'lib/betty_resource/model.rb', line 27
def attributes
properties.map(&:name)
end
|
#create(attributes = {}) ⇒ Object
57
58
59
60
61
|
# File 'lib/betty_resource/model.rb', line 57
def create(attributes = {})
new(attributes).tap do |record|
record.save
end
end
|
#first(options = {}) ⇒ Object
49
50
51
|
# File 'lib/betty_resource/model.rb', line 49
def first(options = {})
all(options.merge(limit: 1)).first
end
|
#get(record_id) ⇒ Object
42
43
44
45
46
47
|
# File 'lib/betty_resource/model.rb', line 42
def get(record_id)
begin
load Api.get("/models/#{id}/records/#{record_id}").parsed_response
rescue MultiJson::DecodeError
end
end
|
#new(attributes = {}) ⇒ Object
53
54
55
|
# File 'lib/betty_resource/model.rb', line 53
def new(attributes = {})
BettyResource::Model::Record.new(self, attributes)
end
|
#property(name) ⇒ Object
18
19
20
|
# File 'lib/betty_resource/model.rb', line 18
def property(name)
properties.find { |p|p.name == name.to_s }
end
|
#to_s ⇒ Object
63
64
65
|
# File 'lib/betty_resource/model.rb', line 63
def to_s
name
end
|
#typecast(key, value) ⇒ Object
22
23
24
25
|
# File 'lib/betty_resource/model.rb', line 22
def typecast(key, value)
property = self.property(key)
property ? property.typecast(value) : value
end
|