Module: Groovy::Model
- Defined in:
- lib/groovy/model.rb
Defined Under Namespace
Modules: ClassMethods, PatriciaTrieMethods
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Instance Attribute Details
#changes ⇒ Object
Returns the value of attribute changes.
305
306
307
|
# File 'lib/groovy/model.rb', line 305
def changes
@changes
end
|
#id ⇒ Object
Returns the value of attribute id.
305
306
307
|
# File 'lib/groovy/model.rb', line 305
def id
@id
end
|
#record ⇒ Object
Returns the value of attribute record.
305
306
307
|
# File 'lib/groovy/model.rb', line 305
def record
@record
end
|
Class Method Details
.get_class(table_name) ⇒ Object
38
39
40
41
|
# File 'lib/groovy/model.rb', line 38
def self.get_class(table_name)
classified = table_name.gsub(/([A-Z])/, '_\1').split('_').collect! { |w| w.capitalize }.join
Kernel.const_get(classified)
end
|
.included(base) ⇒ Object
32
33
34
35
36
|
# File 'lib/groovy/model.rb', line 32
def self.included(base)
base.extend(ClassMethods)
base.include(Forwardable)
base.table_name = base.name.sub(/y$/, 'ie') + 's'
end
|
.initialize_from_record(obj) ⇒ Object
18
19
20
21
|
# File 'lib/groovy/model.rb', line 18
def self.initialize_from_record(obj)
model = model_from_table(obj.table.name)
model.new_from_record(obj)
end
|
.model_from_table(table_name) ⇒ Object
def self.initialize_from_hash(key, obj)
model = model_from_table(key)
model.find(obj['_id'])
end
28
29
30
|
# File 'lib/groovy/model.rb', line 28
def self.model_from_table(table_name)
get_class(table_name.to_s.sub(/ies$/, 'y').sub(/s$/, ''))
end
|
Instance Method Details
#<=>(other) ⇒ Object
436
437
438
|
# File 'lib/groovy/model.rb', line 436
def <=>(other)
self.class == other.class && self.id <=> other.id
end
|
#==(other) ⇒ Object
432
433
434
|
# File 'lib/groovy/model.rb', line 432
def ==(other)
self.class == other.class && self.id == other.id
end
|
#[](key) ⇒ Object
347
348
349
350
351
|
# File 'lib/groovy/model.rb', line 347
def [](key)
k = key.to_sym
@attributes[k] = get_record_attribute(k) unless @attributes.key?(k)
@attributes[k]
end
|
#[]=(key, val) ⇒ Object
353
354
355
356
357
358
359
360
361
362
363
|
# File 'lib/groovy/model.rb', line 353
def []=(key, val)
if self.class.schema.singular_references.include?(key.to_sym) return set_ref(key, val)
end
unless self.class.attribute_names.include?(key.to_sym)
raise "Invalid attribute: #{key}"
end
set_attribute(key, val)
end
|
#as_json(options = {}) ⇒ Object
428
429
430
|
# File 'lib/groovy/model.rb', line 428
def as_json(options = {})
options[:only] ? attributes.slice(*options[:only]) : attributes
end
|
#attributes ⇒ Object
get reference to the actual record in the Groonga table, not the temporary one we get as part of a search result. def load_record
self.class.table[id]
end
333
334
335
336
|
# File 'lib/groovy/model.rb', line 333
def attributes
load_attributes_from_record @attributes
end
|
#delete ⇒ Object
404
405
406
407
408
409
410
411
412
|
# File 'lib/groovy/model.rb', line 404
def delete
self.class.table.delete(record._id) set_record(nil)
self
rescue Groonga::InvalidArgument => e
raise RecordNotPersisted, e.message
end
|
#dirty? ⇒ Boolean
370
371
372
|
# File 'lib/groovy/model.rb', line 370
def dirty?
changes.any?
end
|
#increment(values, do_save = true) ⇒ Object
365
366
367
368
|
# File 'lib/groovy/model.rb', line 365
def increment(values, do_save = true)
values.each { |key, num| self[key] += num }
save if do_save
end
|
#initialize(attrs = nil, record = nil, key = nil) ⇒ Object
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
|
# File 'lib/groovy/model.rb', line 307
def initialize(attrs = nil, record = nil, key = nil)
@attributes, @vectors, @_key = {}, {}, key
if set_record(record)
else
attrs ||= {}
unless attrs.is_a?(Hash)
raise ArgumentError.new("Attributes should be a Hash, not a #{attrs.class}")
end
set_attributes(attrs)
end
@changes = {}
end
|
#inspect ⇒ Object
338
339
340
|
# File 'lib/groovy/model.rb', line 338
def inspect
"#<#{self.class.name} id:#{id.inspect} attributes:[#{self.class.attribute_names.join(', ')}]>"
end
|
#new_record? ⇒ Boolean
342
343
344
345
|
# File 'lib/groovy/model.rb', line 342
def new_record?
id.nil?
end
|
#reload ⇒ Object
414
415
416
417
418
419
420
421
422
423
424
425
426
|
# File 'lib/groovy/model.rb', line 414
def reload
unless new_record?
rec = self.class.table[id] set_record(rec)
end
@attributes = {}
@changes = {}
self
end
|
#save(options = {}) ⇒ Object
390
391
392
393
394
395
396
397
|
# File 'lib/groovy/model.rb', line 390
def save(options = {})
return false if respond_to?(:invalid?) and invalid?
if new_record?
create && true
else
update
end
end
|
#save!(options = {}) ⇒ Object
399
400
401
402
|
# File 'lib/groovy/model.rb', line 399
def save!(options = {})
raise "Invalid!" unless save
self
end
|
#set_attributes(obj = {}) ⇒ Object
374
375
376
377
378
|
# File 'lib/groovy/model.rb', line 374
def set_attributes(obj = {})
obj.delete('_id') obj.each { |k,v| public_send("#{k}=", v) }
end
|
#update_attributes(obj) ⇒ Object
380
381
382
383
384
|
# File 'lib/groovy/model.rb', line 380
def update_attributes(obj)
set_attributes(obj)
return false if respond_to?(:invalid?) and invalid?
update
end
|
#update_attributes!(obj) ⇒ Object
386
387
388
|
# File 'lib/groovy/model.rb', line 386
def update_attributes!(obj)
update_attributes(obj) or raise "Invalid!"
end
|