Class: CassandraObject::Base

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Naming, ActiveSupport::DescendantsTracker
Includes:
ActiveModel::Conversion, Associations, Attributes, Batches, Callbacks, Configuration, Consistency, Dirty, FinderMethods, Identity, Migrations, Mocking, NestedAttributes, Persistence, RowTTL, Serialization, Timestamps, Validations
Defined in:
lib/cassandra_object/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from NestedAttributes

#_destroy

Methods included from Callbacks

#destroy, #touch

Methods included from Validations

#save, #save!, #valid?

Methods included from Dirty

#save, #save!, #write_attribute

Methods included from Persistence

#destroy, #destroyed?, #new_record?, #persisted?, #reload, #save, #save!, #update_attribute, #update_attributes, #update_attributes!

Methods included from Attributes

#attributes=, #method_missing, #read_attribute, #respond_to?, #write_attribute

Methods included from Identity

#id, #id=

Constructor Details

#initialize(attributes = {}) ⇒ Base

Returns a new instance of Base.



55
56
57
58
59
60
61
62
63
# File 'lib/cassandra_object/base.rb', line 55

def initialize(attributes={})
  @key = attributes.delete(:key)
  @new_record = true
  @destroyed = false
  @readonly = false
  @attributes = {}.with_indifferent_access
  self.attributes = attributes
  @schema_version = self.class.current_schema_version
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class CassandraObject::Attributes

Instance Attribute Details

#keyObject

Returns the value of attribute key.



49
50
51
# File 'lib/cassandra_object/base.rb', line 49

def key
  @key
end

Class Method Details

.base_classObject



18
19
20
21
22
23
24
# File 'lib/cassandra_object/base.rb', line 18

def base_class
  klass = self
  while klass.superclass != Base
    klass = klass.superclass
  end
  klass
end

.column_familyObject



14
15
16
# File 'lib/cassandra_object/base.rb', line 14

def column_family
  @column_family || name.pluralize
end

.column_family=(column_family) ⇒ Object



10
11
12
# File 'lib/cassandra_object/base.rb', line 10

def column_family=(column_family)
  @column_family = column_family
end

Instance Method Details

#==(comparison_object) ⇒ Object



91
92
93
94
95
96
# File 'lib/cassandra_object/base.rb', line 91

def ==(comparison_object)
  comparison_object.equal?(self) ||
    (comparison_object.instance_of?(self.class) &&
      comparison_object.key == key &&
      !comparison_object.new_record?)
end

#[](attr_name) ⇒ Object

Returns the value of the attribute identified by attr_name after it has been typecast (for example, “2004-12-12” in a data column is cast to a date object, like Date.new(2004, 12, 12)). (Alias for the protected read_attribute method).



105
106
107
# File 'lib/cassandra_object/base.rb', line 105

def [](attr_name)
  read_attribute(attr_name)
end

#[]=(attr_name, value) ⇒ Object

Updates the attribute identified by attr_name with the specified value. (Alias for the protected write_attribute method).



111
112
113
# File 'lib/cassandra_object/base.rb', line 111

def []=(attr_name, value)
  write_attribute(attr_name, value)
end

#attributesObject



79
80
81
# File 'lib/cassandra_object/base.rb', line 79

def attributes
  @attributes.merge(:id => id)
end

#eql?(comparison_object) ⇒ Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/cassandra_object/base.rb', line 98

def eql?(comparison_object)
  self == (comparison_object)
end

#hashObject



87
88
89
# File 'lib/cassandra_object/base.rb', line 87

def hash
  id.hash
end

#readonly!Object

Marks this record as read only.



75
76
77
# File 'lib/cassandra_object/base.rb', line 75

def readonly!
  self.readonly = true
end

#readonly=(value) ⇒ Object



70
71
72
# File 'lib/cassandra_object/base.rb', line 70

def readonly=(value)
  @readonly = value
end

#readonly?Boolean

Returns true if the record is read only.

Returns:

  • (Boolean)


66
67
68
# File 'lib/cassandra_object/base.rb', line 66

def readonly?
  @readonly
end

#to_paramObject



83
84
85
# File 'lib/cassandra_object/base.rb', line 83

def to_param
  id.to_s if persisted?
end