Module: Groovy::Model::ClassMethods

Extended by:
Forwardable
Defined in:
lib/groovy/model.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#context_nameObject

Returns the value of attribute context_name.



77
78
79
# File 'lib/groovy/model.rb', line 77

def context_name
  @context_name
end

#table_nameObject

Returns the value of attribute table_name.



77
78
79
# File 'lib/groovy/model.rb', line 77

def table_name
  @table_name
end

Instance Method Details

#add_accessors_for(col, s = schema) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
# File 'lib/groovy/model.rb', line 142

def add_accessors_for(col, s = schema)
  if s.attribute_columns.include?(col)
    add_attr_accessors(col)
  elsif s.singular_references.include?(col)
    add_ref_accessors(col)
  elsif s.plural_references.include?(col)
    add_vector_accessors(col)
  else
    raise "Unknown column type: #{col}"
  end
end

#add_column(name, type, options = {}) ⇒ Object



129
130
131
132
133
134
# File 'lib/groovy/model.rb', line 129

def add_column(name, type, options = {})
  @attribute_names = nil # ensure cache is cleared
  schema.column(name, type, options)
  schema.sync
  add_accessors_for(name)
end

#add_reference(name, table_name, options = {}) ⇒ Object



136
137
138
139
140
# File 'lib/groovy/model.rb', line 136

def add_reference(name, table_name, options = {})
  schema.reference(name, table_name, options)
  schema.sync
  add_accessors_for(name)
end

#allObject



244
245
246
# File 'lib/groovy/model.rb', line 244

def all
  query
end

#attribute_namesObject



92
93
94
# File 'lib/groovy/model.rb', line 92

def attribute_names
  schema.attribute_columns
end

#callbacksObject



304
305
306
# File 'lib/groovy/model.rb', line 304

def callbacks
  @callbacks ||= {}
end

#create(attributes) ⇒ Object



179
180
181
182
# File 'lib/groovy/model.rb', line 179

def create(attributes)
  obj = new(attributes)
  obj.save ? obj : false
end

#create!(attributes) ⇒ Object



184
185
186
# File 'lib/groovy/model.rb', line 184

def create!(attributes)
  create(attributes) or raise Error, "Invalid"
end

#delete_allObject



192
193
194
195
# File 'lib/groovy/model.rb', line 192

def delete_all
  # all.each { |child| child.delete }
  table.delete { |record| record._id > -1 }
end

#find(id) ⇒ Object

def find_records(&block)

records = table.select(&block)
records.map do |r|
  find_and_init_record(r.attributes['_key']['_id'])
end

end



173
174
175
176
177
# File 'lib/groovy/model.rb', line 173

def find(id)
  if record = table[id.to_i] and record.record_id
    new_from_record(record)
  end
end

#find_or_create_by(attributes) ⇒ Object



268
269
270
# File 'lib/groovy/model.rb', line 268

def find_or_create_by(attributes)
  find_by(**attributes) || create(**attributes)
end

#index_search(column, query, options = {}, &block) ⇒ Object



212
213
214
215
# File 'lib/groovy/model.rb', line 212

def index_search(column, query, options = {}, &block)
  results = table.select { |rec| rec[column].match(query) }
  render_results(results, &block)
end

#insert(attributes, key = nil) ⇒ Object

called from instance too, so must by public



281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/groovy/model.rb', line 281

def insert(attributes, key = nil)
  set_timestamp(attributes, :created_at)
  set_timestamp(attributes, :updated_at)

  # remove nil attributes for integer columns, otherwise
  # we get a TypeError (no implicit conversion from nil to integer)
  attributes.each do |k, v|
    attributes.delete(k) if v.nil? # && schema.integer_columns.include?(k)
  end

  if table.support_key?
    raise "Key required" if key.nil?
    table.add(key, attributes)
  else
    raise "Key present, but unsupported" if key
    table.add(attributes)
  end
end

#load_schema(options = {}, &block) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/groovy/model.rb', line 112

def load_schema(options = {}, &block)
  @schema = begin
    self.context_name = options[:context] || Groovy.first_context_name
    self.table_name = options[:table_name] if options[:table_name]

    s = Schema.new(db_context, table_name, options)
    yield s if block
    s.sync

    include(HashTable) if table.is_a?(Groonga::Hash)
    include(PatriciaTrieTable) if table.is_a?(Groonga::PatriciaTrie)

    s.column_names.each { |col| add_accessors_for(col, s) }
    s
  end
end

#new_from_record(record) ⇒ Object

called from Query, so needs to be public



155
156
157
158
# File 'lib/groovy/model.rb', line 155

def new_from_record(record)
  # new(record.attributes, record)
  new(nil, record)
end

#queryObject



248
249
250
# File 'lib/groovy/model.rb', line 248

def query
  query_class.new(self, table)
end

#reference_namesObject



96
97
98
# File 'lib/groovy/model.rb', line 96

def reference_names
  schema.singular_references + schema.plural_references
end

#render_results(results, &block) ⇒ Object



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

def render_results(results, &block)
  if block_given?
    results.each { |rec| yield new_from_record(rec) }
  else
    results.map { |rec| new_from_record(rec) }
  end
end

#schema(options = {}, &block) ⇒ Object

def plural_refs

schema.plural_references

end



108
109
110
# File 'lib/groovy/model.rb', line 108

def schema(options = {}, &block)
  @schema ||= load_schema(options, &block)
end

#scope(name, obj) ⇒ Object



256
257
258
259
260
261
262
# File 'lib/groovy/model.rb', line 256

def scope(name, obj)
  scopes.push(name)
  query_class.add_scope(name, obj)
  define_singleton_method(name) do |*args|
    query.public_send(name, *args)
  end
end

#scopesObject



252
253
254
# File 'lib/groovy/model.rb', line 252

def scopes
  @scopes ||= []
end

#search_tableObject

def column(name)

Groonga["#{table_name}.#{name}"] # .search, .similar_search, etc

end



206
207
208
# File 'lib/groovy/model.rb', line 206

def search_table
  schema.search_table
end

#set_timestamp(obj, key_name) ⇒ Object



300
301
302
# File 'lib/groovy/model.rb', line 300

def set_timestamp(obj, key_name)
  obj[key_name] = Time.now.utc if attribute_names.include?(key_name.to_sym)
end

#similar_search(column, query, options = {}, &block) ⇒ Object



217
218
219
220
221
222
223
224
225
# File 'lib/groovy/model.rb', line 217

def similar_search(column, query, options = {}, &block)
  unless schema.index_columns.include?(column.to_sym)
    raise Error, "Column '#{column}' doesn't have an index set!"
  end

  # results = table.select("#{col}:#{q}", operator: Groonga::Operation::SIMILAR)
  results = table.select { |rec| rec[column].similar_search(query) }
  render_results(results, &block)
end

#tableObject



83
84
85
86
# File 'lib/groovy/model.rb', line 83

def table
  # raise "Table name not set: #{table_name}" if table_name.nil?
  db_context[table_name]
end

#underscore_nameObject



88
89
90
# File 'lib/groovy/model.rb', line 88

def underscore_name
  Utils.underscore(name)
end

#unique_values_for(column, limit: -1,, cache: false) ⇒ Object



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

def unique_values_for(column, limit: -1, cache: false)
  cols = [column]
  opts = { drill_down: cols, drill_down_limit: limit, drilldown_sortby: '_key' }
  opts[:cache] = cache ? 'yes' : 'no'
  resp = table.context.select(table_name, opts) # drill_down_output_columns: cols
  arr  = resp.body[1] # or resp.drilldowns
  arr.slice(2..-1) # .flatten
end

#update_all(attrs) ⇒ Object



188
189
190
# File 'lib/groovy/model.rb', line 188

def update_all(attrs)
  all.each { |child| child.update_attributes(attrs) }
end

#validatable!Object



79
80
81
# File 'lib/groovy/model.rb', line 79

def validatable!
  include(ActiveModel::Validations)
end

#where(*args, &block) ⇒ Object



264
265
266
# File 'lib/groovy/model.rb', line 264

def where(*args, &block)
  query.where(*args, &block)
end