Class: FlavourSaver::Helpers::Decorator

Inherits:
Defaults
  • Object
show all
Defined in:
lib/flavour_saver/helpers.rb

Instance Method Summary collapse

Methods inherited from Defaults

#each, #if, #log, #this, #unless, #with

Constructor Details

#initialize(locals, source) ⇒ Decorator

Returns a new instance of Decorator.



63
64
65
66
67
68
69
70
71
# File 'lib/flavour_saver/helpers.rb', line 63

def initialize(locals, source)
  @source = source
  mixin = Module.new do
    locals.each do |name,impl|
      define_method name, &impl
    end
  end
  extend(mixin)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &b) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/flavour_saver/helpers.rb', line 91

def method_missing(name,*args,&b)
  # I would rather have it raise a NameError, but Moustache
  # compatibility requires that missing helpers return
  # nothing. A good place for bugs to hide.
  #
  # SECURITY: public_send here is load-bearing, not stylistic. Do not
  # change it back to send.
  #
  # Private Kernel methods -- system, eval, exec, fork -- are deliberately
  # not refused by Runtime#forbidden_method?, because refusing them would
  # also refuse the ~60 context methods that share a name with one. They
  # are safe because they cannot be reached: public_send skips them, so
  # they arrive here, and this call skips them again.
  #
  # That makes this the only thing standing between a context that
  # overreports respond_to? -- a proxy or delegator answering true to
  # everything -- and arbitrary command execution. Reverting it to send
  # reopens {{system "..."}} on such a context; verified, not theorised.
  @source.public_send(name, *args, &b) if @source.respond_to? name
end

Instance Method Details

#[](accessor) ⇒ Object



77
78
79
80
81
82
83
84
85
# File 'lib/flavour_saver/helpers.rb', line 77

def [](accessor)
  return unless @source
  if array?
    if accessor.match?(/[0-9]+/)
      return @source.at(accessor.to_i)
    end
  end
  @source[accessor]
end

#array?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/flavour_saver/helpers.rb', line 73

def array?
  !!@source.is_a?(Array)
end

#respond_to?(name) ⇒ Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/flavour_saver/helpers.rb', line 87

def respond_to?(name)
  super || @source.respond_to?(name)
end