Sha256: efcf35f48be7358636e926f2e8f5da5fba6817889b32100c4e5ed1e55ef42385

Contents?: true

Size: 1.75 KB

Versions: 9

Compression:

Stored size: 1.75 KB

Contents

require 'erb'
require 'set'
require 'enumerator'

module Haml
  module Util
    class << self; include Haml::Util; end

    RUBY_VERSION = ::RUBY_VERSION.split(".").map {|s| s.to_i}

    def to_hash(arr)
      arr.compact.inject({}) {|h, (k, v)| h[k] = v; h}
    end

    def map_keys(hash)
      to_hash(hash.map {|k, v| [yield(k), v]})
    end

    def map_vals(hash)
      to_hash(hash.map {|k, v| [k, yield(v)]})
    end

    def map_hash(hash, &block)
      to_hash(hash.map(&block))
    end

    def powerset(arr)
      arr.inject([Set.new].to_set) do |powerset, el|
        new_powerset = Set.new
        powerset.each do |subset|
          new_powerset << subset
          new_powerset << subset + [el]
        end
        new_powerset
      end
    end

    def ruby1_8?
      Haml::Util::RUBY_VERSION[0] == 1 && Haml::Util::RUBY_VERSION[1] < 9
    end

    def has?(attr, klass, method)
      klass.send("#{attr}s").include?(ruby1_8? ? method.to_s : method.to_sym)
    end

    def enum_with_index(enum)
      ruby1_8? ? enum.enum_with_index : enum.each_with_index
    end

    class StaticConditionalContext
      def initialize(set)
        @set = set
      end

      def method_missing(name, *args, &block)
        super unless args.empty? && block.nil?
        @set.include?(name)
      end
    end

    def def_static_method(klass, name, args, *vars)
      erb = vars.pop
      powerset(vars).each do |set|
        context = StaticConditionalContext.new(set).instance_eval {binding}
        klass.class_eval(<<METHOD)
def #{static_method_name(name, *vars.map {|v| set.include?(v)})}(#{args.join(', ')})
  #{ERB.new(erb).result(context)}
end
METHOD
      end
    end

    def static_method_name(name, *vars)
      "#{name}_#{vars.map {|v| !!v}.join('_')}"
    end
  end
end

Version data entries

9 entries across 9 versions & 5 rubygems

Version Path
honkster-haml-2.1.0 lib/haml/util.rb
honkster-haml-2.1.1 lib/haml/util.rb
jwhitmire-haml-2.1.0.1 lib/haml/util.rb
jwhitmire-haml-2.1.0.2 lib/haml/util.rb
jwhitmire-haml-2.1.0.3 lib/haml/util.rb
wireframe-haml-2.1.0 lib/haml/util.rb
wireframe-haml-2.1.1 lib/haml/util.rb
merbjedi-haml-2.1.0 lib/haml/util.rb
spree-0.8.0 vendor/gems/haml-2.1.0/lib/haml/util.rb