Class: Platform::IEL::NodeUtil
- Inherits:
-
Object
- Object
- Platform::IEL::NodeUtil
- Defined in:
- lib/introhive_expression_language/iel/node_util.rb
Overview
Converts data from and to standard node graph formats.
Class Method Summary collapse
-
.from_native(value) ⇒ Object
Recursively convert the given native value to a node using standard representations.
-
.to_native(node) ⇒ Object
Recursively.
Class Method Details
.from_native(value) ⇒ Object
Recursively convert the given native value to a node using standard representations. The following types are converted: Hash => nested list e.g. [[a 1] [b 2]] String, Time (etc) => string_literal Fixnum, Float => numeric_literal, nan Array => list nil => nil Boolean => boolean
e.g.
- => 123, 5 => 6, [9, 8, 7.1, “hi”, false, [nil]]
-
is transformed to:
- [[“z” 123
- 5 6]
- 9 8 7.1 “hi” false [nil]]
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/introhive_expression_language/iel/node_util.rb', line 18 def self.from_native(value) if value.is_a? Hash Platform::IEL::SexpParser::Node.list([SexpParser::Node.symbol('assoc')] + value.map { |k, v| SexpParser::Node.list([from_native(k), from_native(v)]) }) elsif value.is_a? Array Platform::IEL::SexpParser::Node.list(value.map(&method(:from_native))) elsif value.is_a? Numeric Platform::IEL::SexpParser::Node.numeric_literal(value) elsif value.is_a? NilClass Platform::IEL::SexpParser::Node.nil elsif value.is_a? TrueClass Platform::IEL::SexpParser::Node.boolean(true) elsif value.is_a? FalseClass Platform::IEL::SexpParser::Node.boolean(false) elsif value.is_a? Symbol Platform::IEL::SexpParser::Node.symbol(value.to_s) else Platform::IEL::SexpParser::Node.string_literal(value.to_s) end end |
.to_native(node) ⇒ Object
Recursively
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/introhive_expression_language/iel/node_util.rb', line 39 def self.to_native(node) case node.kind when :string_literal, :numeric_literal, :boolean node.value when :symbol node.value.to_sym when :nil nil when :nan Float::NAN when :list if node.value.size > 0 && node.value[0].kind == :symbol && node.value[0].value == 'assoc' Hash[node.value.drop(1).map do |kv_pair| if kv_pair.kind == :list && kv_pair.value.size >= 2 [to_native(kv_pair.value[0]), to_native(kv_pair.value[1])] end end.compact] else node.value.map { |n| to_native(n) } end else raise ArgumentError, "Unable to convert kind #{node.kind} to native value" end end |