Sha256: 61f927780c79a150aff4e9ac0df884ac9ddf8c259388b84d925ab562893cd7f1

Contents?: true

Size: 1.49 KB

Versions: 5

Compression:

Stored size: 1.49 KB

Contents

module Alf
  class Predicate
    class ToRubyCode < Sexpr::Processor

      def on_tautology(sexpr)
        "true"
      end

      def on_contradiction(sexpr)
        "false"
      end

      def on_qualified_identifier(sexpr)
        "#{sexpr.qualifier}.#{sexpr.name}"
      end

      def on_identifier(sexpr)
        if s = options[:scope]
          "#{s}.#{sexpr.last.to_s}"
        else
          sexpr.last.to_s
        end
      end

      def on_not(sexpr)
        "#{sexpr.operator_symbol}" << apply(sexpr.last, sexpr)
      end

      def on_nadic_bool(sexpr)
        sexpr.sexpr_body.map{|term|
          apply(term, sexpr)
        }.join(" #{sexpr.operator_symbol} ")
      end
      alias :on_and :on_nadic_bool
      alias :on_or  :on_nadic_bool

      def on_dyadic(sexpr)
        sexpr.sexpr_body.map{|term|
          apply(term, sexpr)
        }.join(" #{sexpr.operator_symbol} ")
      end
      alias :on_eq  :on_dyadic
      alias :on_neq :on_dyadic
      alias :on_lt  :on_dyadic
      alias :on_lte :on_dyadic
      alias :on_gt  :on_dyadic
      alias :on_gte :on_dyadic

      def on_in(sexpr)
        "#{Support.to_ruby_literal(sexpr.values)}.include?(#{apply(sexpr.identifier)})"
      end

      def on_literal(sexpr)
        Support.to_ruby_literal(sexpr.last)
      end

    protected

      def apply(sexpr, parent = nil)
        code = super(sexpr)
        if parent && (parent.priority >= sexpr.priority)
          code = "(" << code << ")"
        end
        code
      end

    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
alf-core-0.16.3 lib/alf/predicate/processors/to_ruby_code.rb
alf-core-0.16.2 lib/alf/predicate/processors/to_ruby_code.rb
alf-core-0.16.1 lib/alf/predicate/processors/to_ruby_code.rb
alf-core-0.16.0 lib/alf/predicate/processors/to_ruby_code.rb
alf-core-0.15.0 lib/alf/predicate/processors/to_ruby_code.rb