Sha256: 9ea50911a8f8fea5b7df8c02026dab8c8b2655ca4cb99e4a9a897f48d3963389

Contents?: true

Size: 878 Bytes

Versions: 7

Compression:

Stored size: 878 Bytes

Contents

module Analyst

  module Entities
    class Hash < Entity

      handles_node :hash

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

      # 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.
      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

      private

      def contents
        pairs
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
analyst-1.2.4 lib/analyst/entities/hash.rb
analyst-1.2.3 lib/analyst/entities/hash.rb
analyst-1.2.2 lib/analyst/entities/hash.rb
analyst-1.2.1 lib/analyst/entities/hash.rb
analyst-1.2.0 lib/analyst/entities/hash.rb
analyst-1.0.1 lib/analyst/entities/hash.rb
analyst-1.0.0 lib/analyst/entities/hash.rb