Sha256: 2dc8b2e210459f3873ad059ea2bc7d7c0a73624fa67024b1ad2d7a98579e0d39

Contents?: true

Size: 1.22 KB

Versions: 8

Compression:

Stored size: 1.22 KB

Contents

module Alf
  module Support

    # Converts `value` to a ruby literal
    #
    # This method is provided for code generation mechanisms used by Alf in
    # various places (such as .rash files). The to_ruby_literal contract is
    # such that the following invariant holds:
    #
    #     eval(to_ruby_literal(value)) == value
    #
    # This contract is ensured by Myrrha::ToRubyLiteral for various ruby values.
    # Myrrha delegates the job to `value.to_ruby_literal` provided this method
    # exists. In such case, the implementation must be such that the invariant
    # above is met.
    #
    # In every case, an invocation to this method only makes sense provided that
    # `value` denotes a pure value, with the obvious semantics (from TTM).
    #
    # @param [Object] value any ruby object that denotes a pure value.
    # @return [String] a ruby literal for `value`
    def to_ruby_literal(value)
      ToRubyLiteral.apply(value)
    end

    # Myrrha rules for converting to ruby literals
    ToRubyLiteral = Myrrha::ToRubyLiteral.dup.append do |g|
      g.coercion(Time)    {|s,_| "Time.parse('#{s.iso8601}')"     }
      g.coercion(DateTime){|s,_| "DateTime.parse('#{s.iso8601}')" }
    end

  end # module Support
end # module Alf

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
alf-core-0.16.3 lib/alf/support/to_ruby_literal.rb
alf-core-0.16.2 lib/alf/support/to_ruby_literal.rb
alf-core-0.16.1 lib/alf/support/to_ruby_literal.rb
alf-core-0.16.0 lib/alf/support/to_ruby_literal.rb
alf-core-0.15.0 lib/alf/support/to_ruby_literal.rb
alf-core-0.14.0 lib/alf-support/alf/support/to_ruby_literal.rb
alf-core-0.13.1 lib/alf-support/alf/support/to_ruby_literal.rb
alf-core-0.13.0 lib/alf-support/alf/support/to_ruby_literal.rb