Class: Foobara::DataPath
- Inherits:
-
Object
show all
- Defined in:
- foobara-0.2.2/projects/common/src/data_path.rb
Overview
TODO: figure out how to share code between here and ErrorKey TODO: use this to implement computed attributes
Defined Under Namespace
Classes: BadPathError, TooManyValuesAtPathError
Constant Summary
collapse
- EMPTY_PATH =
TODO: use this wherever it makes sense
[].freeze
- INDEX_VALUE =
/\A\d+\z/
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(path = []) ⇒ DataPath
TODO: accept error_class instead of symbol/category??
93
94
95
96
97
98
|
# File 'foobara-0.2.2/projects/common/src/data_path.rb', line 93
def initialize(path = [])
path = path.to_s if path.is_a?(::Symbol)
path = path.split(".") if path.is_a?(::String)
self.path = path
end
|
Instance Attribute Details
#path ⇒ Object
Returns the value of attribute path.
90
91
92
|
# File 'foobara-0.2.2/projects/common/src/data_path.rb', line 90
def path
@path
end
|
Class Method Details
.append_path(key) ⇒ Object
68
69
70
71
72
73
74
75
|
# File 'foobara-0.2.2/projects/common/src/data_path.rb', line 68
def append_path(key, *)
if key.is_a?(DataPath)
key.append(*)
else
key = new(key)
key.append!(*).to_s
end
end
|
.for(object) ⇒ Object
81
82
83
84
85
86
87
|
# File 'foobara-0.2.2/projects/common/src/data_path.rb', line 81
def for(object)
if object.is_a?(DataPath)
object
else
new(object)
end
end
|
.parse(key_string) ⇒ Object
77
78
79
|
# File 'foobara-0.2.2/projects/common/src/data_path.rb', line 77
def parse(key_string)
new(key_string)
end
|
.prepend_path(key) ⇒ Object
59
60
61
62
63
64
65
66
|
# File 'foobara-0.2.2/projects/common/src/data_path.rb', line 59
def prepend_path(key, *)
if key.is_a?(DataPath)
key.prepend(*)
else
key = new(key)
key.prepend!(*).to_s
end
end
|
.set_value_at(object, value, data_path) ⇒ Object
51
52
53
54
55
56
57
|
# File 'foobara-0.2.2/projects/common/src/data_path.rb', line 51
def set_value_at(object, value, data_path)
unless data_path.is_a?(DataPath)
data_path = new(data_path)
end
data_path.set_value_at(object, value)
end
|
.to_s_type(key) ⇒ Object
26
27
28
29
30
31
32
|
# File 'foobara-0.2.2/projects/common/src/data_path.rb', line 26
def to_s_type(key)
unless key.is_a?(DataPath)
key = new(key)
end
key.to_s_type
end
|
.value_at(data_path, object) ⇒ Object
43
44
45
46
47
48
49
|
# File 'foobara-0.2.2/projects/common/src/data_path.rb', line 43
def value_at(data_path, object)
unless data_path.is_a?(DataPath)
data_path = new(data_path)
end
data_path.value_at(object)
end
|
.values_at(data_path, object) ⇒ Object
TODO: flip these arguments?
35
36
37
38
39
40
41
|
# File 'foobara-0.2.2/projects/common/src/data_path.rb', line 35
def values_at(data_path, object)
unless data_path.is_a?(DataPath)
data_path = new(data_path)
end
data_path.values_at(object)
end
|
Instance Method Details
#==(other) ⇒ Object
212
213
214
|
# File 'foobara-0.2.2/projects/common/src/data_path.rb', line 212
def ==(other)
self.class == other.class && path == other.path
end
|
#append ⇒ Object
136
137
138
139
140
|
# File 'foobara-0.2.2/projects/common/src/data_path.rb', line 136
def append(*)
dup.tap do |key|
key.append!(*)
end
end
|
#append!(*append_parts) ⇒ Object
123
124
125
126
127
128
129
130
131
132
133
134
|
# File 'foobara-0.2.2/projects/common/src/data_path.rb', line 123
def append!(*append_parts)
if append_parts.size == 1
arg = append_parts.first
if arg.is_a?(Array)
append_parts = arg
end
end
self.path = [*path, *append_parts]
self
end
|
#empty? ⇒ Boolean
220
221
222
|
# File 'foobara-0.2.2/projects/common/src/data_path.rb', line 220
def empty?
path.empty?
end
|
#last ⇒ Object
216
217
218
|
# File 'foobara-0.2.2/projects/common/src/data_path.rb', line 216
def last
path.last
end
|
#prepend ⇒ Object
117
118
119
120
121
|
# File 'foobara-0.2.2/projects/common/src/data_path.rb', line 117
def prepend(*)
dup.tap do |key|
key.prepend!(*)
end
end
|
#prepend!(*prepend_parts) ⇒ Object
104
105
106
107
108
109
110
111
112
113
114
115
|
# File 'foobara-0.2.2/projects/common/src/data_path.rb', line 104
def prepend!(*prepend_parts)
if prepend_parts.size == 1
arg = prepend_parts.first
if arg.is_a?(Array)
prepend_parts = arg
end
end
self.path = [*prepend_parts, *path]
self
end
|
#set_value_at(object, value, parts = path) ⇒ Object
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
|
# File 'foobara-0.2.2/projects/common/src/data_path.rb', line 180
def set_value_at(object, value, parts = path)
owner = value_at(object, parts[0..-2])
index = parts.last
if owner.is_a?(::Hash)
if owner.key?(index.to_s)
owner[index.to_s] = value
else
owner[index] = value
end
elsif owner.is_a?(::Array)
owner[index] = value
else
method = "#{index}="
if owner.respond_to?(method)
owner.send(method, value)
else
raise BadPathError, "Bad path: #{parts}"
end
end
value
end
|
#simple_collection? ⇒ Boolean
Helper method that determines if the path points to an array and none of the atoms along the way are also arrays. And that there’s at least one atom (we are going to consider a collection to be “named” not an anonymous array.)
208
209
210
|
# File 'foobara-0.2.2/projects/common/src/data_path.rb', line 208
def simple_collection?
path.size > 1 && path.last == :"#" && path[0..-2].none? { |part| part == :"#" }
end
|
#to_s ⇒ Object
154
155
156
|
# File 'foobara-0.2.2/projects/common/src/data_path.rb', line 154
def to_s
path.join(".")
end
|
#to_s_type ⇒ Object
162
163
164
|
# File 'foobara-0.2.2/projects/common/src/data_path.rb', line 162
def to_s_type
to_type.to_s
end
|
#to_sym ⇒ Object
158
159
160
|
# File 'foobara-0.2.2/projects/common/src/data_path.rb', line 158
def to_sym(...)
to_s(...).to_sym
end
|
#to_type ⇒ Object
150
151
152
|
# File 'foobara-0.2.2/projects/common/src/data_path.rb', line 150
def to_type
dup.tap(&:to_type!)
end
|
#to_type! ⇒ Object
144
145
146
147
148
|
# File 'foobara-0.2.2/projects/common/src/data_path.rb', line 144
def to_type!
path.map! do |part|
part.is_a?(Integer) ? :"#" : part
end
end
|
#value_at(object, parts = path) ⇒ Object
170
171
172
173
174
175
176
177
178
|
# File 'foobara-0.2.2/projects/common/src/data_path.rb', line 170
def value_at(object, parts = path)
values = values_at(object, parts)
if values.size > 1
raise TooManyValuesAtPathError.new(path, values)
end
values.first
end
|
#values_at(object, parts = path) ⇒ Object
166
167
168
|
# File 'foobara-0.2.2/projects/common/src/data_path.rb', line 166
def values_at(object, parts = path)
_values_at([object], parts)
end
|