Class: Chanko::ActiveIf

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

Defined Under Namespace

Classes: Any, Block, NoConditionFound, None

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*conditions, &block) ⇒ ActiveIf

Returns a new instance of ActiveIf.



31
32
33
34
35
36
37
38
# File 'lib/chanko/active_if.rb', line 31

def initialize(*conditions, &block)
  @options    = conditions.extract_options!
  unless @options.empty?
    warn "options in ActiveIf#new are deprecated and are never used at #{caller[0]}"
  end
  @conditions = conditions
  @block      = block
end

Instance Attribute Details

#conditionsObject (readonly)

Returns the value of attribute conditions.



24
25
26
# File 'lib/chanko/active_if.rb', line 24

def conditions
  @conditions
end

Class Method Details

.clearObject



19
20
21
# File 'lib/chanko/active_if.rb', line 19

def clear
  definitions.clear
end

.define(label, &block) ⇒ Object



7
8
9
# File 'lib/chanko/active_if.rb', line 7

def define(label, &block)
  definitions[label] = block
end

.definitionsObject



15
16
17
# File 'lib/chanko/active_if.rb', line 15

def definitions
  @definitions ||= {}
end

.find(label) ⇒ Object



11
12
13
# File 'lib/chanko/active_if.rb', line 11

def find(label)
  definitions[label]
end

Instance Method Details

#active?(context, options = {}) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/chanko/active_if.rb', line 40

def active?(context, options = {})
  blocks.all? {|block| block.call(context, options) }
end

#blocksObject



44
45
46
47
48
49
50
# File 'lib/chanko/active_if.rb', line 44

def blocks
  @blocks ||= begin
    conditions.map do |condition|
      Block.new(condition)
    end << @block
  end.compact
end

#optionsObject

remains just for backward compatibility



26
27
28
29
# File 'lib/chanko/active_if.rb', line 26

def options # remains just for backward compatibility
  warn "ActiveIf#options is deprecated and is never used at #{caller[0]}"
  @options
end