Class: GroongaClientModel::Record
- Inherits:
-
Object
- Object
- GroongaClientModel::Record
- Includes:
- ActiveModel::AttributeAssignment, ActiveModel::Attributes, ActiveModel::Callbacks, ActiveModel::Conversion, ActiveModel::Dirty, ActiveModel::Translation, ActiveModel::Validations, ActiveModel::Validations::Callbacks
- Defined in:
- lib/groonga_client_model/record.rb
Direct Known Subclasses
Class Method Summary collapse
- .all ⇒ Object
- .clear_cache ⇒ Object
- .columns ⇒ Object
- .count ⇒ Object
- .create(attributes = nil, &block) ⇒ Object
- .create!(attributes = nil, &block) ⇒ Object
- .define_attributes ⇒ Object
- .find(id) ⇒ Object
- .first ⇒ Object
- .have_key? ⇒ Boolean
- .i18n_scope ⇒ Object
- .last ⇒ Object
- .schema ⇒ Object
- .select ⇒ Object
- .table_name ⇒ Object
Instance Method Summary collapse
- #assign_dynamic_attribute(name, value) ⇒ Object
- #assign_dynamic_attributes(dynamic_attributes) ⇒ Object
- #delete ⇒ Object
- #destroy ⇒ Object
- #destroyed? ⇒ Boolean
- #id ⇒ Object
-
#initialize(attributes = nil) {|_self| ... } ⇒ Record
constructor
A new instance of Record.
- #new_record? ⇒ Boolean
- #persisted? ⇒ Boolean
- #read_attribute_for_validation(attribute) ⇒ Object
- #save(validate: true) ⇒ Object
- #save!(validate: true) ⇒ Object
- #update(attributes) ⇒ Object
Constructor Details
#initialize(attributes = nil) {|_self| ... } ⇒ Record
Returns a new instance of Record.
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/groonga_client_model/record.rb', line 169 def initialize(attributes=nil) self.class.define_attributes super() assign_attributes(attributes) if attributes if attribute("_id") @new_record = false clear_changes_information else @new_record = true end @destroyed = false yield self if block_given? end |
Class Method Details
.all ⇒ Object
71 72 73 |
# File 'lib/groonga_client_model/record.rb', line 71 def all select.limit(-1) end |
.clear_cache ⇒ Object
37 38 39 |
# File 'lib/groonga_client_model/record.rb', line 37 def clear_cache @@schema = nil end |
.columns ⇒ Object
45 46 47 |
# File 'lib/groonga_client_model/record.rb', line 45 def columns schema.tables[table_name].columns end |
.count ⇒ Object
67 68 69 |
# File 'lib/groonga_client_model/record.rb', line 67 def count select.limit(0).output_columns("_id").response.n_hits end |
.create(attributes = nil, &block) ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/groonga_client_model/record.rb', line 116 def create(attributes=nil, &block) if attributes.is_a?(Array) attributes.collect do |attrs| create(attrs, &block) end else record = new(attributes, &block) record.save record end end |
.create!(attributes = nil, &block) ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/groonga_client_model/record.rb', line 128 def create!(attributes=nil, &block) if attributes.is_a?(Array) attributes.collect do |attrs| create!(attrs, &block) end else record = new(attributes, &block) record.save! record end end |
.define_attributes ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/groonga_client_model/record.rb', line 53 def define_attributes return if defined?(@defined) @defined = true columns.each do |name, column| type = normalize_type((column["value_type"] || {})["name"]) attribute(name, type) if type == :boolean attribute_method_suffix("?") define_attribute_methods(name) end end end |
.find(id) ⇒ Object
75 76 77 78 79 80 81 82 83 84 |
# File 'lib/groonga_client_model/record.rb', line 75 def find(id) if id.respond_to?(:id) id = id.id end record = select.filter("_id == %{id}", id: id).limit(1).first if record.nil? raise RecordNotFound.new("Record not found: _id: <#{id}>") end record end |
.first ⇒ Object
86 87 88 |
# File 'lib/groonga_client_model/record.rb', line 86 def first select.sort_keys("_id").limit(1).first end |
.have_key? ⇒ Boolean
49 50 51 |
# File 'lib/groonga_client_model/record.rb', line 49 def have_key? columns.exist?("_key") end |
.i18n_scope ⇒ Object
29 30 31 |
# File 'lib/groonga_client_model/record.rb', line 29 def i18n_scope :groonga_client_model end |
.last ⇒ Object
90 91 92 |
# File 'lib/groonga_client_model/record.rb', line 90 def last select.sort_keys("-_id").limit(1).first end |
.schema ⇒ Object
33 34 35 |
# File 'lib/groonga_client_model/record.rb', line 33 def schema @@schema ||= Schema.new end |
.select ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/groonga_client_model/record.rb', line 94 def select full_text_searchable_column_names = [] columns.each do |name, column| if column.have_full_text_search_index? full_text_searchable_column_names << name end end model_class = self model_class_module = Module.new do define_method :model_class do model_class end end extensions = [ ClientOpener, Modelizable, model_class_module, ] Groonga::Client::Request::Select.new(table_name, extensions). match_columns(full_text_searchable_column_names) end |
.table_name ⇒ Object
41 42 43 |
# File 'lib/groonga_client_model/record.rb', line 41 def table_name name.to_s.demodulize.underscore.pluralize end |
Instance Method Details
#assign_dynamic_attribute(name, value) ⇒ Object
244 245 246 247 248 249 250 251 |
# File 'lib/groonga_client_model/record.rb', line 244 def assign_dynamic_attribute(name, value) if respond_to?(name) singleton_class.__send__(:undef_method, name) end singleton_class.__send__(:define_method, name) do value end end |
#assign_dynamic_attributes(dynamic_attributes) ⇒ Object
236 237 238 239 240 241 242 |
# File 'lib/groonga_client_model/record.rb', line 236 def assign_dynamic_attributes(dynamic_attributes) return if dynamic_attributes.blank? dynamic_attributes.each do |name, value| assign_dynamic_attribute(name, value) end end |
#delete ⇒ Object
203 204 205 |
# File 'lib/groonga_client_model/record.rb', line 203 def delete destroy_raw end |
#destroy ⇒ Object
207 208 209 210 211 |
# File 'lib/groonga_client_model/record.rb', line 207 def destroy run_callbacks(:destroy) do destroy_raw end end |
#destroyed? ⇒ Boolean
226 227 228 |
# File 'lib/groonga_client_model/record.rb', line 226 def destroyed? @destroyed end |
#id ⇒ Object
218 219 220 |
# File 'lib/groonga_client_model/record.rb', line 218 def id _id end |
#new_record? ⇒ Boolean
222 223 224 |
# File 'lib/groonga_client_model/record.rb', line 222 def new_record? @new_record end |
#persisted? ⇒ Boolean
230 231 232 233 234 |
# File 'lib/groonga_client_model/record.rb', line 230 def persisted? return false if @new_record return false if @destroyed true end |
#read_attribute_for_validation(attribute) ⇒ Object
186 187 188 |
# File 'lib/groonga_client_model/record.rb', line 186 def read_attribute_for_validation(attribute) @attributes[attribute.to_s].value_before_type_cast end |
#save(validate: true) ⇒ Object
190 191 192 193 194 |
# File 'lib/groonga_client_model/record.rb', line 190 def save(validate: true) run_callbacks(:save) do save_raw(validate: validate) end end |
#save!(validate: true) ⇒ Object
196 197 198 199 200 201 |
# File 'lib/groonga_client_model/record.rb', line 196 def save!(validate: true) unless save(validate: validate) = "Failed to save the record" raise RecordNotSaved.new(, self) end end |
#update(attributes) ⇒ Object
213 214 215 216 |
# File 'lib/groonga_client_model/record.rb', line 213 def update(attributes) assign_attributes(attributes) save end |