Class: DirectoryTemplate::ErbTemplate::Variables

Inherits:
BlankSlate show all
Defined in:
lib/directory_template/erb_template.rb

Overview

Variables is similar to OpenStruct, but slightly optimized for create once, use once and giving diagnostics on exceptions/missing keys.

Examples:

variables = Variables.new(delegator, :x => "Value of X") { |exception|
  do_something_with(exception)
}
variables.x # => "Value of X"

Author:

Constant Summary collapse

Raiser =

A proc for &on_error in DirectoryTemplate::ErbTemplate::Variables::new or DirectoryTemplate::ErbTemplate#result. Raises the error further on.

proc { |e|
  raise(e)
}
Teller =

A proc for &on_error in DirectoryTemplate::ErbTemplate::Variables.new or DirectoryTemplate::ErbTemplate#result. Inserts <<error_class: error_message>> in the place where the error occurred.

proc { |e|
  "<<#{e.class}: #{e}>>"
}
EmptyHash =

An empty Hash

{}.freeze
SetterPattern =

Regex to match setter method names

/=\z/.freeze

Instance Method Summary collapse

Methods inherited from BlankSlate

find_hidden_method, hide, reveal

Constructor Details

#initialize(delegate = nil, variables = {}, on_error_name = nil) {|exception| ... } ⇒ Variables

Returns a new instance of Variables.

Parameters:

  • delegate (Object) (defaults to: nil)

    All method calls and undefined variables are delegated to this object as method call.

  • variables (Hash<Symbol,Object>) (defaults to: {})

    A hash with variables in it, keys must be Symbols.

  • on_error_name (Symbol) (defaults to: nil)

    Instead of a block you can pass the name of an existing handler, e.g. :Raiser or :Teller.

Yields:

  • (exception)

    description The block is yielded in case of an exception with the exception as argument.



69
70
71
72
73
74
75
76
77
# File 'lib/directory_template/erb_template.rb', line 69

def initialize(delegate=nil, variables={}, on_error_name=nil, &on_error)
  @delegate = delegate
  @table    = (@delegate ? Hash.new { |h,k| @delegate.send(k) } : EmptyHash).merge(variables)
  if !on_error && on_error_name then
    @on_error = self.class.const_get(on_error_name)
  else
    @on_error = on_error || Raiser
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args) ⇒ Object

Set or get the value associated with the key matching the method name.



100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/directory_template/erb_template.rb', line 100

def method_missing(m, *args) # :nodoc:
  argn = args.length
  if argn.zero? && @table.has_key?(m) then
    @table[m]
  elsif argn == 1 && m.to_s =~ SetterPattern
    @table[m] = args.first
  elsif @delegate
    @delegate.send(m, *args)
  end
rescue => e
  @on_error.call(e)
end

Instance Method Details

#__binding__Binding

Returns Make the binding publicly available.

Returns:

  • (Binding)

    Make the binding publicly available



88
89
90
# File 'lib/directory_template/erb_template.rb', line 88

def __binding__
  binding
end

#__keys__(include_delegate = true) ⇒ Array<Symbol>

Returns All keys this Variables instance provides, if the include_delegate argument is true and the object to delegate to responds to __keys__, then it will add the keys of the delegate.

Returns:

  • (Array<Symbol>)

    All keys this Variables instance provides, if the include_delegate argument is true and the object to delegate to responds to __keys__, then it will add the keys of the delegate.



83
84
85
# File 'lib/directory_template/erb_template.rb', line 83

def __keys__(include_delegate=true)
  @table.keys + ((include_delegate && @delegate.respond_to?(:__keys__)) ? @delegate.__keys__ : [])
end

#inspectObject

See Object#inspect



115
116
117
118
119
120
121
# File 'lib/directory_template/erb_template.rb', line 115

def inspect # :nodoc:
  sprintf "#<%s:0x%08x @delegate=%s %s>",
    self.class,
    __id__,
    @table.map { |k,v| "#{k}=#{v.inspect}" }.join(', '),
    @delegate ? "#<%s:0x%08x ...>" %  [@delegate.class, @delegate.object_id << 1] : "nil"
end

#respond_to_missing?(key) ⇒ Boolean

Returns:

  • (Boolean)

See Also:

  • Object#respond_to_missing?


94
95
96
# File 'lib/directory_template/erb_template.rb', line 94

def respond_to_missing?(key)
  @table.respond_to?(key) || (@delegate && @delegate.respond_to?(key))
end