Class: Jrec::Model

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Callbacks
Includes:
ActiveModel::Model, ActiveModel::Serializers::JSON, ActiveModel::Validations, ActiveModel::Validations::Callbacks, PG
Defined in:
lib/jrec/model.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from PG

#config, #config_file, #env, #exec_func, #exec_params, #pg

Constructor Details

#initialize(data = {}) ⇒ Model

Returns a new instance of Model.



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

def initialize data = {}
  data ||= {}
  register_attr_accessors data
  super(data)
end

Class Attribute Details

.collectionObject

Returns the value of attribute collection.



23
24
25
# File 'lib/jrec/model.rb', line 23

def collection
  @collection
end

.schemaObject

Returns the value of attribute schema.



23
24
25
# File 'lib/jrec/model.rb', line 23

def schema
  @schema
end

.tableObject

Returns the value of attribute table.



23
24
25
# File 'lib/jrec/model.rb', line 23

def table
  @table
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



77
78
79
# File 'lib/jrec/model.rb', line 77

def attributes
  @attributes
end

Class Method Details

.count(conds = {}) ⇒ Object



69
70
71
# File 'lib/jrec/model.rb', line 69

def count conds = {}
  collection.count(conds).match(/(\d+)/)[1].to_i
end

.create(data = {}) ⇒ Object



38
39
40
41
42
# File 'lib/jrec/model.rb', line 38

def create data = {}
  record = new(data)
  record.save
  record
end

.data_attr_accessor(*fields) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/jrec/model.rb', line 29

def data_attr_accessor *fields
  fields.each do |field|
    class_eval %Q{ 
      def #{field}; self.attributes['#{field}'] end
      def #{field}=val; self.attributes['#{field}']=val end
    }
  end 
end

Instance Method Details

#destroyObject



112
113
114
115
116
117
118
119
# File 'lib/jrec/model.rb', line 112

def destroy
  return unless self.persisted?
  run_callbacks :destroy do
    if result = self.class.delete(identity)
      process_response result
    end
  end
end

#persisted?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/jrec/model.rb', line 91

def persisted?
  self.uuid.present?
end

#saveObject

persistency



102
103
104
# File 'lib/jrec/model.rb', line 102

def save
  create_or_update if valid?
end

#update(attrs) ⇒ Object



106
107
108
109
110
# File 'lib/jrec/model.rb', line 106

def update attrs
  register_attr_accessors attrs        
  self.attributes.update(attrs)
  save
end

#uuidObject



95
96
97
# File 'lib/jrec/model.rb', line 95

def uuid
  self.attributes['uuid']
end