Class: DynamicLocals::BaseTranslator

Inherits:
Object
  • Object
show all
Defined in:
lib/dynamic_locals/base_translator.rb

Direct Known Subclasses

EvalTranslator, RewriteTranslator

Instance Attribute Summary collapse

Instance Method Summary collapse

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_localsObject (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_hashObject (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_strategyObject (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_srcObject (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

#parametersObject

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

#translateObject

Raises:

  • (NotImplementedError)


12
13
14
# File 'lib/dynamic_locals/base_translator.rb', line 12

def translate
  raise NotImplementedError
end