Class: CassandraModel::Base

Inherits:
Object show all
Extended by:
Forwardable
Includes:
Callbacks, Persistence
Defined in:
lib/cassandra-model/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Persistence

included

Methods included from Callbacks

included

Constructor Details

#initialize(attrs = {}, convert = true) ⇒ Base

Returns a new instance of Base.



65
66
67
68
69
70
71
72
73
74
# File 'lib/cassandra-model/base.rb', line 65

def initialize(attrs = {}, convert = true)
  @new_record = true
  @errors     = []
  @attributes = {}
  if convert
    self.attributes = attrs
  else
    @attributes     = attrs
  end
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



63
64
65
# File 'lib/cassandra-model/base.rb', line 63

def attributes
  @attributes
end

#errorsObject

Returns the value of attribute errors.



62
63
64
# File 'lib/cassandra-model/base.rb', line 62

def errors
  @errors
end

#keyObject

Returns the value of attribute key.



62
63
64
# File 'lib/cassandra-model/base.rb', line 62

def key
  @key
end

#new_recordObject

Returns the value of attribute new_record.



62
63
64
# File 'lib/cassandra-model/base.rb', line 62

def new_record
  @new_record
end

Class Method Details

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



30
31
32
33
34
35
36
37
38
39
# File 'lib/cassandra-model/base.rb', line 30

def column(name, type = :string)
  columns[name] = type
  class_eval "def #{name}; #{type.capitalize}Type.load(@attributes['#{name}']); end"

  if type == :string || type == :integer || type == :float
    class_eval "def #{name}=(value); @attributes['#{name}'] = value.to_s; end"
  else
    class_eval "def #{name}=(value); @attributes['#{name}'] = #{type.capitalize}Type.dump(value); end"
  end
end

.column_family(name = nil) ⇒ Object



19
20
21
# File 'lib/cassandra-model/base.rb', line 19

def column_family(name = nil)
  @column_family || (@column_family = name || self.name.split('::').last)
end

.columnsObject



50
51
52
# File 'lib/cassandra-model/base.rb', line 50

def columns
  @columns ||= {}
end

.connectionObject



15
16
17
# File 'lib/cassandra-model/base.rb', line 15

def connection
  @connection || (superclass.connection if superclass)
end

.establish_connection(*args) ⇒ Object



11
12
13
# File 'lib/cassandra-model/base.rb', line 11

def establish_connection(*args)
  @connection = Cassandra.new(*args)
end

.key(name) ⇒ Object



23
24
25
26
27
28
# File 'lib/cassandra-model/base.rb', line 23

def key(name)
  class_eval "    def \#{name}=(value); @key = value.to_s; end\n    def \#{name}; @key; end\n  EVAL\nend\n", __FILE__, __LINE__ + 1

.validate(&block) ⇒ Object

Raises:

  • (ArgumentError)


41
42
43
44
# File 'lib/cassandra-model/base.rb', line 41

def validate(&block)
  raise ArgumentError.new('provide a block that does validation') unless block_given?
  @validation = block
end

.validationObject



46
47
48
# File 'lib/cassandra-model/base.rb', line 46

def validation
  @validation
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



90
91
92
93
94
95
96
# File 'lib/cassandra-model/base.rb', line 90

def ==(other)
  if other.respond_to? :key
    self.key == other.key
  else
    false
  end
end

#new_record?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/cassandra-model/base.rb', line 86

def new_record?
  @new_record
end

#valid?Boolean

Returns:

  • (Boolean)


80
81
82
83
84
# File 'lib/cassandra-model/base.rb', line 80

def valid?
  @errors << "key required" if key.to_s !~ /\S/
  self.instance_eval(&self.class.validation) unless self.class.validation.nil?
  @errors.empty?
end