Class: GroongaClientModel::Record

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::AttributeMethods, ActiveModel::Callbacks, ActiveModel::Conversion, ActiveModel::Dirty, ActiveModel::Translation, ActiveModel::Validations, AttributeAssignment
Defined in:
lib/groonga_client_model/record.rb

Direct Known Subclasses

ApplicationGroongaRecord

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from AttributeAssignment

#assign_attributes

Constructor Details

#initialize(attributes = nil) ⇒ Record

Returns a new instance of Record.



171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/groonga_client_model/record.rb', line 171

def initialize(attributes=nil)
  @attributes = {}
  self.class.define_attributes
  assign_attributes(attributes) if attributes

  if @attributes["_id"]
    @new_record = false
    clear_changes_information
  else
    @new_record = true
  end
  @destroyed = false
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



161
162
163
# File 'lib/groonga_client_model/record.rb', line 161

def attributes
  @attributes
end

Class Method Details

.allObject



76
77
78
# File 'lib/groonga_client_model/record.rb', line 76

def all
  select.limit(-1)
end

.clear_cacheObject



36
37
38
# File 'lib/groonga_client_model/record.rb', line 36

def clear_cache
  @@schema = nil
end

.columnsObject



44
45
46
# File 'lib/groonga_client_model/record.rb', line 44

def columns
  schema.tables[table_name].columns
end

.countObject



72
73
74
# File 'lib/groonga_client_model/record.rb', line 72

def count
  select.limit(0).output_columns("_id").response.n_hits
end

.create(attributes = nil) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
# File 'lib/groonga_client_model/record.rb', line 121

def create(attributes=nil)
  if attributes.is_a?(Array)
    attributes.collect do |attrs|
      create(attrs)
    end
  else
    record = new(attributes)
    record.save
    record
  end
end

.define_attributesObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/groonga_client_model/record.rb', line 52

def define_attributes
  return if defined?(@defined)
  @defined = true

  boolean_column_names = []
  non_boolean_column_names = []
  columns.each do |name, column|
    if (column["value_type"] || {})["name"] == "Bool"
      boolean_column_names << name
    else
      non_boolean_column_names << name
    end
  end

  attribute_method_suffix("=")
  define_attribute_methods(*non_boolean_column_names)
  attribute_method_suffix("?")
  define_attribute_methods(*boolean_column_names)
end

.find(id) ⇒ Object



80
81
82
83
84
85
86
87
88
89
# File 'lib/groonga_client_model/record.rb', line 80

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

.firstObject



91
92
93
# File 'lib/groonga_client_model/record.rb', line 91

def first
  select.sort_keys("_id").limit(1).first
end

.have_key?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/groonga_client_model/record.rb', line 48

def have_key?
  columns.exist?("_key")
end

.i18n_scopeObject



28
29
30
# File 'lib/groonga_client_model/record.rb', line 28

def i18n_scope
  :groonga_client_model
end

.lastObject



95
96
97
# File 'lib/groonga_client_model/record.rb', line 95

def last
  select.sort_keys("-_id").limit(1).first
end

.schemaObject



32
33
34
# File 'lib/groonga_client_model/record.rb', line 32

def schema
  @@schema ||= Schema.new
end

.selectObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/groonga_client_model/record.rb', line 99

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_nameObject



40
41
42
# File 'lib/groonga_client_model/record.rb', line 40

def table_name
  name.to_s.demodulize.underscore.pluralize
end

Instance Method Details

#assign_dynamic_attribute(name, value) ⇒ Object



235
236
237
238
239
240
241
242
# File 'lib/groonga_client_model/record.rb', line 235

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



227
228
229
230
231
232
233
# File 'lib/groonga_client_model/record.rb', line 227

def assign_dynamic_attributes(dynamic_attributes)
  return if dynamic_attributes.blank?

  dynamic_attributes.each do |name, value|
    assign_dynamic_attribute(name, value)
  end
end

#destroyObject



198
199
200
201
202
# File 'lib/groonga_client_model/record.rb', line 198

def destroy
  run_callbacks(:destroy) do
    destroy_raw
  end
end

#destroyed?Boolean

Returns:

  • (Boolean)


217
218
219
# File 'lib/groonga_client_model/record.rb', line 217

def destroyed?
  @destroyed
end

#idObject



209
210
211
# File 'lib/groonga_client_model/record.rb', line 209

def id
  _id
end

#new_record?Boolean

Returns:

  • (Boolean)


213
214
215
# File 'lib/groonga_client_model/record.rb', line 213

def new_record?
  @new_record
end

#persisted?Boolean

Returns:

  • (Boolean)


221
222
223
224
225
# File 'lib/groonga_client_model/record.rb', line 221

def persisted?
  return false if @new_record
  return false if @destroyed
  true
end

#save(validate: true) ⇒ Object



185
186
187
188
189
# File 'lib/groonga_client_model/record.rb', line 185

def save(validate: true)
  run_callbacks(:save) do
    save_raw(validate: validate)
  end
end

#save!(validate: true) ⇒ Object



191
192
193
194
195
196
# File 'lib/groonga_client_model/record.rb', line 191

def save!(validate: true)
  unless save(validate: validate)
    message = "Failed to save the record"
    raise RecordNotSaved.new(message, self)
  end
end

#update(attributes) ⇒ Object



204
205
206
207
# File 'lib/groonga_client_model/record.rb', line 204

def update(attributes)
  assign_attributes(attributes)
  save
end