Class: Analyst::Entities::Hash

Inherits:
Entity
  • Object
show all
Defined in:
lib/analyst/entities/hash.rb

Instance Attribute Summary

Attributes inherited from Entity

#ast, #parent

Instance Method Summary collapse

Methods inherited from Entity

#classes, #conditionals, #constant_assignments, #constants, #file_path, #full_name, handles_node, #hashes, #initialize, #inspect, #line_number, #location, #method_calls, #methods, #modules, process, #source, #strings, #top_level_classes, #top_level_constant_assignments, #top_level_constants, #top_level_modules, #variables

Constructor Details

This class inherits a constructor from Analyst::Entities::Entity

Instance Method Details

#pairsObject



8
9
10
# File 'lib/analyst/entities/hash.rb', line 8

def pairs
  @pairs ||= process_nodes(ast.children)
end

#to_hash(extract_values: true) ⇒ Object

Convenience method to turn this Entity into an actual ::Hash. If ‘extract_values` is true, then all keys and values that respond to `#value` will be replaced by the value they return from that call. Otherwise they’ll be left as Analyst::Entities::Entity objects.



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/analyst/entities/hash.rb', line 16

def to_hash(extract_values:true)
  pairs.inject({}) do |hash, pair|
    key = pair.key
    val = pair.value
    if extract_values
      key = key.value if key.respond_to?(:value)
      val = val.value if val.respond_to?(:value)
    end
    hash[key] = val
    hash
  end
end