Class: CassandraModel::Base
- Extended by:
- Forwardable
- Includes:
- Batches, Callbacks, Persistence
- Defined in:
- lib/cassandra-model/base.rb
Constant Summary collapse
- @@logger =
nil
Instance Attribute Summary collapse
-
#attributes ⇒ Object
Returns the value of attribute attributes.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#key ⇒ Object
Returns the value of attribute key.
-
#key_type ⇒ Object
Returns the value of attribute key_type.
-
#new_record ⇒ Object
Returns the value of attribute new_record.
Class Method Summary collapse
-
.benchmark(title, log_level = Logger::DEBUG, use_silence = true) ⇒ Object
Log and benchmark multiple statements in a single block.
- .column(name, type = :string) ⇒ Object
- .column_family(name = nil) ⇒ Object
- .columns ⇒ Object
- .connection ⇒ Object
- .key(name, type = :string) ⇒ Object
- .key_type ⇒ Object
- .key_type=(value) ⇒ Object
- .logger ⇒ Object
- .logger=(obj) ⇒ Object
-
.primary_key ⇒ Object
Defines the primary key field – can be overridden in subclasses.
-
.silence ⇒ Object
Silences the logger for the duration of the block.
- .validate(&block) ⇒ Object
- .validation ⇒ Object
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
-
#deleted! ⇒ Object
Marks a entity as deleted.
-
#deleted? ⇒ Boolean
When a entity with no attributes is read from the datasource it is auto marked as deleted.
-
#initialize(attrs = {}, convert = true) ⇒ Base
constructor
A new instance of Base.
- #method_missing(symbol, *args, &block) ⇒ Object
- #new_record? ⇒ Boolean
-
#readonly! ⇒ Object
Marks a entity as readonly.
-
#readonly? ⇒ Boolean
Returns
true
if the record is read only. - #valid? ⇒ Boolean
Methods included from Batches
Methods included from Persistence
Methods included from 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
#attributes ⇒ Object
Returns the value of attribute attributes.
131 132 133 |
# File 'lib/cassandra-model/base.rb', line 131 def attributes @attributes end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
131 132 133 |
# File 'lib/cassandra-model/base.rb', line 131 def errors @errors end |
#key ⇒ Object
Returns the value of attribute key.
130 131 132 |
# File 'lib/cassandra-model/base.rb', line 130 def key @key end |
#key_type ⇒ Object
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_record ⇒ Object
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 |
.columns ⇒ Object
64 65 66 |
# File 'lib/cassandra-model/base.rb', line 64 def columns @columns ||= {} end |
.connection ⇒ Object
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_type ⇒ Object
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 |
.logger ⇒ Object
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_key ⇒ Object
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 |
.silence ⇒ Object
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
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 |
.validation ⇒ Object
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.
182 183 184 |
# File 'lib/cassandra-model/base.rb', line 182 def deleted? defined?(@deleted) && @deleted == true end |
#new_record? ⇒ 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.
171 172 173 |
# File 'lib/cassandra-model/base.rb', line 171 def readonly? defined?(@readonly) && @readonly == true end |
#valid? ⇒ 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 |