Class: JSONP3::NameSelector

Inherits:
Selector show all
Defined in:
lib/json_p3/selector.rb

Overview

The name selector select values from hashes given a key.

Direct Known Subclasses

SymbolNameSelector

Instance Attribute Summary collapse

Attributes inherited from Selector

#token

Instance Method Summary collapse

Constructor Details

#initialize(env, token, name) ⇒ NameSelector

Returns a new instance of NameSelector.



31
32
33
34
# File 'lib/json_p3/selector.rb', line 31

def initialize(env, token, name)
  super(env, token)
  @name = name
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



29
30
31
# File 'lib/json_p3/selector.rb', line 29

def name
  @name
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



52
53
54
55
56
# File 'lib/json_p3/selector.rb', line 52

def ==(other)
  self.class == other.class &&
    @name == other.name &&
    @token == other.token
end

#hashObject



60
61
62
# File 'lib/json_p3/selector.rb', line 60

def hash
  [@name, @token].hash
end

#resolve(node) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/json_p3/selector.rb', line 36

def resolve(node)
  if node.value.is_a?(Hash) && node.value.key?(@name)
    [node.new_child(node.value[@name], @name)]
  else
    []
  end
end

#singular?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/json_p3/selector.rb', line 44

def singular?
  true
end

#to_sObject



48
49
50
# File 'lib/json_p3/selector.rb', line 48

def to_s
  JSONP3.canonical_string(@name)
end