Class: Chimera::Base

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Callbacks, ActiveModel::Naming
Includes:
ActiveModel::Validations, Associations, Attributes, Finders, GeoIndexes, Indexes, Persistence, RedisObjects
Defined in:
lib/chimera/base.rb

Constant Summary

Constants included from Indexes

Indexes::SEARCH_EXCLUDE_LIST

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Persistence

included

Methods included from Finders

included

Methods included from RedisObjects

included

Methods included from Associations

included

Methods included from GeoIndexes

included

Methods included from Indexes

included

Methods included from Attributes

included

Constructor Details

#initialize(attributes = {}, id = nil, is_new = true) ⇒ Base

Returns a new instance of Base.



77
78
79
80
81
82
83
# File 'lib/chimera/base.rb', line 77

def initialize(attributes={},id=nil,is_new=true)
  @attributes = attributes
  @orig_attributes = @attributes.clone
  @id = id
  @new = is_new
  @sibling_attributes = nil
end

Instance Attribute Details

#associationsObject

Returns the value of attribute associations.



25
26
27
# File 'lib/chimera/base.rb', line 25

def associations
  @associations
end

#attributesObject

Returns the value of attribute attributes.



25
26
27
# File 'lib/chimera/base.rb', line 25

def attributes
  @attributes
end

#idObject

Returns the value of attribute id.



25
26
27
# File 'lib/chimera/base.rb', line 25

def id
  @id
end

#orig_attributesObject

Returns the value of attribute orig_attributes.



25
26
27
# File 'lib/chimera/base.rb', line 25

def orig_attributes
  @orig_attributes
end

#riak_responseObject

Returns the value of attribute riak_response.



25
26
27
# File 'lib/chimera/base.rb', line 25

def riak_response
  @riak_response
end

#sibling_attributesObject

Returns the value of attribute sibling_attributes.



25
26
27
# File 'lib/chimera/base.rb', line 25

def sibling_attributes
  @sibling_attributes
end

Class Method Details

.bucket(keys = false) ⇒ Object



55
56
57
# File 'lib/chimera/base.rb', line 55

def self.bucket(keys=false)
  self.connection(:riak_raw).bucket(self.bucket_key,keys)
end

.bucket_keyObject



51
52
53
# File 'lib/chimera/base.rb', line 51

def self.bucket_key
  self.to_s
end

.configObject



32
33
34
# File 'lib/chimera/base.rb', line 32

def self.config
  @config ||= (Chimera.config[:default] || raise(Chimera::Error::MissingConfig,":default"))
end

.connection(server) ⇒ Object



36
37
38
# File 'lib/chimera/base.rb', line 36

def self.connection(server)
  Thread.current["Chimera::#{self.to_s}::#{server}::connection"] ||= new_connection(server)
end

.new_connection(server) ⇒ Object



40
41
42
43
44
45
46
47
48
49
# File 'lib/chimera/base.rb', line 40

def self.new_connection(server)
  case server.to_sym
  when :redis
    Redis.new(self.config[:redis])
  when :riak_raw
    RiakRaw::Client.new(self.config[:riak_raw][:host], self.config[:riak_raw][:port])
  else
    nil
  end
end

.new_uuidObject



59
60
61
# File 'lib/chimera/base.rb', line 59

def self.new_uuid
  UUIDTools::UUID.random_create.to_s
end

.use_config(key) ⇒ Object



28
29
30
# File 'lib/chimera/base.rb', line 28

def self.use_config(key)
  @config = (Chimera.config[key.to_sym] || raise(Chimera::Error::MissingConfig,":#{key}"))
end

Instance Method Details

#<=>(obj) ⇒ Object



73
74
75
# File 'lib/chimera/base.rb', line 73

def <=>(obj)
  self.id.to_s <=> obj.id.to_s
end

#==(obj) ⇒ Object



67
68
69
70
71
# File 'lib/chimera/base.rb', line 67

def ==(obj)
  obj.class.to_s == self.class.to_s &&
    !obj.new? && !self.new? &&
    obj.id == self.id
end

#inspectObject



63
64
65
# File 'lib/chimera/base.rb', line 63

def inspect
  "#<#{self.to_s}: @id=#{self.id}, @new=#{@new}>"
end