Sha256: a254c2a7ccc8dd6445199fb468f8d3172d6e3a23be9ad928aee3fe6a123678de

Contents?: true

Size: 1.09 KB

Versions: 11

Compression:

Stored size: 1.09 KB

Contents

# TODO: Tests
module Slate
  class Target
    def self.build(metric, &block)
      target = new(metric)
      yield target if block_given?
      target
    end

    def initialize(metric)
      @metric    = metric
      @functions = []
    end

    def to_s
      target = @metric
      @functions.each do |function|
        if function.is_a? Symbol
          target = %Q{#{function}(#{target})}
        else
          args = arguments(function.last).join(",")
          target = %Q{#{function.first}(#{target},#{args})}
        end
      end

      target
    end

    def add_function(*function)
      if function.size > 1
        arguments = function[1..-1]
        @functions << [function.first.to_sym, arguments]
      else
        @functions << function.first.to_sym
      end

      to_s
    end

    private

    def arguments(args=[])
      args.map do |arg|
        if arg.is_a?(Numeric)
          arg.to_s
        elsif arg.is_a?(Slate::Graph)
          arg.send(:target)
        elsif arg.is_a? Slate::Target
          arg.to_s
        else
          %Q{"#{arg}"}
        end
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
slate-1.0.3 lib/slate/target.rb
slate-1.0.2 lib/slate/target.rb
slate-1.0.1 lib/slate/target.rb
slate-1.0.0 lib/slate/target.rb
slate-0.2.1 lib/slate/target.rb
slate-0.2.0 lib/slate/target.rb
slate-0.1.0 lib/slate/target.rb
slate-0.0.5 lib/slate/target.rb
slate-0.0.4 lib/slate/target.rb
slate-0.0.3 lib/slate/target.rb
slate-0.0.2 lib/slate/target.rb