Module: Groovy::Model
- Defined in:
- lib/groovy/model.rb
Defined Under Namespace
Modules: ClassMethods, HashTable, PatriciaTrieTable
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Instance Attribute Details
#changes ⇒ Object
Returns the value of attribute changes.
420
421
422
|
# File 'lib/groovy/model.rb', line 420
def changes
@changes
end
|
#id ⇒ Object
Returns the value of attribute id.
420
421
422
|
# File 'lib/groovy/model.rb', line 420
def id
@id
end
|
#record ⇒ Object
Returns the value of attribute record.
420
421
422
|
# File 'lib/groovy/model.rb', line 420
def record
@record
end
|
Class Method Details
.get_class(klass_name) ⇒ Object
71
72
73
|
# File 'lib/groovy/model.rb', line 71
def self.get_class(klass_name)
Kernel.const_get(Utils.classify(klass_name))
end
|
.included(base) ⇒ Object
61
62
63
64
65
66
67
68
69
|
# File 'lib/groovy/model.rb', line 61
def self.included(base)
base.extend(ClassMethods)
base.include(Forwardable)
base.table_name = Utils.pluralize(base.name)
Groovy.models[base.table_name] = base
end
|
.initialize_from_record(obj) ⇒ Object
47
48
49
50
|
# File 'lib/groovy/model.rb', line 47
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
57
58
59
|
# File 'lib/groovy/model.rb', line 57
def self.model_from_table(table_name)
get_class(Utils.singularize(table_name))
end
|
Instance Method Details
#<=>(other) ⇒ Object
574
575
576
|
# File 'lib/groovy/model.rb', line 574
def <=>(other)
self.class == other.class && self.id <=> other.id
end
|
#==(other) ⇒ Object
570
571
572
|
# File 'lib/groovy/model.rb', line 570
def ==(other)
self.class == other.class && self.id == other.id
end
|
#[](key) ⇒ Object
472
473
474
475
476
|
# File 'lib/groovy/model.rb', line 472
def [](key)
k = key.to_sym
@attributes[k] = get_record_attribute(k) unless @attributes.key?(k)
@attributes[k]
end
|
#[]=(key, val) ⇒ Object
478
479
480
481
482
483
484
485
486
487
488
|
# File 'lib/groovy/model.rb', line 478
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
566
567
568
|
# File 'lib/groovy/model.rb', line 566
def as_json(options = {})
options[:only] ? attributes.slice(*options[:only]) : attributes
end
|
#attributes ⇒ Object
Also known as:
attrs
449
450
451
452
|
# File 'lib/groovy/model.rb', line 449
def attributes
load_attributes_from_record @attributes
end
|
#delete ⇒ Object
541
542
543
544
545
546
547
548
549
|
# File 'lib/groovy/model.rb', line 541
def delete
self.class.table.delete(record._id) set_record(nil)
self
rescue Groonga::InvalidArgument => e
raise RecordNotPersisted, e.message
end
|
#dirty? ⇒ Boolean
495
496
497
|
# File 'lib/groovy/model.rb', line 495
def dirty?
changes.any?
end
|
#dump ⇒ Object
460
461
462
|
# File 'lib/groovy/model.rb', line 460
def dump
pp attributes; nil
end
|
#increment(values, do_save = true) ⇒ Object
490
491
492
493
|
# File 'lib/groovy/model.rb', line 490
def increment(values, do_save = true)
values.each { |key, num| self[key] += num }
save if do_save
end
|
#initialize(attrs = nil, record = nil) ⇒ Object
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
|
# File 'lib/groovy/model.rb', line 422
def initialize(attrs = nil, record = nil)
@attributes, @vectors = {}, {}
@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
456
457
458
|
# File 'lib/groovy/model.rb', line 456
def inspect
"#<#{self.class.name} id:#{id.inspect} attributes:[#{self.class.attribute_names.join(', ')}] references:[#{self.class.reference_names.join(', ')}]>"
end
|
#load_record ⇒ Object
get reference to the actual record in the Groonga table, not the temporary one we get as part of a search result.
445
446
447
|
# File 'lib/groovy/model.rb', line 445
def load_record
self.class.table[id]
end
|
#new_record? ⇒ Boolean
464
465
466
|
# File 'lib/groovy/model.rb', line 464
def new_record?
id.nil?
end
|
#persisted? ⇒ Boolean
468
469
470
|
# File 'lib/groovy/model.rb', line 468
def persisted?
!new_record?
end
|
#reload ⇒ Object
551
552
553
554
555
556
557
558
559
560
561
562
563
564
|
# File 'lib/groovy/model.rb', line 551
def reload
unless new_record?
rec = load_record
set_record(rec)
end
@attributes = {}
@changes = {}
@foreign_refs_to_update = {}
self
end
|
#save(options = {}) ⇒ Object
527
528
529
530
531
532
533
534
|
# File 'lib/groovy/model.rb', line 527
def save(options = {})
return false if respond_to?(:invalid?) and invalid?
if new_record?
create && true
else
update && true
end
end
|
#save!(options = {}) ⇒ Object
536
537
538
539
|
# File 'lib/groovy/model.rb', line 536
def save!(options = {})
raise "Invalid!" unless save
self
end
|
#set_attributes(obj = {}) ⇒ Object
499
500
501
502
503
|
# File 'lib/groovy/model.rb', line 499
def set_attributes(obj = {})
obj.delete('_id') obj.each { |k,v| public_send("#{k}=", v) }
end
|
#set_reverse_reference(key, obj, removing = false) ⇒ Object
505
506
507
508
509
510
511
512
513
514
515
|
# File 'lib/groovy/model.rb', line 505
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
517
518
519
520
521
|
# File 'lib/groovy/model.rb', line 517
def update_attributes(obj)
set_attributes(obj)
return false if respond_to?(:invalid?) and invalid?
update
end
|
#update_attributes!(obj) ⇒ Object
523
524
525
|
# File 'lib/groovy/model.rb', line 523
def update_attributes!(obj)
update_attributes(obj) or raise "Invalid!"
end
|