Class: JPath::Pointer
- Inherits:
-
Object
- Object
- JPath::Pointer
- Defined in:
- lib/jpath/pointer.rb
Class Method Summary collapse
Instance Method Summary collapse
- #+(string) ⇒ Object
- #==(other) ⇒ Object
- #each(&block) ⇒ Object
- #grandparent ⇒ Object
- #index? ⇒ Boolean
-
#initialize(*ary) ⇒ Pointer
constructor
A new instance of Pointer.
- #name ⇒ Object
- #parent ⇒ Object
- #size ⇒ Object
- #to_s ⇒ Object
- #top? ⇒ Boolean
Constructor Details
#initialize(*ary) ⇒ Pointer
Returns a new instance of Pointer.
10 11 12 |
# File 'lib/jpath/pointer.rb', line 10 def initialize(*ary) @ary = ary.flatten end |
Class Method Details
.parse(string) ⇒ Object
4 5 6 7 8 |
# File 'lib/jpath/pointer.rb', line 4 def self.parse(string) new string.split(".").map { |str| str =~ /\A\d+\z/ ? str.to_i : str }.to_a end |
Instance Method Details
#+(string) ⇒ Object
18 19 20 |
# File 'lib/jpath/pointer.rb', line 18 def +(string) self.class.new(@ary + [string]) end |
#==(other) ⇒ Object
14 15 16 |
# File 'lib/jpath/pointer.rb', line 14 def ==(other) to_s == other.to_s end |
#each(&block) ⇒ Object
22 23 24 |
# File 'lib/jpath/pointer.rb', line 22 def each(&block) @ary.each(&block) end |
#grandparent ⇒ Object
30 31 32 |
# File 'lib/jpath/pointer.rb', line 30 def grandparent parent.nil? ? nil : parent.parent end |
#index? ⇒ Boolean
42 43 44 |
# File 'lib/jpath/pointer.rb', line 42 def index? @ary.last.is_a?(Integer) end |
#name ⇒ Object
26 27 28 |
# File 'lib/jpath/pointer.rb', line 26 def name index? ? (parent.nil? ? nil : parent.name) : @ary.last end |
#parent ⇒ Object
34 35 36 |
# File 'lib/jpath/pointer.rb', line 34 def parent top? ? nil : self.class.new(without_last) end |
#size ⇒ Object
38 39 40 |
# File 'lib/jpath/pointer.rb', line 38 def size @ary.size end |
#to_s ⇒ Object
50 51 52 |
# File 'lib/jpath/pointer.rb', line 50 def to_s @ary.join(".") end |
#top? ⇒ Boolean
46 47 48 |
# File 'lib/jpath/pointer.rb', line 46 def top? @ary.empty? or top_index? end |