Class: Ro::Node::List
- Inherits:
-
Array
- Object
- Array
- Ro::Node::List
show all
- Includes:
- Pagination
- Defined in:
- lib/ro/node/list.rb
Instance Method Summary
collapse
Methods included from Pagination
#current_page, #num_pages, #num_pages=, #page, #page=, #paginate, #per, #per=, #total_pages
Constructor Details
#initialize(*args, &block) ⇒ List
Returns a new instance of List.
8
9
10
11
12
13
14
15
16
17
18
19
|
# File 'lib/ro/node/list.rb', line 8
def initialize(*args, &block)
options = Map.options_for!(args)
root = args.shift || options[:root]
type = args.shift || options[:type]
@root = Root.new(root)
@type = type.nil? ? nil : String(type)
@index = {}
block.call(self) if block
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
119
120
121
122
123
124
125
126
127
128
129
130
131
|
# File 'lib/ro/node/list.rb', line 119
def method_missing(method, *args, &block)
Ro.log "Ro::List(#{ identifier })#method_missing(#{ method.inspect }, #{ args.inspect })"
if @type.nil?
type = method.to_s
list = self[type]
super unless list
list.empty? ? super : list
else
node = self[Slug.for(method, :join => '-')] || self[Slug.for(method, :join => '_')]
node.nil? ? super : node
end
end
|
Instance Method Details
#[](*args, &block) ⇒ Object
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
# File 'lib/ro/node/list.rb', line 53
def [](*args, &block)
key = args.first
case key
when String, Symbol
if @type.nil?
type = key.to_s
list = select{|node| type == node._type}
list.type = type
list
else
id = Slug.for(key.to_s)
detect{|node| id == node.id}
end
else
super(*args, &block)
end
end
|
#_binding ⇒ Object
137
138
139
|
# File 'lib/ro/node/list.rb', line 137
def _binding
Kernel.binding
end
|
#add(node) ⇒ Object
29
30
31
32
33
34
35
36
37
38
39
|
# File 'lib/ro/node/list.rb', line 29
def add(node)
return nil if node.nil?
unless index.has_key?(node.identifier)
push(node)
index[node.identifier] = node
node
else
false
end
end
|
#binding ⇒ Object
133
134
135
|
# File 'lib/ro/node/list.rb', line 133
def binding
Kernel.binding
end
|
#find(*args, &block) ⇒ Object
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
# File 'lib/ro/node/list.rb', line 94
def find(*args, &block)
case
when !args.empty? && block
raise ArgumentError.new
when args.empty? && block
detect{|node| node.instance_eval(&block)}
when args.size == 1
id = args.first.to_s
detect{|node| node.id == id}
when args.size > 1
where(*args, &block)
else
raise ArgumentError.new
end
end
|
#identifier ⇒ Object
113
114
115
|
# File 'lib/ro/node/list.rb', line 113
def identifier
[root, type].compact.join('/')
end
|
#load(path) ⇒ Object
25
26
27
|
# File 'lib/ro/node/list.rb', line 25
def load(path)
add( node = Node.new(path) )
end
|
#nodes ⇒ Object
21
22
23
|
# File 'lib/ro/node/list.rb', line 21
def nodes
self
end
|
41
42
43
44
45
46
47
48
49
50
51
|
# File 'lib/ro/node/list.rb', line 41
def related(*args, &block)
related = List.new(root)
each do |node|
node.related(*args, &block).each do |related_node|
related.add(related_node)
end
end
related
end
|
#select(*args, &block) ⇒ Object
72
73
74
|
# File 'lib/ro/node/list.rb', line 72
def select(*args, &block)
List.new(root){|list| list.replace(super)}
end
|
#where(*args, &block) ⇒ Object
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
# File 'lib/ro/node/list.rb', line 76
def where(*args, &block)
case
when !args.empty? && block
raise ArgumentError.new
when args.empty? && block
select{|node| node.instance_eval(&block)}
when !args.empty?
ids = args.flatten.compact.uniq.map{|arg| Slug.for(arg.to_s)}
index = ids.inject(Hash.new){|h,id| h.update(id => id)}
select{|node| index[node.id]}
else
raise ArgumentError.new
end
end
|