Class: JSONP3::Path::NameSelector

Inherits:
Selector
  • Object
show all
Defined in:
lib/json_p3/path/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

Methods inherited from Selector

#resolve_enum

Constructor Details

#initialize(env, token, name) ⇒ NameSelector

Returns a new instance of NameSelector.



36
37
38
39
# File 'lib/json_p3/path/selector.rb', line 36

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

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



34
35
36
# File 'lib/json_p3/path/selector.rb', line 34

def name
  @name
end

Instance Method Details

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



57
58
59
60
61
# File 'lib/json_p3/path/selector.rb', line 57

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

#hashObject



65
66
67
# File 'lib/json_p3/path/selector.rb', line 65

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

#resolve(node) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/json_p3/path/selector.rb', line 41

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)


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

def singular?
  true
end

#to_sObject



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

def to_s
  JSONP3::Path.canonical_string(@name)
end