Sha256: b3da8688c268e59bf1a7014cdb5cb1e0840ade35d13599dc19d3bd299d9ce55f

Contents?: true

Size: 1006 Bytes

Versions: 4

Compression:

Stored size: 1006 Bytes

Contents

require 'taketo/constructs'

module Taketo
  def self.downcased_construct_class_name(klass)
    klass.name.gsub("Taketo::Constructs::", "").gsub(/[A-Z][^A-Z]*/) { |s| s.gsub("::", "").downcase + "_" }.chop
  end

  class ConfigVisitor
    include Taketo::Constructs

    def self.visit(*klasses, &block)
      klasses.each do |klass|
        define_method(:"visit_#{Taketo.downcased_construct_class_name(klass)}", block)
      end
    end

    def visit(obj)
      obj.class.ancestors.each do |ancestor|
        next unless ancestor.name # skip anonymous classes
        method_name = :"visit_#{Taketo.downcased_construct_class_name(ancestor)}"
        next unless respond_to?(method_name)
        return send(method_name, obj)
      end
    end
  end

  def SimpleCollector(*types)
    Class.new(ConfigVisitor) do
      attr_reader :result

      def initialize
        @result = []
      end

      types.each do |t|
        visit t do |n|
          @result << n
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
taketo-0.1.3 lib/taketo/config_visitor.rb
taketo-0.1.2 lib/taketo/config_visitor.rb
taketo-0.1.1 lib/taketo/config_visitor.rb
taketo-0.1.0 lib/taketo/config_visitor.rb