Class: Foobara::DataPath
- Inherits:
-
Object
show all
- Defined in:
- foobara-0.0.130/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??
92
93
94
95
96
97
|
# File 'foobara-0.0.130/projects/common/src/data_path.rb', line 92
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.
89
90
91
|
# File 'foobara-0.0.130/projects/common/src/data_path.rb', line 89
def path
@path
end
|
Class Method Details
.append_path(key) ⇒ Object
67
68
69
70
71
72
73
74
|
# File 'foobara-0.0.130/projects/common/src/data_path.rb', line 67
def append_path(key, *)
if key.is_a?(DataPath)
key.append(*)
else
key = new(key)
key.append!(*).to_s
end
end
|
.for(object) ⇒ Object
80
81
82
83
84
85
86
|
# File 'foobara-0.0.130/projects/common/src/data_path.rb', line 80
def for(object)
if object.is_a?(DataPath)
object
else
new(object)
end
end
|
.parse(key_string) ⇒ Object
76
77
78
|
# File 'foobara-0.0.130/projects/common/src/data_path.rb', line 76
def parse(key_string)
new(key_string)
end
|
.prepend_path(key) ⇒ Object
58
59
60
61
62
63
64
65
|
# File 'foobara-0.0.130/projects/common/src/data_path.rb', line 58
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
50
51
52
53
54
55
56
|
# File 'foobara-0.0.130/projects/common/src/data_path.rb', line 50
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.0.130/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
42
43
44
45
46
47
48
|
# File 'foobara-0.0.130/projects/common/src/data_path.rb', line 42
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
34
35
36
37
38
39
40
|
# File 'foobara-0.0.130/projects/common/src/data_path.rb', line 34
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
211
212
213
|
# File 'foobara-0.0.130/projects/common/src/data_path.rb', line 211
def ==(other)
self.class == other.class && path == other.path
end
|
#append ⇒ Object
135
136
137
138
139
|
# File 'foobara-0.0.130/projects/common/src/data_path.rb', line 135
def append(*)
dup.tap do |key|
key.append!(*)
end
end
|
#append!(*append_parts) ⇒ Object
122
123
124
125
126
127
128
129
130
131
132
133
|
# File 'foobara-0.0.130/projects/common/src/data_path.rb', line 122
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
219
220
221
|
# File 'foobara-0.0.130/projects/common/src/data_path.rb', line 219
def empty?
path.empty?
end
|
#last ⇒ Object
215
216
217
|
# File 'foobara-0.0.130/projects/common/src/data_path.rb', line 215
def last
path.last
end
|
#prepend ⇒ Object
116
117
118
119
120
|
# File 'foobara-0.0.130/projects/common/src/data_path.rb', line 116
def prepend(*)
dup.tap do |key|
key.prepend!(*)
end
end
|
#prepend!(*prepend_parts) ⇒ Object
103
104
105
106
107
108
109
110
111
112
113
114
|
# File 'foobara-0.0.130/projects/common/src/data_path.rb', line 103
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
|
# File 'foobara-0.0.130/projects/common/src/data_path.rb', line 179
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.)
207
208
209
|
# File 'foobara-0.0.130/projects/common/src/data_path.rb', line 207
def simple_collection?
path.size > 1 && path.last == :"#" && path[0..-2].none? { |part| part == :"#" }
end
|
#to_s ⇒ Object
153
154
155
|
# File 'foobara-0.0.130/projects/common/src/data_path.rb', line 153
def to_s
path.join(".")
end
|
#to_s_type ⇒ Object
161
162
163
|
# File 'foobara-0.0.130/projects/common/src/data_path.rb', line 161
def to_s_type
to_type.to_s
end
|
#to_sym ⇒ Object
157
158
159
|
# File 'foobara-0.0.130/projects/common/src/data_path.rb', line 157
def to_sym(...)
to_s(...).to_sym
end
|
#to_type ⇒ Object
149
150
151
|
# File 'foobara-0.0.130/projects/common/src/data_path.rb', line 149
def to_type
dup.tap(&:to_type!)
end
|
#to_type! ⇒ Object
143
144
145
146
147
|
# File 'foobara-0.0.130/projects/common/src/data_path.rb', line 143
def to_type!
path.map! do |part|
part.is_a?(Integer) ? :"#" : part
end
end
|
#value_at(object, parts = path) ⇒ Object
169
170
171
172
173
174
175
176
177
|
# File 'foobara-0.0.130/projects/common/src/data_path.rb', line 169
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
165
166
167
|
# File 'foobara-0.0.130/projects/common/src/data_path.rb', line 165
def values_at(object, parts = path)
_values_at([object], parts)
end
|