Class: Hana::Pointer
- Inherits:
-
Object
show all
- Includes:
- Enumerable
- Defined in:
- lib/hana.rb
Defined Under Namespace
Classes: Exception, FormatError
Constant Summary
collapse
- ESC =
{'^/' => '/', '^^' => '^', '~0' => '~', '~1' => '/'}
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(path) ⇒ Pointer
Returns a new instance of Pointer.
15
16
17
|
# File 'lib/hana.rb', line 15
def initialize path
@path = Pointer.parse path
end
|
Class Method Details
.eval(list, object) ⇒ Object
27
28
29
30
31
32
33
34
35
36
37
|
# File 'lib/hana.rb', line 27
def self.eval list, object
list.inject(object) { |o, part|
return nil unless o
if Array === o
raise Patch::IndexError unless part =~ /\A(?:\d|[1-9]\d+)\Z/
part = part.to_i
end
o[part]
}
end
|
.parse(path) ⇒ Object
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
# File 'lib/hana.rb', line 39
def self.parse path
return [''] if path == '/'
return [] if path == ''
unless path.start_with? '/'
raise FormatError, "JSON Pointer should start with a slash"
end
parts = path.sub(/^\//, '').split(/(?<!\^)\//).each { |part|
part.gsub!(/\^[\/^]|~[01]/) { |m| ESC[m] }
}
parts.push("") if path[-1] == '/'
parts
end
|
Instance Method Details
#each(&block) ⇒ Object
19
|
# File 'lib/hana.rb', line 19
def each(&block); @path.each(&block); end
|
#eval(object) ⇒ Object
21
22
23
|
# File 'lib/hana.rb', line 21
def eval object
Pointer.eval @path, object
end
|