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.
322
323
324
|
# File 'lib/groovy/model.rb', line 322
def changes
@changes
end
|
#id ⇒ Object
Returns the value of attribute id.
322
323
324
|
# File 'lib/groovy/model.rb', line 322
def id
@id
end
|
#record ⇒ Object
Returns the value of attribute record.
322
323
324
|
# File 'lib/groovy/model.rb', line 322
def record
@record
end
|
Class Method Details
.get_class(table_name) ⇒ Object
46
47
48
49
50
|
# File 'lib/groovy/model.rb', line 46
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
36
37
38
39
40
41
42
43
44
|
# File 'lib/groovy/model.rb', line 36
def self.included(base)
base.extend(ClassMethods)
base.include(Forwardable)
base.table_name = base.name.sub(/y$/, 'ie') + 's'
Groovy.models[base.table_name] = base
end
|
.initialize_from_record(obj) ⇒ Object
22
23
24
25
|
# File 'lib/groovy/model.rb', line 22
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
32
33
34
|
# File 'lib/groovy/model.rb', line 32
def self.model_from_table(table_name)
get_class(table_name.to_s.sub(/ies$/, 'y').sub(/s$/, ''))
end
|
Instance Method Details
#<=>(other) ⇒ Object
477
478
479
|
# File 'lib/groovy/model.rb', line 477
def <=>(other)
self.class == other.class && self.id <=> other.id
end
|
#==(other) ⇒ Object
473
474
475
|
# File 'lib/groovy/model.rb', line 473
def ==(other)
self.class == other.class && self.id == other.id
end
|
#[](key) ⇒ Object
375
376
377
378
379
|
# File 'lib/groovy/model.rb', line 375
def [](key)
k = key.to_sym
@attributes[k] = get_record_attribute(k) unless @attributes.key?(k)
@attributes[k]
end
|
#[]=(key, val) ⇒ Object
381
382
383
384
385
386
387
388
389
390
391
|
# File 'lib/groovy/model.rb', line 381
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
469
470
471
|
# File 'lib/groovy/model.rb', line 469
def as_json(options = {})
options[:only] ? attributes.slice(*options[:only]) : attributes
end
|
#attributes ⇒ Object
Also known as:
attrs
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
351
352
353
354
|
# File 'lib/groovy/model.rb', line 351
def attributes
load_attributes_from_record @attributes
end
|
#delete ⇒ Object
444
445
446
447
448
449
450
451
452
|
# File 'lib/groovy/model.rb', line 444
def delete
self.class.table.delete(record._id) set_record(nil)
self
rescue Groonga::InvalidArgument => e
raise RecordNotPersisted, e.message
end
|
#dirty? ⇒ Boolean
398
399
400
|
# File 'lib/groovy/model.rb', line 398
def dirty?
changes.any?
end
|
#dump ⇒ Object
362
363
364
|
# File 'lib/groovy/model.rb', line 362
def dump
pp attributes; nil
end
|
#increment(values, do_save = true) ⇒ Object
393
394
395
396
|
# File 'lib/groovy/model.rb', line 393
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
|
# File 'lib/groovy/model.rb', line 324
def initialize(attrs = nil, record = nil, key = nil)
@attributes, @vectors, @_key = {}, {}, key @foreign_refs_to_update = {}
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
358
359
360
|
# File 'lib/groovy/model.rb', line 358
def inspect
"#<#{self.class.name} id:#{id.inspect} attributes:[#{self.class.attribute_names.join(', ')}]>"
end
|
#new_record? ⇒ Boolean
366
367
368
369
|
# File 'lib/groovy/model.rb', line 366
def new_record?
id.nil?
end
|
#persisted? ⇒ Boolean
371
372
373
|
# File 'lib/groovy/model.rb', line 371
def persisted?
!new_record?
end
|
#reload ⇒ Object
454
455
456
457
458
459
460
461
462
463
464
465
466
467
|
# File 'lib/groovy/model.rb', line 454
def reload
unless new_record?
rec = self.class.table[id] set_record(rec)
end
@attributes = {}
@changes = {}
@foreign_refs_to_update = {}
self
end
|
#save(options = {}) ⇒ Object
430
431
432
433
434
435
436
437
|
# File 'lib/groovy/model.rb', line 430
def save(options = {})
return false if respond_to?(:invalid?) and invalid?
if new_record?
create && true
else
update && true
end
end
|
#save!(options = {}) ⇒ Object
439
440
441
442
|
# File 'lib/groovy/model.rb', line 439
def save!(options = {})
raise "Invalid!" unless save
self
end
|
#set_attributes(obj = {}) ⇒ Object
402
403
404
405
406
|
# File 'lib/groovy/model.rb', line 402
def set_attributes(obj = {})
obj.delete('_id') obj.each { |k,v| public_send("#{k}=", v) }
end
|
#set_reverse_reference(key, obj, removing = false) ⇒ Object
408
409
410
411
412
413
414
415
416
417
418
|
# File 'lib/groovy/model.rb', line 408
def set_reverse_reference(key, obj, removing = false)
if self.class.schema.singular_references.include?(key.to_sym)
set_ref(key, removing ? nil : obj, true)
elsif self.class.schema.plural_references.include?(key.to_sym)
public_send(key).set_ref(obj, removing)
else
raise "Invalid reference name: #{name}"
end
end
|
#update_attributes(obj) ⇒ Object
420
421
422
423
424
|
# File 'lib/groovy/model.rb', line 420
def update_attributes(obj)
set_attributes(obj)
return false if respond_to?(:invalid?) and invalid?
update
end
|
#update_attributes!(obj) ⇒ Object
426
427
428
|
# File 'lib/groovy/model.rb', line 426
def update_attributes!(obj)
update_attributes(obj) or raise "Invalid!"
end
|