class OttomanORM::Model
Attributes
_id[RW]
internal references
Public Class Methods
attribute(*names)
click to toggle source
attributes
# File lib/ottoman_orm/model.rb, line 31 def self.attribute *names names.each { |name| @@_attributes << name } class_eval { attr_accessor *names } end
Also aliased as: attributes
create(data)
click to toggle source
object creation
# File lib/ottoman_orm/model.rb, line 42 def self.create data self.new(data).save end
fetch(name, id)
click to toggle source
# File lib/ottoman_orm/model.rb, line 92 def self.fetch name, id # r = [] # OttomanORM.client.get(id).each_pair do |key, value| # if v[0].is_a?(Hash) # instance = new(value[0].extract!(*@@_attributes)) # instance._id = key # r << instance # end # end # r.empty? ? nil : r.length == 1 ? r[0] : r OttomanORM.client.get name, id end
fetch_by_field(name, field, value)
click to toggle source
# File lib/ottoman_orm/model.rb, line 105 def self.fetch_by_field name, field, value OttomanORM.client.get_by_field name, field, value end
new(params={})
click to toggle source
Initializes a new model with the given params
.
class Person include OttomanORM::Model attr_accessor :name, :age end person = Person.new(name: 'bob', age: '18') person.name # => "bob" person.age # => 18
Calls superclass method
# File lib/ottoman_orm/model.rb, line 22 def initialize(params={}) params.each do |attr, value| self.public_send("#{attr}=", value) end if params super() end
Public Instance Methods
attributes()
click to toggle source
# File lib/ottoman_orm/model.rb, line 37 def attributes @@_attributes end
create_or_update()
click to toggle source
# File lib/ottoman_orm/model.rb, line 78 def create_or_update new_record? ? create_record : update_record end
create_record()
click to toggle source
# File lib/ottoman_orm/model.rb, line 82 def create_record @_id = OttomanORM.client.create self.class.name, self.to_json self end
delete()
click to toggle source
delete record
# File lib/ottoman_orm/model.rb, line 124 def delete OttomanORM.client.delete(@_id) unless new_record? freeze end
id()
click to toggle source
# File lib/ottoman_orm/model.rb, line 63 def id @_id end
new_record?()
click to toggle source
# File lib/ottoman_orm/model.rb, line 59 def new_record? @_id.blank? end
persisted?()
click to toggle source
Indicates if the model is persisted. Default is false
.
class Person include OttomanORM::Model attr_accessor :id, :name end person = Person.new(id: 1, name: 'bob') person.persisted? # => false
# File lib/ottoman_orm/model.rb, line 55 def persisted? !new_record? end
run_validations()
click to toggle source
validations
# File lib/ottoman_orm/model.rb, line 68 def run_validations true end
save()
click to toggle source
save record
# File lib/ottoman_orm/model.rb, line 73 def save return false unless run_validations create_or_update end
update_attribute(attribute, value)
click to toggle source
update record
# File lib/ottoman_orm/model.rb, line 110 def update_attribute attribute, value public_send("#{attribute}=", value) save end
update_attributes(attributes)
click to toggle source
# File lib/ottoman_orm/model.rb, line 115 def update_attributes attributes return if attributes.blank? attributes.stringify_keys.each_pair do |attribute, value| public_send("#{attribute}=", value) end save end
update_record()
click to toggle source
# File lib/ottoman_orm/model.rb, line 87 def update_record OttomanORM.client.update @_id, self.to_hash self end