Module: Zeng::Document::ClassMethods

Defined in:
lib/zeng/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



214
215
216
217
# File 'lib/zeng/document.rb', line 214

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

#allObject



219
220
# File 'lib/zeng/document.rb', line 219

def all
end

#assign(*attributes) ⇒ Object

Assigning attributs needed to persistence

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


137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/zeng/document.rb', line 137

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



158
159
160
# File 'lib/zeng/document.rb', line 158

def assigned_attributes
  @assigned_attributes_with_default_values.keys
end

#backendObject



162
163
164
# File 'lib/zeng/document.rb', line 162

def backend
  @backend
end

#backend_configurationsObject

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



194
195
196
# File 'lib/zeng/document.rb', line 194

def backend_configurations
  @backend.infos
end

#backend_configure(adapter, config) ⇒ Object

Configure Zeng’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



188
189
190
# File 'lib/zeng/document.rb', line 188

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

#clearObject

empty database



168
169
170
# File 'lib/zeng/document.rb', line 168

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)


238
239
240
241
242
243
# File 'lib/zeng/document.rb', line 238

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

#deserialize(raw_value) ⇒ Object



295
296
297
# File 'lib/zeng/document.rb', line 295

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)


268
269
270
# File 'lib/zeng/document.rb', line 268

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


204
205
206
207
208
209
210
# File 'lib/zeng/document.rb', line 204

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



154
155
156
# File 'lib/zeng/document.rb', line 154

def read_assigned_attributes_with_default_values
  @assigned_attributes_with_default_values.dup
end

#reload(object) ⇒ Object



255
256
257
258
259
260
261
# File 'lib/zeng/document.rb', line 255

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



245
246
247
# File 'lib/zeng/document.rb', line 245

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

#select_serializer(type = :json) ⇒ Object



283
284
285
# File 'lib/zeng/document.rb', line 283

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

#serialize(value) ⇒ Object



291
292
293
# File 'lib/zeng/document.rb', line 291

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

#serializer_typeObject



287
288
289
# File 'lib/zeng/document.rb', line 287

def serializer_type
  @serializer[:type]
end

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



249
250
251
252
253
# File 'lib/zeng/document.rb', line 249

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



150
151
152
# File 'lib/zeng/document.rb', line 150

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