Module: Postsvg::Visitors::PsVisitor::Common
- Included in:
- Postsvg::Visitors::PsVisitor
- Defined in:
- lib/postsvg/visitors/ps_visitor/common.rb
Overview
Helpers shared across multiple visitor modules. Mixed into
PsVisitor so every module sees the same helpers without
re-defining them. DRY: previously normalize_key existed
in both Dictionary and Container.
Instance Method Summary collapse
- #lookup_dict(name) ⇒ Object
-
#normalize_key(key) ⇒ Object
Convert a key from any literal-typed value to the canonical string form used in dictionary lookups.
-
#numeric_value(value) ⇒ Object
Resolve a value to a number, looking up names in the dict stack when possible.
-
#string_value(node) ⇒ Object
Coerce a runtime value to its underlying string content (handles StringLiteral / HexLiteral / plain String).
-
#truthy?(value) ⇒ Boolean
PS truthiness: false, nil, and 0 are falsy.
Instance Method Details
#lookup_dict(name) ⇒ Object
46 47 48 49 50 51 |
# File 'lib/postsvg/visitors/ps_visitor/common.rb', line 46 def lookup_dict(name) @dict_stack.reverse_each do |dict| return dict[name] if dict.key?(name) end nil end |
#normalize_key(key) ⇒ Object
Convert a key from any literal-typed value to the canonical string form used in dictionary lookups.
13 14 15 16 17 18 19 |
# File 'lib/postsvg/visitors/ps_visitor/common.rb', line 13 def normalize_key(key) case key when Model::Literals::Name then key.value when String then key.to_s.sub(/\A\//, "") else key.to_s end end |
#numeric_value(value) ⇒ Object
Resolve a value to a number, looking up names in the dict stack when possible. Falls back to 0 for unknown values.
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/postsvg/visitors/ps_visitor/common.rb', line 34 def numeric_value(value) case value when Numeric then value when Model::Literals::Number then value.value when Model::Literals::Name entry = lookup_dict(value.value) entry ? numeric_value(entry) : 0 when Model::Computed then 0 else 0 end end |
#string_value(node) ⇒ Object
Coerce a runtime value to its underlying string content (handles StringLiteral / HexLiteral / plain String).
23 24 25 26 27 28 29 30 |
# File 'lib/postsvg/visitors/ps_visitor/common.rb', line 23 def string_value(node) case node when String then node when Model::Literals::StringLiteral then node.value when Model::Literals::HexLiteral then node.bytes else nil end end |
#truthy?(value) ⇒ Boolean
PS truthiness: false, nil, and 0 are falsy. Everything else (including the integer 1 and any non-zero Numeric, plus any object) is truthy.
56 57 58 59 60 61 62 |
# File 'lib/postsvg/visitors/ps_visitor/common.rb', line 56 def truthy?(value) return false if [false, nil].include?(value) return value != 0 if value.is_a?(Numeric) true end |