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
- #first ⇒ Object
- #gather(data) ⇒ Object
-
#initialize(s) ⇒ Path
constructor
A new instance of Path.
- #last ⇒ Object
- #length ⇒ Object (also: #size)
- #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.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/dense/path.rb', line 6 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.
4 5 6 |
# File 'lib/dense/path.rb', line 4 def original @original end |
Class Method Details
.make(path_or_array_or_string) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/dense/path.rb', line 28 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
151 152 153 154 |
# File 'lib/dense/path.rb', line 151 def -(path) self.class.make(subtract(@path.dup, path.to_a.dup)) end |
#==(other) ⇒ Object
145 146 147 148 149 |
# File 'lib/dense/path.rb', line 145 def ==(other) other.class == self.class && other.to_a == @path end |
#[](offset, count = nil) ⇒ Object
134 135 136 137 138 139 140 141 142 143 |
# File 'lib/dense/path.rb', line 134 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
111 |
# File 'lib/dense/path.rb', line 111 def any?; @path.any?; end |
#empty? ⇒ Boolean
112 |
# File 'lib/dense/path.rb', line 112 def empty?; @path.empty?; end |
#first ⇒ Object
114 |
# File 'lib/dense/path.rb', line 114 def first; @path.first; end |
#gather(data) ⇒ Object
161 162 163 164 165 166 |
# File 'lib/dense/path.rb', line 161 def gather(data) _gather(0, [], nil, data, @path, []) .inject({}) { |h, hit| h[(hit[1] + [ hit[3] ]).inspect] ||= hit; h } .values end |
#last ⇒ Object
115 |
# File 'lib/dense/path.rb', line 115 def last; @path.last; end |
#length ⇒ Object Also known as: size
108 |
# File 'lib/dense/path.rb', line 108 def length; @path.length; end |
#multiple? ⇒ Boolean
98 99 100 101 |
# File 'lib/dense/path.rb', line 98 def multiple? ! single? end |
#narrow(outcome) ⇒ Object
156 157 158 159 |
# File 'lib/dense/path.rb', line 156 def narrow(outcome) single? ? outcome.first : outcome end |
#pop ⇒ Object
117 |
# File 'lib/dense/path.rb', line 117 def pop; @path.pop; end |
#shift ⇒ Object
118 |
# File 'lib/dense/path.rb', line 118 def shift; @path.shift; end |
#single? ⇒ Boolean
93 94 95 96 |
# File 'lib/dense/path.rb', line 93 def single? ! @path.find { |e| e.is_a?(Symbol) || e.is_a?(Hash) || e.is_a?(Array) } end |
#to_a ⇒ Object
103 104 105 106 |
# File 'lib/dense/path.rb', line 103 def to_a @path end |
#to_s ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/dense/path.rb', line 120 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 |