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.



49
50
51
52
53
54
55
56
57
# File 'lib/flavour_saver/helpers.rb', line 49

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



76
77
78
79
80
81
# File 'lib/flavour_saver/helpers.rb', line 76

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.
  @source.send(name, *args, &b) if @source.respond_to? name
end

Instance Method Details

#[](accessor) ⇒ Object



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

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

#array?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/flavour_saver/helpers.rb', line 59

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

#respond_to?(name) ⇒ Boolean

Returns:

  • (Boolean)


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

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