Module: Alf::Support
- Extended by:
- Support
- Included in:
- Support
- Defined in:
- lib/alf-support/alf/support.rb,
lib/alf-io/alf/support/csv_utils.rb,
lib/alf-support/alf/support/scope.rb,
lib/alf-support/alf/support/coerce.rb,
lib/alf-support/alf/support/config.rb,
lib/alf-support/alf/support/bindable.rb,
lib/alf-support/alf/support/registry.rb,
lib/alf-support/alf/support/to_lispy.rb,
lib/alf-support/alf/support/dot_utils.rb,
lib/alf-support/alf/support/ordered_set.rb,
lib/alf-support/alf/support/tuple_scope.rb,
lib/alf-support/alf/support/miscellaneous.rb,
lib/alf-support/alf/support/to_ruby_literal.rb
Overview
Provides tooling methods that are used here and there in Alf.
Defined Under Namespace
Modules: Bindable, CSVUtils, DotUtils, OrderedSet, Registry Classes: Config, Scope, TupleScope
Constant Summary collapse
- Coercions =
Defines all coercion rules, through Myrrha inheritance
Myrrha::Coerce.dup.append do |g| g.error_handler = lambda{|v,t,c| raise TypeError, "Unable to coerce `#{v.inspect}` to `#{t}`", caller } g.coercion String, Time, lambda{|s,t| Time.parse(s) } g.coercion String, DateTime, lambda{|s,t| DateTime.parse(s) } end
- ToLispy =
Myrrha rules for converting to ruby literals
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
- ToRubyLiteral =
Myrrha rules for converting to ruby literals
Myrrha::ToRubyLiteral.dup.append do |g| g.coercion(Time) {|s,_| "Time.parse('#{s.iso8601}')" } g.coercion(DateTime){|s,_| "DateTime.parse('#{s.iso8601}')" } end
Instance Method Summary collapse
-
#class_name(clazz) ⇒ Symbol
Returns the unqualified name of a ruby class or module.
-
#coerce(val, domain) ⇒ Object
Coerces a value to a particular domain.
-
#ruby_case(s) ⇒ String
Converts an unqualified class or module name to a ruby case method name.
-
#rubycase_name(name) ⇒ Symbol
Converts ‘name` to a rubycase name, as a Symbol.
-
#symbolize_keys(tuple) ⇒ Tuple
Symbolizes all keys of ‘tuple`.
-
#to_lispy(expr) ⇒ String
Converts ‘value` to a lispy expression.
-
#to_ruby_literal(value) ⇒ String
Converts ‘value` to a ruby literal.
-
#unsymbolize_keys(tuple) ⇒ Tuple
Unsymbolizes all keys of ‘tuple`.
Instance Method Details
#class_name(clazz) ⇒ Symbol
Returns the unqualified name of a ruby class or module.
Example
class_name(Alf::Support) -> :Support
12 13 14 |
# File 'lib/alf-support/alf/support/miscellaneous.rb', line 12 def class_name(clazz) /([A-Za-z0-9_]+)$/.match(clazz.name.to_s)[1].to_sym end |
#coerce(val, domain) ⇒ Object
Coerces a value to a particular domain.
Example:
Support.coerce("123", Integer) # => 123
23 24 25 |
# File 'lib/alf-support/alf/support/coerce.rb', line 23 def coerce(val, domain) Coercions.apply(val, domain) end |
#ruby_case(s) ⇒ String
Converts an unqualified class or module name to a ruby case method name.
Example
ruby_case(:Alf) -> "alf"
ruby_case(:HelloWorld) -> "hello_world"
25 26 27 |
# File 'lib/alf-support/alf/support/miscellaneous.rb', line 25 def ruby_case(s) s.to_s.gsub(/[A-Z]/){|x| "_#{x.downcase}"}[1..-1] end |
#rubycase_name(name) ⇒ Symbol
Converts ‘name` to a rubycase name, as a Symbol.
Example:
rubycase_name(Alf) => :alf
rubycase_name(HelloWorld) => :hello_world
37 38 39 40 41 |
# File 'lib/alf-support/alf/support/miscellaneous.rb', line 37 def rubycase_name(name) name = class_name(name) if name.is_a?(Module) name = ruby_case(name.to_s) name.to_sym end |
#symbolize_keys(tuple) ⇒ Tuple
Symbolizes all keys of ‘tuple`
47 48 49 |
# File 'lib/alf-support/alf/support/miscellaneous.rb', line 47 def symbolize_keys(tuple) tuple.each_with_object({}){|(k,v),h| h[k.to_sym] = v} end |
#to_lispy(expr) ⇒ String
Converts ‘value` to a lispy expression.
Example:
expr = Alf.examples.compile{
project(:suppliers, [:name])
}
Support.to_lispy(expr)
# => project(:suppliers, [:name])
16 17 18 |
# File 'lib/alf-support/alf/support/to_lispy.rb', line 16 def to_lispy(expr) ToLispy.apply(expr) end |
#to_ruby_literal(value) ⇒ String
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).
22 23 24 |
# File 'lib/alf-support/alf/support/to_ruby_literal.rb', line 22 def to_ruby_literal(value) ToRubyLiteral.apply(value) end |
#unsymbolize_keys(tuple) ⇒ Tuple
Unsymbolizes all keys of ‘tuple`
55 56 57 |
# File 'lib/alf-support/alf/support/miscellaneous.rb', line 55 def unsymbolize_keys(tuple) tuple.each_with_object({}){|(k,v),h| h[k.to_s] = v} end |