Class: Nanoc::Core::Context
- Inherits:
-
Object
- Object
- Nanoc::Core::Context
- Defined in:
- lib/nanoc/core/context.rb
Overview
Provides a context and a binding for use in filters such as the ERB and Haml ones.
Direct Known Subclasses
Instance Method Summary collapse
-
#get_binding ⇒ Binding
Returns a binding for this instance.
- #include(mod) ⇒ Object
-
#initialize(hash) ⇒ Context
constructor
Creates a new context based off the contents of the hash.
-
#method_missing(method, *args) ⇒ Object
rubocop:enable Naming/AccessorMethodName.
- #respond_to_missing?(method, include_all) ⇒ Boolean
Constructor Details
#initialize(hash) ⇒ Context
Creates a new context based off the contents of the hash.
Each pair in the hash will be converted to an instance variable and an instance method. For example, passing the hash ‘{ :foo => ’bar’ }‘ will cause `@foo` to have the value `“bar”`, and the instance method `#foo` to return the same value `“bar”`.
27 28 29 30 31 |
# File 'lib/nanoc/core/context.rb', line 27 def initialize(hash) hash.each_pair do |key, value| instance_variable_set('@' + key.to_s, value) end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object
rubocop:enable Naming/AccessorMethodName
42 43 44 45 46 47 48 49 |
# File 'lib/nanoc/core/context.rb', line 42 def method_missing(method, *args, &) ivar_name = '@' + method.to_s if instance_variable_defined?(ivar_name) instance_variable_get(ivar_name) else super end end |
Instance Method Details
#get_binding ⇒ Binding
Returns a binding for this instance.
rubocop:disable Naming/AccessorMethodName
37 38 39 |
# File 'lib/nanoc/core/context.rb', line 37 def get_binding binding end |
#include(mod) ⇒ Object
64 65 66 67 |
# File 'lib/nanoc/core/context.rb', line 64 def include(mod) = class << self; self; end .instance_eval { include(mod) } end |
#respond_to_missing?(method, include_all) ⇒ Boolean
51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/nanoc/core/context.rb', line 51 def respond_to_missing?(method, include_all) ivar_name = '@' + method.to_s valid_ivar_name = if defined?(Contracts) ivar_name =~ /\A@[A-Za-z_]+\z/ else true # probably good enough end (valid_ivar_name && instance_variable_defined?(ivar_name)) || super end |