Module: Cql::Model::ClassMethods
- Defined in:
- lib/cql/model/class_methods.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#cql_client(new_client = nil) ⇒ Cql::Client
Get or set the client connection used by this class.
- #each(&block) ⇒ Object
- #each_row(&block) ⇒ Object
-
#insert(values) ⇒ Cql::Model::Query::InsertStatement
(also: #create)
Begin building a CQL INSERT statement.
-
#primary_key(*keys) ⇒ Cql::Model
Specify or get a primary key or a composite primary key.
- #property(name, type, opts = {}) ⇒ Object
- #read_consistency(new_consistency = nil) ⇒ Object
- #scope(name, &block) ⇒ Object
-
#select(*params) ⇒ Object
Begin building a CQL SELECT statement.
- #table_name(new_name = nil) ⇒ Object
-
#update(values = {}) ⇒ Cql::Model::Query::UpdateStatement
Start an UPDATE CQL statement The method #keys must be called on the result before #execute.
- #write_consistency(new_consistency = nil) ⇒ Object
Class Method Details
.extended(klass) ⇒ Object
2 3 4 5 6 7 8 9 10 11 12 13 14 |
# File 'lib/cql/model/class_methods.rb', line 2 def self.extended(klass) klass.instance_eval do # The mutex is shared by all Cql::Model inheritors @@cql_model_mutex ||= Mutex.new # Other attributes are tracked per-class @cql_table_name ||= klass.name.split('::').last @cql_model_properties ||= {} @cql_model_keys ||= [] @cql_model_read_consistency ||= 'LOCAL_QUORUM' @cql_model_write_consistency ||= 'LOCAL_QUORUM' end end |
Instance Method Details
#cql_client(new_client = nil) ⇒ Cql::Client
Get or set the client connection used by this class.
20 21 22 23 24 25 26 27 28 |
# File 'lib/cql/model/class_methods.rb', line 20 def cql_client(new_client=nil) if new_client @@cql_model_mutex.synchronize do @cql_client = new_client end end @cql_client || ::Cql::Model.cql_client end |
#each(&block) ⇒ Object
175 176 177 |
# File 'lib/cql/model/class_methods.rb', line 175 def each(&block) Cql::Model::Query::SelectStatement.new(self).each(&block) end |
#each_row(&block) ⇒ Object
170 171 172 |
# File 'lib/cql/model/class_methods.rb', line 170 def each_row(&block) Cql::Model::Query::SelectStatement.new(self).each_row(&block) end |
#insert(values) ⇒ Cql::Model::Query::InsertStatement Also known as: create
Begin building a CQL INSERT statement.
149 150 151 |
# File 'lib/cql/model/class_methods.rb', line 149 def insert(values) Cql::Model::Query::InsertStatement.new(self).insert(values) end |
#primary_key(*keys) ⇒ Cql::Model
Specify or get a primary key or a composite primary key
66 67 68 69 70 71 72 73 74 75 |
# File 'lib/cql/model/class_methods.rb', line 66 def primary_key(*keys) if keys.empty? @cql_model_keys else @@cql_model_mutex.synchronize do @cql_model_keys = keys end self end end |
#property(name, type, opts = {}) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/cql/model/class_methods.rb', line 78 def property(name, type, opts={}) definition = {} # If the user specified the name as a symbol, then they automatically get # a reader and writer because the property has a predictable, fixed column # name. if name.is_a?(Symbol) definition[:reader] = opts[:reader] || name definition[:writer] = opts[:writer] || "#{definition[:reader]}=".to_sym name = name.to_s end @@cql_model_mutex.synchronize do definition[:type] = type if @cql_model_properties.key?(name) && (@cql_model_properties[name] != definition) raise ArgumentError, "Property #{name} is already defined" end unless @cql_model_properties.key?(name) @cql_model_properties[name] = definition __send__(:define_method, definition[:reader]) do self[name] end if definition[:reader] __send__(:define_method, definition[:writer]) do |value| self[name] = value end if definition[:writer] end end self end |
#read_consistency(new_consistency = nil) ⇒ Object
44 45 46 47 48 49 50 |
# File 'lib/cql/model/class_methods.rb', line 44 def read_consistency(new_consistency=nil) if new_consistency @cql_model_read_consistency = new_consistency else @cql_model_read_consistency end end |
#scope(name, &block) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/cql/model/class_methods.rb', line 114 def scope(name, &block) @@cql_model_mutex.synchronize do eigenclass = class <<self self end eigenclass.instance_eval do define_method(name.to_sym) do |*params| # @TODO use a prepared statement for speed self.select.where(*params, &block) end end end self end |
#select(*params) ⇒ Object
Begin building a CQL SELECT statement.
137 138 139 |
# File 'lib/cql/model/class_methods.rb', line 137 def select(*params) Cql::Model::Query::SelectStatement.new(self).select(*params) end |
#table_name(new_name = nil) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/cql/model/class_methods.rb', line 31 def table_name(new_name=nil) if new_name @@cql_model_mutex.synchronize do # Set the table name @cql_table_name = new_name end else # Get the table name @cql_table_name end end |
#update(values = {}) ⇒ Cql::Model::Query::UpdateStatement
Start an UPDATE CQL statement The method #keys must be called on the result before #execute
165 166 167 |
# File 'lib/cql/model/class_methods.rb', line 165 def update(values={}) Cql::Model::Query::UpdateStatement.new(self).update(values) end |
#write_consistency(new_consistency = nil) ⇒ Object
53 54 55 56 57 58 59 |
# File 'lib/cql/model/class_methods.rb', line 53 def write_consistency(new_consistency=nil) if new_consistency @cql_model_write_consistency = new_consistency else @cql_model_write_consistency end end |