Class: DataGraph::Node
- Inherits:
-
Object
show all
- Includes:
- Utils
- Defined in:
- lib/data_graph/node.rb
Overview
– Treated as immutable once created.
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from Utils
cpk?, foreign_key, patherize_attrs, primary_keys, reference_key
Constructor Details
#initialize(model, options = {}) ⇒ Node
Returns a new instance of Node.
15
16
17
18
|
# File 'lib/data_graph/node.rb', line 15
def initialize(model, options={})
@model = model
self.options = options
end
|
Instance Attribute Details
#always_columns ⇒ Object
Returns the value of attribute always_columns.
13
14
15
|
# File 'lib/data_graph/node.rb', line 13
def always_columns
@always_columns
end
|
#column_names ⇒ Object
Returns the value of attribute column_names.
10
11
12
|
# File 'lib/data_graph/node.rb', line 10
def column_names
@column_names
end
|
#linkages ⇒ Object
Returns the value of attribute linkages.
12
13
14
|
# File 'lib/data_graph/node.rb', line 12
def linkages
@linkages
end
|
#method_names ⇒ Object
Returns the value of attribute method_names.
11
12
13
|
# File 'lib/data_graph/node.rb', line 11
def method_names
@method_names
end
|
#model ⇒ Object
Returns the value of attribute model.
9
10
11
|
# File 'lib/data_graph/node.rb', line 9
def model
@model
end
|
Instance Method Details
#[](name) ⇒ Object
24
25
26
27
|
# File 'lib/data_graph/node.rb', line 24
def [](name)
linkage = linkages[name.to_s]
linkage ? linkage.node : nil
end
|
#aliases ⇒ Object
71
72
73
74
75
76
77
78
79
80
81
|
# File 'lib/data_graph/node.rb', line 71
def aliases
aliases = {'*' => column_names.dup}
linkages.each_pair do |name, linkage|
linkage.node.aliases.each_pair do |als, paths|
aliases["#{name}.#{als}"] = paths.collect {|path| "#{name}.#{path}" }
end
end
aliases
end
|
#associations ⇒ Object
20
21
22
|
# File 'lib/data_graph/node.rb', line 20
def associations
linkages.keys
end
|
#except(paths) ⇒ Object
204
205
206
|
# File 'lib/data_graph/node.rb', line 204
def except(paths)
dup.except!(paths)
end
|
#except!(paths) ⇒ Object
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
|
# File 'lib/data_graph/node.rb', line 183
def except!(paths)
attr_paths, nest_paths = partition(paths)
source, target = linkages, {}
(attr_paths - nest_paths.keys).each do |path|
source.delete(path)
end
nest_paths.each_pair do |name, paths|
if linkage = source[name]
target[name] = linkage.inherit(:except, paths)
end
end
@column_names -= attr_paths
@method_names -= attr_paths
@linkages = target
self
end
|
#find(*args) ⇒ Object
125
126
127
|
# File 'lib/data_graph/node.rb', line 125
def find(*args)
link model.find(*find_args(args))
end
|
#find_args(args = []) ⇒ Object
133
134
135
136
137
|
# File 'lib/data_graph/node.rb', line 133
def find_args(args=[])
args << {} unless args.last.kind_of?(Hash)
scope(args.last)
args
end
|
#get_paths ⇒ Object
40
41
42
43
44
45
46
47
48
49
50
51
52
|
# File 'lib/data_graph/node.rb', line 40
def get_paths
paths = primary_keys(model) + column_names + method_names
linkages.each_pair do |name, linkage|
paths << name
paths.concat linkage.parent_columns
(linkage.node.get_paths + linkage.child_columns).each do |path|
paths << "#{name}.#{path}"
end
end
paths.uniq!
paths
end
|
#link(records) ⇒ Object
148
149
150
151
152
153
154
|
# File 'lib/data_graph/node.rb', line 148
def link(records)
linkages.each_value do |linkage|
linkage.link(records)
end
records
end
|
#nest_paths ⇒ Object
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
# File 'lib/data_graph/node.rb', line 83
def nest_paths
paths = []
linkages.each_pair do |name, linkage|
if linkage.macro == :has_many
paths << "#{name}_attributes"
end
linkage.node.nest_paths.each do |path|
paths << "#{name}.#{path}"
end
end
paths
end
|
#only(paths) ⇒ Object
179
180
181
|
# File 'lib/data_graph/node.rb', line 179
def only(paths)
dup.only!(paths)
end
|
#only!(paths) ⇒ Object
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
# File 'lib/data_graph/node.rb', line 156
def only!(paths)
attr_paths, nest_paths = partition(paths)
source, target = linkages, {}
attr_paths.each do |name|
if linkage = source[name]
target[name] = linkage
end
end
nest_paths.each_pair do |name, paths|
if linkage = source[name]
target[name] = linkage.inherit(:only, paths)
end
end
@column_names &= attr_paths
@method_names &= attr_paths
@linkages = target
self
end
|
#options ⇒ Object
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
# File 'lib/data_graph/node.rb', line 99
def options
associations = {}
linkages.each_pair do |name, linkage|
associations[name.to_sym] = linkage.node.options
end
{
:only => column_names,
:methods => method_names,
:include => associations
}
end
|
#options=(options) ⇒ Object
115
116
117
118
119
120
121
122
123
|
# File 'lib/data_graph/node.rb', line 115
def options=(options)
unless options.kind_of?(Hash)
raise "not a hash: #{options.inspect}"
end
@column_names = parse_columns(options)
@method_names = parse_methods(options)
@linkages = parse_linkages(options)
@always_columns = parse_always(options)
end
|
#paginate(*args) ⇒ Object
129
130
131
|
# File 'lib/data_graph/node.rb', line 129
def paginate(*args)
link model.paginate(*find_args(args))
end
|
#paths ⇒ Object
29
30
31
32
33
34
35
36
37
38
|
# File 'lib/data_graph/node.rb', line 29
def paths
paths = column_names + method_names
linkages.each_pair do |name, linkage|
linkage.node.paths.each do |path|
paths << "#{name}.#{path}"
end
end
paths
end
|
#scope(options = {}) ⇒ Object
139
140
141
142
143
144
145
146
|
# File 'lib/data_graph/node.rb', line 139
def scope(options={})
columns = arrayify(options[:select]) + column_names + always_columns
linkages.each_value {|linkage| columns.concat linkage.parent_columns }
columns.uniq!
options[:select] = columns.join(',')
options
end
|
#set_paths ⇒ Object
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
# File 'lib/data_graph/node.rb', line 54
def set_paths
paths = primary_keys(model) + column_names + method_names
nested_attributes = model.nested_attributes_options
linkages.each_pair do |name, linkage|
next unless nested_attributes.has_key?(name.to_sym)
attributes = linkage.node.set_paths
attributes += ActiveRecord::NestedAttributes::UNASSIGNABLE_KEYS
attributes.each do |path|
paths << "#{name}_attributes.#{path}"
end
end
paths.uniq!
paths
end
|