Class: OttomanORM::Model

Inherits:
Object show all
Includes:
Representation
Defined in:
lib/ottoman_orm/model.rb

Constant Summary collapse

@@_attributes =

attributes list

[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Representation

#to_hash, #to_json

Constructor Details

#initialize(params = {}) ⇒ Model

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


22
23
24
25
26
27
28
# 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

Instance Attribute Details

#_idObject

internal references



7
8
9
# File 'lib/ottoman_orm/model.rb', line 7

def _id
  @_id
end

Class Method Details

.attribute(*names) ⇒ Object Also known as: attributes

attributes



31
32
33
34
# File 'lib/ottoman_orm/model.rb', line 31

def self.attribute *names
    names.each { |name| @@_attributes << name }
    class_eval { attr_accessor *names }
end

.create(data) ⇒ Object

object creation



42
43
44
# File 'lib/ottoman_orm/model.rb', line 42

def self.create data
    self.new(data).save
end

.fetch(name, id) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
# 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) ⇒ Object



105
106
107
# 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

Instance Method Details

#attributesObject



37
38
39
# File 'lib/ottoman_orm/model.rb', line 37

def attributes
    @@_attributes
end

#create_or_updateObject



78
79
80
# File 'lib/ottoman_orm/model.rb', line 78

def create_or_update
    new_record? ? create_record : update_record
end

#create_recordObject



82
83
84
85
# File 'lib/ottoman_orm/model.rb', line 82

def create_record
    @_id = OttomanORM.client.create self.class.name, self.to_json
    self
end

#deleteObject

delete record



124
125
126
127
# File 'lib/ottoman_orm/model.rb', line 124

def delete
    OttomanORM.client.delete(@_id) unless new_record?
    freeze
end

#idObject



63
64
65
# File 'lib/ottoman_orm/model.rb', line 63

def id
    @_id
end

#new_record?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/ottoman_orm/model.rb', line 59

def new_record?
    @_id.blank?
end

#persisted?Boolean

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

Returns:

  • (Boolean)


55
56
57
# File 'lib/ottoman_orm/model.rb', line 55

def persisted?
    !new_record?
end

#run_validationsObject

validations



68
69
70
# File 'lib/ottoman_orm/model.rb', line 68

def run_validations
    true
end

#saveObject

save record



73
74
75
76
# File 'lib/ottoman_orm/model.rb', line 73

def save
    return false unless run_validations
    create_or_update
end

#update_attribute(attribute, value) ⇒ Object

update record



110
111
112
113
# File 'lib/ottoman_orm/model.rb', line 110

def update_attribute attribute, value
    public_send("#{attribute}=", value)
    save
end

#update_attributes(attributes) ⇒ Object



115
116
117
118
119
120
121
# 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_recordObject



87
88
89
90
# File 'lib/ottoman_orm/model.rb', line 87

def update_record
    OttomanORM.client.update @_id, self.to_hash
    self
end