Class: Slim::Wrapper
- Inherits:
-
Object
- Object
- Slim::Wrapper
- Defined in:
- lib/slim/wrapper.rb
Overview
For logic-less mode, objects can be encased in the Wrapper class.
For Rails, this allows us to use the environment provided for rendering a view including the instance variables, view helper and application helper methods.
Instance Attribute Summary collapse
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Instance Method Summary collapse
-
#[](name) ⇒ Object
To find the reference, first check for standard method access by using respond_to?.
-
#empty? ⇒ Boolean
Empty objects must appear empty for inverted sections.
-
#initialize(value, parent = nil) ⇒ Wrapper
constructor
A new instance of Wrapper.
-
#to_s ⇒ Object
Used for output.
Constructor Details
#initialize(value, parent = nil) ⇒ Wrapper
Returns a new instance of Wrapper.
10 11 12 |
# File 'lib/slim/wrapper.rb', line 10 def initialize(value, parent = nil) @value, @parent = value, parent end |
Instance Attribute Details
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
8 9 10 |
# File 'lib/slim/wrapper.rb', line 8 def parent @parent end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
8 9 10 |
# File 'lib/slim/wrapper.rb', line 8 def value @value end |
Instance Method Details
#[](name) ⇒ Object
To find the reference, first check for standard method access by using respond_to?.
If not found, check to see if the value is a hash and if the the name is a key on the hash.
Not a hash, or not a key on the hash, then check to see if there is an instance variable with the name.
If the instance variable doesn’t exist and there is a parent object, go through the same steps on the parent object. This is useful when you are iterating over objects.
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/slim/wrapper.rb', line 26 def [](name) if value.respond_to?(name) wrap value.send(name) elsif value.respond_to?(:has_key?) && value.has_key?(name.to_sym) wrap value[name] elsif value.instance_variable_defined?("@#{name}") wrap value.instance_variable_get("@#{name}") elsif parent parent[name] end end |
#empty? ⇒ Boolean
Empty objects must appear empty for inverted sections
39 40 41 |
# File 'lib/slim/wrapper.rb', line 39 def empty? value.respond_to?(:empty) && value.empty? end |
#to_s ⇒ Object
Used for output
44 45 46 |
# File 'lib/slim/wrapper.rb', line 44 def to_s value.to_s end |