Class: Groovy::Vector
- Inherits:
-
Object
show all
- Includes:
- Enumerable
- Defined in:
- lib/groovy/vector.rb
Constant Summary
collapse
- REMOVE_MISSING =
true.freeze
Instance Method Summary
collapse
Constructor Details
#initialize(obj, key) ⇒ Vector
Returns a new instance of Vector.
7
8
9
|
# File 'lib/groovy/vector.rb', line 7
def initialize(obj, key)
@obj, @key = obj, key
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
115
116
117
118
119
120
121
|
# File 'lib/groovy/vector.rb', line 115
def method_missing(name, *args, &block)
if vector_model.scopes.include?(name)
query.send(name)
else
super
end
end
|
Instance Method Details
#==(arr) ⇒ Object
108
109
110
|
# File 'lib/groovy/vector.rb', line 108
def ==(arr)
to_a == arr
end
|
#as_json(options = {}) ⇒ Object
21
22
23
24
25
|
# File 'lib/groovy/vector.rb', line 21
def as_json(options = {})
Array.new.tap do |arr|
each { |record| arr.push(record.as_json(options)) }
end
end
|
#clear ⇒ Object
33
34
35
|
# File 'lib/groovy/vector.rb', line 33
def clear
set([])
end
|
#each(&block) ⇒ Object
29
30
31
|
# File 'lib/groovy/vector.rb', line 29
def each(&block)
items.each { |r| block.call(r) }
end
|
#has?(item) ⇒ Boolean
44
45
46
47
|
# File 'lib/groovy/vector.rb', line 44
def has?(item)
return false unless item.record
obj.record[key].find { |r| r == item.record }
end
|
#inspect ⇒ Object
17
18
19
|
# File 'lib/groovy/vector.rb', line 17
def inspect
"#<#{obj.class}->#{key.capitalize} size:#{size}>"
end
|
#push(item) ⇒ Object
Also known as:
<<
49
50
51
52
53
54
55
56
57
58
59
|
# File 'lib/groovy/vector.rb', line 49
def push(item)
raise "Invalid item type: #{item.class}" unless item.is_a?(Model)
check_parent!
if has?(item)
return false
end
item.save unless item.record
push_record(item.record)
end
|
#push!(item) ⇒ Object
61
62
63
|
# File 'lib/groovy/vector.rb', line 61
def push!(item)
push or raise "Already in list!"
end
|
#remove(item) ⇒ Object
65
66
67
68
69
|
# File 'lib/groovy/vector.rb', line 65
def remove(item)
check_parent!
raise "Item not saved: #{item.inspect}" unless item.record
remove_record(item.record)
end
|
#set(new_items) ⇒ Object
37
38
39
40
41
42
|
# File 'lib/groovy/vector.rb', line 37
def set(new_items)
check_parent!
obj.record[key] = new_items.map do |obj|
obj.is_a?(Model) ? obj.record : obj
end
end
|
#set_ref(item, removing = false) ⇒ Object
71
72
73
74
75
76
77
78
79
80
|
# File 'lib/groovy/vector.rb', line 71
def set_ref(item, removing = false)
check_parent!
raise "Item not saved: #{item.inspect}" unless item.record
if removing
remove_record(item.record)
elsif !has?(item)
push_record(item.record)
end
end
|
#size ⇒ Object
Also known as:
count
11
12
13
14
15
|
# File 'lib/groovy/vector.rb', line 11
def size
return 0 unless obj.record
records.count
end
|