Class: Delorean::Parameter

Inherits:
SNode
  • Object
show all
Defined in:
lib/delorean/nodes.rb

Direct Known Subclasses

BlockParameter, ParameterDefault

Instance Method Summary collapse

Instance Method Details

#check(context) ⇒ Object



18
19
20
# File 'lib/delorean/nodes.rb', line 18

def check(context, *)
  context.parse_define_param(i.text_value, [])
end

#rewrite(context) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/delorean/nodes.rb', line 22

def rewrite(context)
  # Adds a parameter to the current node.  Parameters are
  # implemented as functions (just like attrs).  The environment
  # arg (_e) is a Hash.  To find a param (aname) in node (cname),
  # we first check to see if cname.aname has already been computed
  # in _e.  If not, to compute it we check for the value in _e
  # (i.e. check for aname).  Otherwise, we use the default value
  # if any.
  aname = i.text_value
  cname = context.last_node
  not_found = defined?(e) ? e.rewrite(context) :
    "raise UndefinedParamError, 'undefined parameter #{aname}'"

  <<eos
  class #{cname}
    def self.#{aname}#{POST}(_e)
        _e[self.name+'.#{aname}'] ||= _e.fetch('#{aname}') { #{not_found} }
    end
  end
eos
end