Class: CassandraModel::Base
Instance Attribute Summary collapse
-
#attributes ⇒ Object
Returns the value of attribute attributes.
-
#errors ⇒ Object
Returns the value of attribute errors.
-
#key ⇒ Object
Returns the value of attribute key.
-
#new_record ⇒ Object
Returns the value of attribute new_record.
Class Method Summary collapse
- .column(name, type = :string) ⇒ Object
- .column_family(name = nil) ⇒ Object
- .columns ⇒ Object
- .connection ⇒ Object
- .establish_connection(*args) ⇒ Object
- .key(name) ⇒ Object
- .validate(&block) ⇒ Object
- .validation ⇒ Object
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
-
#initialize(attrs = {}, convert = true) ⇒ Base
constructor
A new instance of Base.
- #new_record? ⇒ Boolean
- #valid? ⇒ Boolean
Methods included from Persistence
Methods included from Callbacks
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
#attributes ⇒ Object
Returns the value of attribute attributes.
63 64 65 |
# File 'lib/cassandra-model/base.rb', line 63 def attributes @attributes end |
#errors ⇒ Object
Returns the value of attribute errors.
62 63 64 |
# File 'lib/cassandra-model/base.rb', line 62 def errors @errors end |
#key ⇒ Object
Returns the value of attribute key.
62 63 64 |
# File 'lib/cassandra-model/base.rb', line 62 def key @key end |
#new_record ⇒ Object
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 |
.columns ⇒ Object
50 51 52 |
# File 'lib/cassandra-model/base.rb', line 50 def columns @columns ||= {} end |
.connection ⇒ Object
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
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 |
.validation ⇒ Object
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
86 87 88 |
# File 'lib/cassandra-model/base.rb', line 86 def new_record? @new_record end |
#valid? ⇒ 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 |