Sha256: 7911271f909776de2da130ebf5527e2ef8f2df38c126bbf026a90123a172ac2f

Contents?: true

Size: 1.38 KB

Versions: 1

Compression:

Stored size: 1.38 KB

Contents

# encoding: utf-8

class AbstractMapper

  # Base class for nodes optimization rules
  #
  # @abstract
  #
  # @api private
  #
  class Rule

    # @private
    def self.composer
      :identity
    end
    private_class_method :composer

    # The transformation function that applies the rule to the array of nodes
    #
    # @return [Transproc::Function]
    #
    def self.transproc
      Functions[composer, proc { |*nodes| new(*nodes).call }]
    end

    # @!attribute [r] nodes
    #
    # @return [Array<AbstractMapper::Node>]
    #   Either one or two nodes to be optimized
    #
    attr_reader :nodes

    # @!scope class
    # @!method new(*nodes)
    # Creates the rule for a sole node, or a pair of consecutive nodes
    #
    # @param [Array<AbstractMapper::Node>] nodes
    #
    # @return [AbstractMapper::Rule]

    # @private
    def initialize(*nodes)
      @nodes = nodes
      IceNine.deep_freeze(self)
    end

    # Checks if optimization is needed for the node(s)
    #
    # @return [Boolean]
    #
    def optimize?
    end

    # Returns the optimized node(s)
    #
    # @return [Object]
    #
    def optimize
      nodes
    end

    # Returns the result of the rule applied to the initialized [#nodes]
    #
    # @return [Array<AbstractMapper::Node>]
    #
    def call
      optimize? ? [optimize].flatten.compact : nodes
    end

  end # class Rule

end # class AbstractMapper

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
abstract_mapper-0.0.2 lib/abstract_mapper/rule.rb