Class: CassandraModel::Base

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

Constant Summary collapse

@@logger =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Batches

included

Methods included from Persistence

included

Methods included from Callbacks

included, #run_callbacks

Constructor Details

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

Returns a new instance of Base.



133
134
135
136
137
138
139
140
141
142
# File 'lib/cassandra-model/base.rb', line 133

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symbol, *args, &block) ⇒ Object



148
149
150
151
152
153
154
# File 'lib/cassandra-model/base.rb', line 148

def method_missing(symbol, *args, &block)
  if symbol[-1] == '='
    @attributes[symbol[0..-2]] = args[0]
  else
    @attributes[symbol.to_s]
  end
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



131
132
133
# File 'lib/cassandra-model/base.rb', line 131

def attributes
  @attributes
end

#errorsObject (readonly)

Returns the value of attribute errors.



131
132
133
# File 'lib/cassandra-model/base.rb', line 131

def errors
  @errors
end

#keyObject

Returns the value of attribute key.



130
131
132
# File 'lib/cassandra-model/base.rb', line 130

def key
  @key
end

#key_typeObject

Returns the value of attribute key_type.



130
131
132
# File 'lib/cassandra-model/base.rb', line 130

def key_type
  @key_type
end

#new_recordObject

Returns the value of attribute new_record.



130
131
132
# File 'lib/cassandra-model/base.rb', line 130

def new_record
  @new_record
end

Class Method Details

.benchmark(title, log_level = Logger::DEBUG, use_silence = true) ⇒ Object

Log and benchmark multiple statements in a single block. Example:

Project.benchmark("Creating project") do
  project = Project.create("name" => "stuff")
  project.create_manager("name" => "David")
  project.milestones << Milestone.find(:all)
end

The benchmark is only recorded if the current level of the logger is less than or equal to the log_level, which makes it easy to include benchmarking statements in production software that will remain inexpensive because the benchmark will only be conducted if the log level is low enough.

The logging of the multiple statements is turned off unless use_silence is set to false.



103
104
105
106
107
108
109
110
111
112
# File 'lib/cassandra-model/base.rb', line 103

def benchmark(title, log_level = Logger::DEBUG, use_silence = true)
  if logger && logger.level <= log_level
    result = nil
    ms = 1000 * Benchmark.realtime { result = use_silence ? silence { yield } : yield }
    logger.add(log_level, '%s (%.1fms)' % [title, ms])
    result
  else
    yield
  end
end

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



46
47
48
49
50
51
52
53
# File 'lib/cassandra-model/base.rb', line 46

def column(name, type = :string)
  columns[name] = type
  class_eval "def #{name}; #{type.capitalize}Type.load(@attributes['#{name}']); end"
  class_eval "def #{name}=(value); @attributes['#{name}'] = #{type.capitalize}Type.dump(value); end"
  if type.downcase == 'timeuuid' or type.downcase == 'time_uuid'
    class_eval "def #{name}_str; (@attributes['#{name}'].nil? or @attributes['#{name}'].size == 0) ? nil : #{type.capitalize}Type.load(@attributes['#{name}']).to_guid; end"
  end
end

.column_family(name = nil) ⇒ Object



21
22
23
# File 'lib/cassandra-model/base.rb', line 21

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

.columnsObject



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

def columns
  @columns ||= {}
end

.connectionObject



17
18
19
# File 'lib/cassandra-model/base.rb', line 17

def connection
  Connection.connection
end

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



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/cassandra-model/base.rb', line 25

def key(name, type = :string)
  @key_type = type.to_s
  class_eval <<-EVAL, __FILE__, __LINE__ + 1
    def #{name}=(value)
      @key_type = '#{type}'
      if @key_type.downcase == 'timeuuid' or @key_type.downcase == 'time_uuid'
        value = SimpleUUID::UUID.new(value)
      end
      @key = value.bytes;
    end
    def #{name}
      @key;
    end
    if @key_type.downcase == 'timeuuid' or @key_type.downcase == 'time_uuid'
      def #{name}_str
        (@key.nil? or @key.size == 0) ? nil : SimpleUUID::UUID.new(@key).to_guid
      end
    end
  EVAL
end

.key_typeObject



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

def key_type
  @key_type
end

.key_type=(value) ⇒ Object



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

def key_type=(value)
  @key_type = value
end

.loggerObject



68
69
70
# File 'lib/cassandra-model/base.rb', line 68

def logger
  return @@logger || RAILS_DEFAULT_LOGGER
end

.logger=(obj) ⇒ Object



72
73
74
# File 'lib/cassandra-model/base.rb', line 72

def logger=(obj)
  @@logger = obj
end

.primary_keyObject

Defines the primary key field – can be overridden in subclasses. Overwriting will negate any effect of the primary_key_prefix_type setting, though.



78
79
80
# File 'lib/cassandra-model/base.rb', line 78

def primary_key
  'id'
end

.silenceObject

Silences the logger for the duration of the block.



115
116
117
118
119
120
# File 'lib/cassandra-model/base.rb', line 115

def silence
  old_logger_level, logger.level = logger.level, Logger::ERROR if logger
  yield
ensure
  logger.level = old_logger_level if logger
end

.validate(&block) ⇒ Object

Raises:

  • (ArgumentError)


55
56
57
58
# File 'lib/cassandra-model/base.rb', line 55

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

.validationObject



60
61
62
# File 'lib/cassandra-model/base.rb', line 60

def validation
  @validation
end

Instance Method Details

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



166
167
168
# File 'lib/cassandra-model/base.rb', line 166

def ==(other)
  true
end

#deleted!Object

Marks a entity as deleted



187
188
189
# File 'lib/cassandra-model/base.rb', line 187

def deleted!
  @deleted = true
end

#deleted?Boolean

When a entity with no attributes is read from the datasource it is auto marked as deleted.

Returns:

  • (Boolean)


182
183
184
# File 'lib/cassandra-model/base.rb', line 182

def deleted?
  defined?(@deleted) && @deleted == true
end

#new_record?Boolean

Returns:

  • (Boolean)


162
163
164
# File 'lib/cassandra-model/base.rb', line 162

def new_record?
  @new_record
end

#readonly!Object

Marks a entity as readonly



176
177
178
# File 'lib/cassandra-model/base.rb', line 176

def readonly!
  @readonly = true
end

#readonly?Boolean

Returns true if the record is read only.

Returns:

  • (Boolean)


171
172
173
# File 'lib/cassandra-model/base.rb', line 171

def readonly?
  defined?(@readonly) && @readonly == true
end

#valid?Boolean

Returns:

  • (Boolean)


156
157
158
159
160
# File 'lib/cassandra-model/base.rb', line 156

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