Sha256: acebc6fde2cb3eab3b77d95f65c48adf94fd81288210026279f6a75088036076

Contents?: true

Size: 1.24 KB

Versions: 1

Compression:

Stored size: 1.24 KB

Contents

module Alf
  class Aggregator
    module ClassMethods
      
      #
      # Returns registered aggregators as an Array of classes
      #
      def aggregators
        @aggregators ||= []
      end

      #
      # Yields aggregator classes in turn
      # 
      def each
        aggregators.each(&Proc.new)
      end

      #
      # Automatically installs factory methods for inherited classes.
      #
      # Example: 
      #   class Sum < Aggregate   # will give a method Aggregator.sum
      #     ...
      #   end
      #   Aggregator.sum{ size }
      # 
      def inherited(clazz)
        aggregators << clazz
        basename = Tools.ruby_case(Tools.class_name(clazz))
        instance_eval <<-EOF
          def #{basename}(*args, &block)
            #{clazz}.new(*args, &block)
          end
        EOF
      end
  
      #
      # Coerces `arg` as an Aggregator
      #
      def coerce(arg)
        case arg
        when Aggregator
          arg
        when String
          agg = instance_eval(arg)
          agg.source = arg
          agg
        else
          raise ArgumentError, "Invalid arg `arg` for Aggregator()"
        end
      end
        
    end # module ClassMethods
    extend(ClassMethods)
  end # class Aggregator
end # module Alf

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
alf-0.10.0 lib/alf/aggregator/class_methods.rb