Class: DirectoryTemplate::ErbTemplate::Variables
- Inherits:
-
BlankSlate
- Object
- BlankSlate
- DirectoryTemplate::ErbTemplate::Variables
- 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.
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
-
#__binding__ ⇒ Binding
Make the binding publicly available.
-
#__keys__(include_delegate = true) ⇒ 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.
-
#initialize(delegate = nil, variables = {}, on_error_name = nil) {|exception| ... } ⇒ Variables
constructor
A new instance of Variables.
-
#inspect ⇒ Object
See Object#inspect.
-
#method_missing(m, *args) ⇒ Object
Set or get the value associated with the key matching the method name.
- #respond_to_missing?(key) ⇒ Boolean
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.
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.
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.
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 |
#inspect ⇒ Object
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
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 |