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.



40
41
42
# File 'lib/groovy/model.rb', line 40

def context_name
  @context_name
end

#table_nameObject

Returns the value of attribute table_name.



40
41
42
# File 'lib/groovy/model.rb', line 40

def table_name
  @table_name
end

Instance Method Details

#add_accessors_for(col, s = schema) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/groovy/model.rb', line 89

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
    puts "WARNING: Unknown column type: #{col}"
  end
end

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



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

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

#allObject



186
187
188
# File 'lib/groovy/model.rb', line 186

def all
  query
end

#attribute_namesObject



51
52
53
# File 'lib/groovy/model.rb', line 51

def attribute_names
  @attribute_names ||= schema.attribute_columns
end

#callbacksObject



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

def callbacks
  @callbacks ||= {}
end

#create(attributes, key = nil) ⇒ Object



126
127
128
129
# File 'lib/groovy/model.rb', line 126

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

#create!(attributes, key = nil) ⇒ Object



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

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

#delete_allObject



139
140
141
142
# File 'lib/groovy/model.rb', line 139

def delete_all
  # find_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



120
121
122
123
124
# File 'lib/groovy/model.rb', line 120

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

#first(num = 1) ⇒ Object



190
191
192
193
# File 'lib/groovy/model.rb', line 190

def first(num = 1)
  arr = limit(num)
  num == 1 ? arr.first : arr
end

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



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

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



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/groovy/model.rb', line 220

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

#last(num = 1) ⇒ Object



195
196
197
198
# File 'lib/groovy/model.rb', line 195

def last(num = 1)
  arr = all.sort_by(_id: :desc).limit(num)
  num == 1 ? arr.first : arr
end

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



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/groovy/model.rb', line 67

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, {})
    yield s if block
    s.sync

    extend(PatriciaTrieMethods) 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



102
103
104
105
# File 'lib/groovy/model.rb', line 102

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

#queryObject



200
201
202
# File 'lib/groovy/model.rb', line 200

def query
  query_class.new(self, table)
end

#render_results(results, &block) ⇒ Object



169
170
171
172
173
174
175
# File 'lib/groovy/model.rb', line 169

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



63
64
65
# File 'lib/groovy/model.rb', line 63

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

#scope(name, obj) ⇒ Object



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

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

#set_timestamp(obj, key_name) ⇒ Object



239
240
241
# File 'lib/groovy/model.rb', line 239

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

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



159
160
161
162
163
164
165
166
167
# File 'lib/groovy/model.rb', line 159

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



46
47
48
49
# File 'lib/groovy/model.rb', line 46

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

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



177
178
179
180
181
182
183
184
# File 'lib/groovy/model.rb', line 177

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



135
136
137
# File 'lib/groovy/model.rb', line 135

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

#validatable!Object



42
43
44
# File 'lib/groovy/model.rb', line 42

def validatable!
  include(ActiveModel::Validations)
end