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,
lib/betty_resource/model/property/types/has_and_belongs_to_many.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
27
28
29
30
31
32
33
34
35
|
# File 'lib/betty_resource/model.rb', line 27
def all(options = {})
begin
response = Api.post("/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
22
23
24
|
# File 'lib/betty_resource/model.rb', line 22
def attributes
properties.map(&:name)
end
|
#create(attributes = {}) ⇒ Object
52
53
54
55
56
|
# File 'lib/betty_resource/model.rb', line 52
def create(attributes = {})
new(attributes).tap do |record|
record.save
end
end
|
#first(options = {}) ⇒ Object
44
45
46
|
# File 'lib/betty_resource/model.rb', line 44
def first(options = {})
all(options.merge(limit: 1)).first
end
|
#get(record_id) ⇒ Object
37
38
39
40
41
42
|
# File 'lib/betty_resource/model.rb', line 37
def get(record_id)
begin
load Api.get("/models/#{id}/records/#{record_id}").parsed_response
rescue MultiJson::DecodeError
end
end
|
#new(attributes = {}) ⇒ Object
48
49
50
|
# File 'lib/betty_resource/model.rb', line 48
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
58
59
60
|
# File 'lib/betty_resource/model.rb', line 58
def to_s
name
end
|