Class: DynamicLocals::BaseTranslator
- Inherits:
-
Object
- Object
- DynamicLocals::BaseTranslator
- Defined in:
- lib/dynamic_locals/base_translator.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#known_locals ⇒ Object
readonly
Returns the value of attribute known_locals.
-
#locals_hash ⇒ Object
readonly
Returns the value of attribute locals_hash.
-
#lookup_strategy ⇒ Object
readonly
Returns the value of attribute lookup_strategy.
-
#original_src ⇒ Object
readonly
Returns the value of attribute original_src.
Instance Method Summary collapse
-
#initialize(src, locals_hash: :locals, lookup_strategy: :hash, known_locals: []) ⇒ BaseTranslator
constructor
A new instance of BaseTranslator.
-
#parameters ⇒ Object
The parameter list that pairs with the translated body.
-
#to_s(name = "__dynamic_locals__") ⇒ Object
Wrap the translated body in a method definition.
- #translate ⇒ Object
Constructor Details
#initialize(src, locals_hash: :locals, lookup_strategy: :hash, known_locals: []) ⇒ BaseTranslator
Returns a new instance of BaseTranslator.
5 6 7 8 9 10 |
# File 'lib/dynamic_locals/base_translator.rb', line 5 def initialize(src, locals_hash: :locals, lookup_strategy: :hash, known_locals: []) @original_src = src @locals_hash = locals_hash @lookup_strategy = lookup_strategy @known_locals = Array(known_locals).map(&:to_sym) end |
Instance Attribute Details
#known_locals ⇒ Object (readonly)
Returns the value of attribute known_locals.
3 4 5 |
# File 'lib/dynamic_locals/base_translator.rb', line 3 def known_locals @known_locals end |
#locals_hash ⇒ Object (readonly)
Returns the value of attribute locals_hash.
3 4 5 |
# File 'lib/dynamic_locals/base_translator.rb', line 3 def locals_hash @locals_hash end |
#lookup_strategy ⇒ Object (readonly)
Returns the value of attribute lookup_strategy.
3 4 5 |
# File 'lib/dynamic_locals/base_translator.rb', line 3 def lookup_strategy @lookup_strategy end |
#original_src ⇒ Object (readonly)
Returns the value of attribute original_src.
3 4 5 |
# File 'lib/dynamic_locals/base_translator.rb', line 3 def original_src @original_src end |
Instance Method Details
#parameters ⇒ Object
The parameter list that pairs with the translated body. The default shape takes the locals as a single hash argument; strategies that use real parameters (see RewriteTranslator's keyword strategy) override this.
19 20 21 |
# File 'lib/dynamic_locals/base_translator.rb', line 19 def parameters "#{locals_hash} = {}" end |
#to_s(name = "__dynamic_locals__") ⇒ Object
Wrap the translated body in a method definition.
24 25 26 |
# File 'lib/dynamic_locals/base_translator.rb', line 24 def to_s(name = "__dynamic_locals__") "def #{name}(#{parameters})\n#{translate}\nend" end |
#translate ⇒ Object
12 13 14 |
# File 'lib/dynamic_locals/base_translator.rb', line 12 def translate raise NotImplementedError end |