Class: Dense::Path
- Inherits:
-
Object
- Object
- Dense::Path
- Defined in:
- lib/dense/path.rb
Defined Under Namespace
Modules: Parser
Instance Attribute Summary collapse
-
#original ⇒ Object
readonly
Returns the value of attribute original.
Class Method Summary collapse
Instance Method Summary collapse
- #-(path) ⇒ Object
- #==(other) ⇒ Object
- #[](offset, count = nil) ⇒ Object
- #any? ⇒ Boolean
- #empty? ⇒ Boolean
- #enumerate(data) ⇒ Object
- #first ⇒ Object
- #gather(data) ⇒ Object
-
#initialize(s) ⇒ Path
constructor
A new instance of Path.
- #last ⇒ Object
- #length ⇒ Object (also: #size)
- #list(data) ⇒ Object
- #multiple? ⇒ Boolean
- #narrow(outcome) ⇒ Object
- #pop ⇒ Object
- #shift ⇒ Object
- #single? ⇒ Boolean
- #to_a ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(s) ⇒ Path
Returns a new instance of Path.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/dense/path.rb', line 7 def initialize(s) @original = s fail ArgumentError.new( "argument is a #{s.class}, not a String" ) unless s.is_a?(String) s = ".#{s}" \ unless s[0, 1] == '[' || s[0, 2] == '.[' #Raabro.pp(Parser.parse(s, debug: 3), colors: true) @path = Parser.parse(s) #Raabro.pp(Parser.parse(s, debug: 3), colors: true) unless @path fail ArgumentError.new( "couldn't determine path from #{s.inspect}" ) unless @path end |
Instance Attribute Details
#original ⇒ Object (readonly)
Returns the value of attribute original.
5 6 7 |
# File 'lib/dense/path.rb', line 5 def original @original end |
Class Method Details
.make(path_or_array_or_string) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/dense/path.rb', line 29 def make(path_or_array_or_string) case (pas = path_or_array_or_string) when nil then nil when Dense::Path then pas when Array then make_from_array(pas) when String then Dense::Path.new(pas) else fail ArgumentError.new( "cannot make Dense::Path instance out of #{pas.inspect}") end end |
Instance Method Details
#-(path) ⇒ Object
152 153 154 155 |
# File 'lib/dense/path.rb', line 152 def -(path) self.class.make(subtract(@path.dup, path.to_a.dup)) end |
#==(other) ⇒ Object
146 147 148 149 150 |
# File 'lib/dense/path.rb', line 146 def ==(other) other.class == self.class && other.to_a == @path end |
#[](offset, count = nil) ⇒ Object
135 136 137 138 139 140 141 142 143 144 |
# File 'lib/dense/path.rb', line 135 def [](offset, count=nil) if count == nil && offset.is_a?(Integer) @path[offset] elsif count self.class.make(@path[offset, count]) else self.class.make(@path[offset]) end end |
#any? ⇒ Boolean
112 |
# File 'lib/dense/path.rb', line 112 def any?; @path.any?; end |
#empty? ⇒ Boolean
113 |
# File 'lib/dense/path.rb', line 113 def empty?; @path.empty?; end |
#enumerate(data) ⇒ Object
169 170 171 172 173 |
# File 'lib/dense/path.rb', line 169 def enumerate(data) _gather(0, [], nil, data, @path, []) .collect { |_, p0, _, k| p0.dup.concat([ k ]).map(&:to_s).join('.') } end |
#first ⇒ Object
115 |
# File 'lib/dense/path.rb', line 115 def first; @path.first; end |
#gather(data) ⇒ Object
162 163 164 165 166 167 |
# File 'lib/dense/path.rb', line 162 def gather(data) _gather(0, [], nil, data, @path, []) .inject({}) { |h, hit| h[(hit[1] + [ hit[3] ]).inspect] ||= hit; h } .values end |
#last ⇒ Object
116 |
# File 'lib/dense/path.rb', line 116 def last; @path.last; end |
#length ⇒ Object Also known as: size
109 |
# File 'lib/dense/path.rb', line 109 def length; @path.length; end |
#list(data) ⇒ Object
175 176 177 178 179 |
# File 'lib/dense/path.rb', line 175 def list(data) _gather(0, [], nil, data, @path, []) .collect { |_, _, coll, k| coll[k] } end |
#multiple? ⇒ Boolean
99 100 101 102 |
# File 'lib/dense/path.rb', line 99 def multiple? ! single? end |
#narrow(outcome) ⇒ Object
157 158 159 160 |
# File 'lib/dense/path.rb', line 157 def narrow(outcome) single? ? outcome.first : outcome end |
#pop ⇒ Object
118 |
# File 'lib/dense/path.rb', line 118 def pop; @path.pop; end |
#shift ⇒ Object
119 |
# File 'lib/dense/path.rb', line 119 def shift; @path.shift; end |
#single? ⇒ Boolean
94 95 96 97 |
# File 'lib/dense/path.rb', line 94 def single? ! @path.find { |e| e.is_a?(Symbol) || e.is_a?(Hash) || e.is_a?(Array) } end |
#to_a ⇒ Object
104 105 106 107 |
# File 'lib/dense/path.rb', line 104 def to_a @path end |
#to_s ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/dense/path.rb', line 121 def to_s o = StringIO.new @path.each { |e| s = _to_s(e, false) o << '.' unless o.size == 0 || '[.'.index(s[0, 1]) o << s } s = o.string s[0, 2] == '..' ? s[1..-1] : s end |