Module: CuteKV::Document::ClassMethods

Defined in:
lib/cute_kv/document.rb

Constant Summary collapse

Serializers =
{
  :json => {:ser=> lambda{|value| JSON.generate(value)},
    :desr=>lambda{|raw| JSON.parse(raw)},
    :type=>"json"
  },
  :marshal => {:ser=> lambda{|value| Marshal.dump(value)},
    :desr=>lambda{|raw| Marshal.load(raw)},
    :type => "marshal"
  }
}

Instance Method Summary collapse

Instance Method Details

#active(value, new_id = false) ⇒ Object

Active sleeped value so we’ll get a live object :-) It’s should not generate a new id, because this value has included a id



219
220
221
222
# File 'lib/cute_kv/document.rb', line 219

def active(value, new_id=false)
  id = value.delete("id")
  new(value, new_id) {|obj| obj.instance_variable_set(:@id, id)}
end

#allObject



224
225
# File 'lib/cute_kv/document.rb', line 224

def all
end

#assign(*attributes) ⇒ Object

Assigning attributs needed to persistence

Example:
  class User
    include CuteKV::Document
    assign :name, :country=>"China", :gender=>"male"
  end


142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/cute_kv/document.rb', line 142

def assign(*attributes)
  attrs = attributes.inject({}){ |h,attr|
    if attr.is_a? Hash
      attr.each {|k, v| attr_accessor k; h[k.to_sym] = v }
    else
      attr_accessor attr
      h[attr.to_sym] = nil
    end
    h
  }
  write_assigned_attributes_with_default_values(attrs)
end

#assigned_attributesObject



163
164
165
# File 'lib/cute_kv/document.rb', line 163

def assigned_attributes
  @assigned_attributes_with_default_values.keys
end

#backendObject



167
168
169
# File 'lib/cute_kv/document.rb', line 167

def backend
  @backend
end

#backend_configurationsObject

返回当前对象的数据库和索引配置信息



199
200
201
# File 'lib/cute_kv/document.rb', line 199

def backend_configurations
  @backend.infos
end

#backend_configure(adapter, config) ⇒ Object

Configure CuteKV’s Back-end database,now support LightCloud/TokyoTyrant/TokyoCabinet.

 +adapter+ specify what database to use
   :TC => TokyoCabinet (few to use :TC in our practice projects)
   :TT => TokyoTyrant
   :LC => LightCloud

+config+ is the config infos about back-end
         when +adapter+ is specified to :TT, +config+ coulde be a String or Hash
   String:
     User.backend_configure :TT, "127.0.0.1:1985"
   or Hash:
     User.backend_configure :TT, :host=>'127.0.0.1', :port=>1985

Back-end database,now we support tokyotyrant, lightcloud



193
194
195
# File 'lib/cute_kv/document.rb', line 193

def backend_configure(adapter, config)
  @backend = Connector.new(adapter,config)
end

#clearObject

empty database



173
174
175
# File 'lib/cute_kv/document.rb', line 173

def clear
  @backend.clear
end

#create(attributes = {}) {|object| ... } ⇒ Object

Create one or more objects and save them to database return objects you have created parameters attributes is Hash or hash Array

Example

# create single object
User.create(:first_name => 'Jamie')

# create more objects
User.create([{ :first_name => 'Jamie' }, { :first_name => 'Jeremy' }])

# create an object, and assign values to attributes through block
User.create(:first_name => 'Jamie') do |u|
  u.is_admin = false
end

Yields:

  • (object)


243
244
245
246
247
248
# File 'lib/cute_kv/document.rb', line 243

def create(attributes = {}, &block)
  object = new(attributes)
  yield(object) if block_given?
  object.save
  object
end

#deserialize(raw_value) ⇒ Object



300
301
302
# File 'lib/cute_kv/document.rb', line 300

def deserialize(raw_value)
  @serializer[:desr].call(raw_value)
end

#destroy(object) ⇒ Object

destroy object who is an instance of Actors # and execute all callback and filt actions

Example

aaron = User.create(:name=>"aaron")
User.destroy(aaron)


273
274
275
# File 'lib/cute_kv/document.rb', line 273

def destroy(object)
  object.is_a?(self) ? @backend.delete(object.id) : nil
end

#find(id) ⇒ Object

find object by it’s id

Example

User.find('[email protected]') return an User's instance object
if User's backend has this id or return nil


209
210
211
212
213
214
215
# File 'lib/cute_kv/document.rb', line 209

def find(id)
  return if id.nil?
  raw_value = @backend.get(id)
  return if raw_value.nil?
  value = deserialize raw_value
  active(value)
end

#read_assigned_attributes_with_default_valuesObject



159
160
161
# File 'lib/cute_kv/document.rb', line 159

def read_assigned_attributes_with_default_values
  @assigned_attributes_with_default_values.dup
end

#reload(object) ⇒ Object



260
261
262
263
264
265
266
# File 'lib/cute_kv/document.rb', line 260

def reload(object)
  return unless object.is_a?(self)
  raw_value = @backend.get(object.id)
  return if raw_value.nil?
  value = deserialize raw_value
  update(object, value)
end

#save(object) ⇒ Object



250
251
252
# File 'lib/cute_kv/document.rb', line 250

def save(object)
  object.is_a?(self) ? @backend.put(object.id, object.serialized_attributes) : nil
end

#select_serializer(type = :json) ⇒ Object



288
289
290
# File 'lib/cute_kv/document.rb', line 288

def select_serializer(type=:json)
  @serializer = Serializers[type]
end

#serialize(value) ⇒ Object



296
297
298
# File 'lib/cute_kv/document.rb', line 296

def serialize(value)
  @serializer[:ser].call(value)
end

#serializer_typeObject



292
293
294
# File 'lib/cute_kv/document.rb', line 292

def serializer_type
  @serializer[:type]
end

#update(object, attributes = {}) ⇒ Object



254
255
256
257
258
# File 'lib/cute_kv/document.rb', line 254

def update(object, attributes={})
  object.is_a?(self) ? attributes.each{|key,value|
    object.send("#{key}=",value) if object.respond_to? "#{key}="
  } : nil
end

#write_assigned_attributes_with_default_values(attributes = {}) ⇒ Object



155
156
157
# File 'lib/cute_kv/document.rb', line 155

def write_assigned_attributes_with_default_values(attributes={})
  (@assigned_attributes_with_default_values ||={}).merge!(attributes)
end