Class: Chanko::Function

Inherits:
Object
  • Object
show all
Defined in:
lib/chanko/function.rb

Constant Summary collapse

THREAD_LOCAL_UNITS_KEY =
'Chanko::Function.units'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(unit, label, &block) ⇒ Function

Returns a new instance of Function.



17
18
19
20
21
# File 'lib/chanko/function.rb', line 17

def initialize(unit, label, &block)
  @unit  = unit
  @label = label
  @block = block
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



3
4
5
# File 'lib/chanko/function.rb', line 3

def block
  @block
end

#labelObject (readonly)

Returns the value of attribute label.



3
4
5
# File 'lib/chanko/function.rb', line 3

def label
  @label
end

#unitObject (readonly)

Returns the value of attribute unit.



3
4
5
# File 'lib/chanko/function.rb', line 3

def unit
  @unit
end

Class Method Details

.current_unitObject



12
13
14
# File 'lib/chanko/function.rb', line 12

def current_unit
  units.last
end

.unitsObject



8
9
10
# File 'lib/chanko/function.rb', line 8

def units
  Thread.current[THREAD_LOCAL_UNITS_KEY] ||= []
end

Instance Method Details

#css_classesObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/chanko/function.rb', line 35

def css_classes
  if Config.compatible_css_class
    %W[
      extension
      ext_#{unit.unit_name}
      ext_#{unit.unit_name}-#{label}
    ]
  else
    %W[
      unit
      unit__#{unit.unit_name}
      unit__#{unit.unit_name}__#{label}
    ]
  end
end

#invoke(context, options = {}) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/chanko/function.rb', line 23

def invoke(context, options = {})
  with_unit_stack(context) do
    with_unit_view_path(context) do
      capture_exception(context) do
        result = context.instance_eval(&block)
        result = decorate(result, context, options[:type]) if context.view? && result.present?
        result
      end
    end
  end
end