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



90
91
92
93
94
95
# File 'lib/flavour_saver/helpers.rb', line 90

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



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

def [](accessor)
  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)


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

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