Class: JsonTools::Pointer
- Inherits:
-
Object
- Object
- JsonTools::Pointer
- Defined in:
- lib/jsontools/jsontools.rb
Defined Under Namespace
Classes: PointerError
Instance Method Summary collapse
-
#exists?(context) ⇒ Boolean
True if the referenced path exists.
-
#initialize(path) ⇒ Pointer
constructor
A new instance of Pointer.
-
#last ⇒ Object
Returns the last segment of the JSON Pointer.
-
#parent(context) ⇒ Object
(also: #[])
Evaluates the pointer against the given context hash object and returns the parent.
-
#value(context) ⇒ Object
Returns the specific value identified by this pointer, if any.
-
#value_with_fail(context) ⇒ Object
Alternative to value that raises a PointerError if the referenced path does not exist.
-
#walk(context) {|key, (!p ? nil : p[key])| ... } ⇒ Object
Enumerates down the pointer path, yielding to the given block each name, value pair specified in the path, halting at the first nil value encountered.
Constructor Details
#initialize(path) ⇒ Pointer
Returns a new instance of Pointer.
51 52 53 54 55 56 |
# File 'lib/jsontools/jsontools.rb', line 51 def initialize path @parts = path.split('/').drop(1).map { |p| p.gsub(/\~1/, '/').gsub(/\~0/, '~') } @last = @parts.pop end |
Instance Method Details
#exists?(context) ⇒ Boolean
True if the referenced path exists
118 119 120 121 122 123 124 125 126 127 |
# File 'lib/jsontools/jsontools.rb', line 118 def exists? context p = parent context if Array === p (0...p.length).cover? Integer(@last) else p.has_key? @last end rescue false end |
#last ⇒ Object
Returns the last segment of the JSON Pointer
59 |
# File 'lib/jsontools/jsontools.rb', line 59 def last; @last; end |
#parent(context) ⇒ Object Also known as: []
Evaluates the pointer against the given context hash object and returns the parent. That is, if the Pointer is “/a/b/c”, parent will return the object referenced by “/a/b”, or nil if that object does not exist.
67 68 69 70 71 72 73 |
# File 'lib/jsontools/jsontools.rb', line 67 def parent context @parts.reduce(context) do |o, p| o[(o.is_a?(Array) ? p.to_i : p)] end rescue raise PointerError end |
#value(context) ⇒ Object
Returns the specific value identified by this pointer, if any. Nil is returned if the path does not exist. Note that this does not differentiate between explicitly null values or missing paths.
102 103 104 105 |
# File 'lib/jsontools/jsontools.rb', line 102 def value context parent = parent context parent[JsonTools.fix_key(parent,@last)] unless !parent end |
#value_with_fail(context) ⇒ Object
Alternative to value that raises a PointerError if the referenced path does not exist.
109 110 111 112 113 114 115 |
# File 'lib/jsontools/jsontools.rb', line 109 def value_with_fail context parent = parent context fail if !parent parent.fetch(JsonTools.fix_key(parent,@last)) rescue raise PointerError end |
#walk(context) {|key, (!p ? nil : p[key])| ... } ⇒ Object
Enumerates down the pointer path, yielding to the given block each name, value pair specified in the path, halting at the first nil value encountered. The required block will be passed two parameters. The first is the accessor name, the second is the value. For instance, given the hash ‘a’=>{‘b’=>{‘c’=>123}}, and the pointer “/a/b/c”, the block will be called three times, first with [‘a’,‘b’=>{‘c’=>123}], next with [‘b’,‘c’=>123], and finally with [‘c’,123].
87 88 89 90 91 92 93 94 95 96 |
# File 'lib/jsontools/jsontools.rb', line 87 def walk context p = @parts.reduce(context) do |o,p| n = o[(o.is_a?(Array) ? p.to_i : p)] yield p, n return if NilClass === n # exit the loop if the object is nil n end key = JsonTools.fix_key(p,@last) yield key, (!p ? nil : p[key]) end |