Sha256: 4414a9aece58cc254f0a0dcdc8f0ec0c0f64c3c1314b7c80e54ba7dc3e5d83a3

Contents?: true

Size: 1.05 KB

Versions: 5

Compression:

Stored size: 1.05 KB

Contents

module Alf
  module Support

    NOT_PROVIDED = Object.new.freeze

    # Converts `value` to a lispy expression.
    #
    # Example:
    #
    #     expr = Alf.examples.compile{
    #       project(:suppliers, [:name])
    #     }
    #     Support.to_lispy(expr)
    #     # => project(:suppliers, [:name])
    #
    # @param [Object] expr any ruby object denoting a lispy expression
    # @return [String] a lispy expression for `value`
    def to_lispy(expr, default = NOT_PROVIDED)
      ToLispy.apply(expr)
    rescue NotSupportedError
      raise if default == NOT_PROVIDED
      default
    end

    # Myrrha rules for converting to ruby literals
    ToLispy = Myrrha::coercions do |r|

      # Delegate to #to_lispy if it exists
      lispy_able = lambda{|v,rd| v.respond_to?(:to_lispy)}
      r.upon(lispy_able) do |v,rd|
        v.to_lispy
      end

      # Let's assume to to_ruby_literal will make the job
      r.fallback(Object) do |v, _|
        Support.to_ruby_literal(v) rescue v.inspect
      end

    end # ToLispy

  end # module Support
end # module Alf

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
alf-core-0.16.3 lib/alf/support/to_lispy.rb
alf-core-0.16.2 lib/alf/support/to_lispy.rb
alf-core-0.16.1 lib/alf/support/to_lispy.rb
alf-core-0.16.0 lib/alf/support/to_lispy.rb
alf-core-0.15.0 lib/alf/support/to_lispy.rb