Class: Gem::Commands::NewCommand::ErbTemplate::Variables

Inherits:
BlankSlate show all
Defined in:
lib/gem/commands/new_command/erb_template.rb

Overview

Indexing

Author: Stefan Rusterholz Contact: [email protected] Version: 0.1.0

About

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

Synopsis

tmpl = Variables.new(delegator, :variable => "content") { |exception|
  do_something_with(exception)
}

Constant Summary collapse

Raiser =

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

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

A proc for &on_error in SilverPlatter::Variables.new or SilverPlatter::Templater#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, &on_error) ⇒ Variables

Arguments

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

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

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

  • &on_error: The block is yielded in case of an exception with the exception as argument



63
64
65
66
67
68
69
70
71
# File 'lib/gem/commands/new_command/erb_template.rb', line 63

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

:nodoc:



88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/gem/commands/new_command/erb_template.rb', line 88

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__Object



79
80
81
# File 'lib/gem/commands/new_command/erb_template.rb', line 79

def __binding__
  binding
end

#__keys__(include_delegate = true) ⇒ Object

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.



75
76
77
# File 'lib/gem/commands/new_command/erb_template.rb', line 75

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

#inspectObject

:nodoc:



101
102
103
104
105
106
107
# File 'lib/gem/commands/new_command/erb_template.rb', line 101

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?(key) ⇒ Boolean

See Object#respond_to?

Returns:

  • (Boolean)


84
85
86
# File 'lib/gem/commands/new_command/erb_template.rb', line 84

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