Class: JPath::Pointer

Inherits:
Object
  • Object
show all
Defined in:
lib/jpath/pointer.rb

Class Method Summary collapse

Instance Method Summary collapse

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

#grandparentObject



30
31
32
# File 'lib/jpath/pointer.rb', line 30

def grandparent
  parent.nil? ? nil : parent.parent
end

#index?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/jpath/pointer.rb', line 42

def index?
  @ary.last.is_a?(Integer)
end

#nameObject



26
27
28
# File 'lib/jpath/pointer.rb', line 26

def name
  index? ? (parent.nil? ? nil : parent.name) : @ary.last
end

#parentObject



34
35
36
# File 'lib/jpath/pointer.rb', line 34

def parent
  top? ? nil : self.class.new(without_last)
end

#sizeObject



38
39
40
# File 'lib/jpath/pointer.rb', line 38

def size
  @ary.size
end

#to_sObject



50
51
52
# File 'lib/jpath/pointer.rb', line 50

def to_s
  @ary.join(".")
end

#top?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/jpath/pointer.rb', line 46

def top?
  @ary.empty? or top_index?
end