Sha256: c980531b33c0c66cf96fea7a842d2f747d4b016b31e2f530f5eb2cc8900b4950

Contents?: true

Size: 1.33 KB

Versions: 32

Compression:

Stored size: 1.33 KB

Contents

require 'opal/nodes/base'

module Opal
  module Nodes
    class HashNode < Base
      handle :hash

      def keys_and_values
        keys, values = [], []

        children.each_with_index do |obj, idx|
          if idx.even?
            keys << obj
          else
            values << obj
          end
        end

        [keys, values]
      end

      def simple_keys?(keys)
        keys.all? { |key| [:sym, :str].include? key.type }
      end

      def compile
        keys, values = keys_and_values

        if simple_keys? keys
          compile_hash2 keys, values
        else
          compile_hash
        end
      end

      def compile_hash
        helper :hash

        children.each_with_index do |child, idx|
          push ', ' unless idx == 0
          push expr(child)
        end

        wrap '$hash(', ')'
      end

      def compile_hash2(keys, values)
        hash_obj, hash_keys = {}, []
        helper :hash2

        keys.size.times do |idx|
          key = keys[idx][1].to_s.inspect
          hash_keys << key unless hash_obj.include? key
          hash_obj[key] = expr(values[idx])
        end

        hash_keys.each_with_index do |key, idx|
          push ', ' unless idx == 0
          push "#{key}: "
          push hash_obj[key]
        end

        wrap "$hash2([#{hash_keys.join ', '}], {", "})"
      end
    end
  end
end

Version data entries

32 entries across 32 versions & 3 rubygems

Version Path
opal-0.9.4 lib/opal/nodes/hash.rb
opal-0.9.3 lib/opal/nodes/hash.rb
opal-0.9.2 lib/opal/nodes/hash.rb
opal-0.9.0 lib/opal/nodes/hash.rb
opal-0.9.0.rc1 lib/opal/nodes/hash.rb
opal-0.9.0.beta2 lib/opal/nodes/hash.rb
opal-0.9.0.beta1 lib/opal/nodes/hash.rb
opal-0.8.1 lib/opal/nodes/hash.rb
opal-0.8.1.rc1 lib/opal/nodes/hash.rb
opal-wedge-0.9.0.dev lib/opal/nodes/hash.rb
opal-0.8.0 lib/opal/nodes/hash.rb
opal-0.8.0.rc3 lib/opal/nodes/hash.rb
opal-0.8.0.rc2 lib/opal/nodes/hash.rb
opal-0.8.0.rc1 lib/opal/nodes/hash.rb
opal-0.8.0.beta1 lib/opal/nodes/hash.rb
opal-0.7.2 lib/opal/nodes/hash.rb
opal-0.7.1 lib/opal/nodes/hash.rb
opal-0.7.0 lib/opal/nodes/hash.rb
opal-0.7.0.rc1 lib/opal/nodes/hash.rb
opal-0.7.0.beta3 lib/opal/nodes/hash.rb