class BettyResource::Model
Attributes
id[RW]
name[RW]
properties[RW]
Public Class Methods
new(id, name, properties = [])
click to toggle source
# File lib/betty_resource/model.rb, line 14 def initialize(id, name, properties = []) @id, @name, @properties = id, name, properties end
parse(input)
click to toggle source
# 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
Public Instance Methods
all(options = {})
click to toggle source
TODO: Refactor this method in order to handle formatted view JSON correctly
# 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()
click to toggle source
# File lib/betty_resource/model.rb, line 22 def attributes properties.map(&:name) end
create(attributes = {})
click to toggle source
# File lib/betty_resource/model.rb, line 52 def create(attributes = {}) new(attributes).tap do |record| record.save end end
first(options = {})
click to toggle source
# File lib/betty_resource/model.rb, line 44 def first(options = {}) all(options.merge(limit: 1)).first end
get(record_id)
click to toggle source
# 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 = {})
click to toggle source
# File lib/betty_resource/model.rb, line 48 def new(attributes = {}) BettyResource::Model::Record.new(self, attributes) end
property(name)
click to toggle source
# File lib/betty_resource/model.rb, line 18 def property(name) properties.find { |p|p.name == name.to_s } end
to_s()
click to toggle source
# File lib/betty_resource/model.rb, line 58 def to_s name end
Private Instance Methods
load(data, record = nil)
click to toggle source
# File lib/betty_resource/model.rb, line 64 def load(data, record = nil) if data id = data.delete 'id' (record || BettyResource::Model::Record.new(self)).tap do |record| record.instance_variable_set :@id, id record.attributes = data record.clean_up! end end end