Sha256: b81b288922dd766ba64111a373794774853e6b5af5b06285fb70991d7bc88577

Contents?: true

Size: 1.21 KB

Versions: 3

Compression:

Stored size: 1.21 KB

Contents

# frozen_string_literal: true

class Code
  class Node
    class Dictionary < Node
      class KeyValue < Node
        def initialize(parsed)
          return if parsed.blank?
          if parsed.key?(:statement)
            @key = Node::Statement.new(parsed.delete(:statement).presence)
          elsif parsed.key?(:name)
            @key = Node::String.new([{ text: parsed.delete(:name).presence }])
          end

          if parsed[:value].presence
            @value = Node::Code.new(parsed.delete(:value).presence)
          end
        end

        def evaluate(**args)
          key = @key&.evaluate(**args) || Object::Nothing.new

          if @value
            value = @value.evaluate(**args)
            [key, value]
          else
            [key, key]
          end
        end
      end

      def initialize(parsed)
        return if parsed.blank?
        @key_values = parsed.presence || []
        @key_values.map! do |key_value|
          Node::Dictionary::KeyValue.new(key_value)
        end
      end

      def evaluate(**args)
        ::Code::Object::Dictionary.new(
          (@key_values || [])
            .map { |key_value| key_value.evaluate(**args) }
            .to_h
        )
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
code-ruby-0.13.1 lib/code/node/dictionary.rb
code-ruby-0.13.0 lib/code/node/dictionary.rb
code-ruby-0.12.0 lib/code/node/dictionary.rb