Class: Xhive::LiquidWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/xhive/liquid_wrapper.rb

Overview

Wrapper to add liquid_method to all of object attributes.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object) ⇒ LiquidWrapper

Returns a new instance of LiquidWrapper.



4
5
6
# File 'lib/xhive/liquid_wrapper.rb', line 4

def initialize(object)
  @object = object
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object

Delegate methods to wrapped object



39
40
41
# File 'lib/xhive/liquid_wrapper.rb', line 39

def method_missing(sym, *args, &block)
  @object.send sym, *args, &block
end

Class Method Details

.liquify(object) ⇒ Object

Public: builds a liquid wrapper around the object.

object - The object to wrap.

Returns: the same object (if it is already liquified) or a liquid wrapper.



34
35
36
# File 'lib/xhive/liquid_wrapper.rb', line 34

def self.liquify(object)
  object.respond_to?(:to_liquid) ? object : new(object)
end

.liquify_objects(objects) ⇒ Object

Public: builds wrapper around each collection object.

objects - The Hash containing the objects.

Returns: the objects hash with the objects wrappers.



22
23
24
25
26
# File 'lib/xhive/liquid_wrapper.rb', line 22

def self.liquify_objects(objects)
  objects.each do |k, v|
    objects[k] = liquify(v)
  end
end

Instance Method Details

#to_liquidObject

Public: liquid interface.

Returns: object attributes or an empty Array.



12
13
14
# File 'lib/xhive/liquid_wrapper.rb', line 12

def to_liquid
  @object.respond_to?(:attributes) ? @object.attributes : []
end