Class: Rhoconnect::StoreOrm

Inherits:
Object
  • Object
show all
Defined in:
lib/rhoconnect/store_orm.rb

Overview

Taken from github.com/voloko/store-model

Simple ORM for store-rb.

Direct Known Subclasses

ApiToken, App, BulkData, Client, ReadState, User

Defined Under Namespace

Modules: Marshal Classes: FieldProxy, ListProxy, SetProxy

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id = nil) ⇒ StoreOrm

Returns a new instance of StoreOrm.



8
9
10
# File 'lib/rhoconnect/store_orm.rb', line 8

def initialize(id=nil)
  self.id = id
end

Class Attribute Details

.prefixObject

Defaults to model_name.dasherize



68
69
70
# File 'lib/rhoconnect/store_orm.rb', line 68

def prefix
  @prefix
end

.validates_presenceObject

Returns the value of attribute validates_presence.



69
70
71
# File 'lib/rhoconnect/store_orm.rb', line 69

def validates_presence
  @validates_presence
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



6
7
8
# File 'lib/rhoconnect/store_orm.rb', line 6

def id
  @id
end

Class Method Details

._field_key(p, i, n) ⇒ Object

:nodoc:



75
76
77
# File 'lib/rhoconnect/store_orm.rb', line 75

def _field_key(p,i,n) #:nodoc:
  "#{p}:#{i}:#{n}"
end

._prefixObject



71
72
73
# File 'lib/rhoconnect/store_orm.rb', line 71

def _prefix
  class_prefix(self)
end

.class_prefix(classname) ⇒ Object



79
80
81
82
83
84
85
# File 'lib/rhoconnect/store_orm.rb', line 79

def class_prefix(classname)
  classname.to_s.
    sub(%r{(.*::)}, '').
    gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
    gsub(/([a-z\d])([A-Z])/,'\1_\2').
    downcase
end

.create(params = {}, attributes = {}) ⇒ Object Also known as: with_next_key

Creates new model instance with new uniqid NOTE: “sequence:model_name:id” key is used

Raises:

  • (ArgumentError)


89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/rhoconnect/store_orm.rb', line 89

def create(params = {}, attributes = {})
  raise ArgumentError.new("Record already exists for '#{params[:id]}'") if self.is_exist?(params[:id])
  if self.validates_presence
    self.validates_presence.each do |field|
      raise ArgumentError.new("Missing required field '#{field}'") unless params[field]
    end
  end
  o = self.new
  o.id = params[:id].nil? ? o.next_id : params[:id]
  params[:rho__id] = params[:id]
  populate_model(o,params)
  populate_attributes(o,attributes)
end

.field(name, type = :string) ⇒ Object Also known as: value

Defines marshaled rw accessor for store string value



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/rhoconnect/store_orm.rb', line 126

def field(name, type = :string)
  if @fields.nil?
    @fields = []
    field :rho__id, :string
  end        
  type = type.to_sym
  type = :integer if type == :int

  class_name = marshal_class_name(name, type)

  fields << {:name => name.to_s, :type => type}
  if type == :string
    class_eval "def #{name}; @#{name} ||= store.get_value(field_key('#{name}')); end"
    class_eval "def #{name}=(value); @#{name} = value.to_s; store.put_value(field_key('#{name}'), value.to_s); end"
  else
    class_eval "def #{name}; @#{name} ||= Marshal::#{class_name}.load(store.get_value(field_key('#{name}'))); end"
    class_eval "def #{name}=(value); @#{name} = value; store.put_value(field_key('#{name}'), Marshal::#{class_name}.dump(value)); end"
  end
end

.fieldsObject

:nodoc:



174
175
176
# File 'lib/rhoconnect/store_orm.rb', line 174

def fields #:nodoc:
  @fields ||= []
end

.is_exist?(id) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/rhoconnect/store_orm.rb', line 49

def self.is_exist?(id)
  !store.get_value(self._field_key(self._prefix,id,'rho__id')).nil?
end

.list(name, type = :string) ⇒ Object

Defines accessor for store list



148
149
150
151
152
153
154
# File 'lib/rhoconnect/store_orm.rb', line 148

def list(name, type = :string)
  class_name = marshal_class_name(name, type)

  fields << {:name => name.to_s, :type => :list}
  class_eval "def #{name}; @#{name} ||= ListProxy.new(self.store, field_key('#{name}'), Marshal::#{class_name}); end"
  eval_writer(name)
end

.load(id, params = {}) ⇒ Object



103
104
105
# File 'lib/rhoconnect/store_orm.rb', line 103

def load(id, params={})
  populate_attributes(self.with_key(id),params) if self.is_exist?(id)
end

.marshal_class_name(name, type) ⇒ Object



165
166
167
# File 'lib/rhoconnect/store_orm.rb', line 165

def marshal_class_name(name, type)
  Marshal::TYPES[type] or raise ArgumentError.new("Unknown type #{type} for field #{name}")
end

.populate_attributes(obj, attribs) ⇒ Object



107
108
109
110
111
112
# File 'lib/rhoconnect/store_orm.rb', line 107

def populate_attributes(obj,attribs)
  attribs.each do |attrib,value|
    obj.send "#{attrib.to_s}=".to_sym, value
  end
  obj
end

.set(name, type = :string) ⇒ Object

Defines accessor for store set



157
158
159
160
161
162
163
# File 'lib/rhoconnect/store_orm.rb', line 157

def set(name, type = :string)
  class_name = marshal_class_name(name, type)

  fields << {:name => name.to_s, :type => :set}
  class_eval "def #{name}; @#{name} ||= SetProxy.new(self.store, field_key('#{name}'), Marshal::#{class_name}); end"
  eval_writer(name)
end

.storeObject

Redefine this to change connection options



170
171
172
# File 'lib/rhoconnect/store_orm.rb', line 170

def store
  @@store ||= Store.get_store(0)
end

.validates_presence_of(*names) ⇒ Object



114
115
116
117
118
119
# File 'lib/rhoconnect/store_orm.rb', line 114

def validates_presence_of(*names)
  self.validates_presence ||= []
  names.each do |name|
    self.validates_presence << name
  end
end

Instance Method Details

#decrement!(name, amount = 1) ⇒ Object

Decrement the specified integer field by 1 or the specified amount.

Raises:

  • (ArgumentError)


40
41
42
43
# File 'lib/rhoconnect/store_orm.rb', line 40

def decrement!(name,amount=1)
  raise ArgumentError, "Only integer fields can be decremented." unless self.class.fields.include?({:name => name.to_s, :type => :integer})
  store.update_count(field_key(name), -amount)
end

#delete(name = nil) ⇒ Object

Issues delete commands for all defined fields



17
18
19
20
21
22
23
24
25
# File 'lib/rhoconnect/store_orm.rb', line 17

def delete(name = nil)
  if name
    store.delete_value(field_key(name.to_s))
  else
    self.class.fields.each do |field|
      store.delete_value(field_key(field[:name]))
    end
  end
end

#field_key(name) ⇒ Object

:nodoc:



27
28
29
# File 'lib/rhoconnect/store_orm.rb', line 27

def field_key(name) #:nodoc:
  self.class._field_key(prefix,id,name)
end

#increment!(name, amount = 1) ⇒ Object

Increment the specified integer field by 1 or the specified amount.

Raises:

  • (ArgumentError)


33
34
35
36
# File 'lib/rhoconnect/store_orm.rb', line 33

def increment!(name,amount=1)
  raise ArgumentError, "Only integer fields can be incremented." unless self.class.fields.include?({:name => name.to_s, :type => :integer})
  store.update_count(field_key(name), amount)
end

#next_idObject

:nodoc:



45
46
47
# File 'lib/rhoconnect/store_orm.rb', line 45

def next_id #:nodoc:
  store.incr "sequence:#{self.prefix}:id"
end

#storeObject

:nodoc:



12
13
14
# File 'lib/rhoconnect/store_orm.rb', line 12

def store #:nodoc:
  self.class.store
end

#to_arrayObject



53
54
55
56
57
58
59
# File 'lib/rhoconnect/store_orm.rb', line 53

def to_array
  res = []
  self.class.fields.each do |field|
    res << field.merge!(:value => send(field[:name].to_sym))
  end
  res
end