Class: Nyanko::ActiveIf

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

Defined Under Namespace

Classes: Any

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*conditions, &block) ⇒ ActiveIf

Returns a new instance of ActiveIf.



23
24
25
26
27
# File 'lib/nyanko/active_if.rb', line 23

def initialize(*conditions, &block)
  @options    = conditions.extract_options!
  @conditions = conditions
  @block      = block
end

Instance Attribute Details

#conditionsObject (readonly)

Returns the value of attribute conditions.



21
22
23
# File 'lib/nyanko/active_if.rb', line 21

def conditions
  @conditions
end

#optionsObject (readonly)

Returns the value of attribute options.



21
22
23
# File 'lib/nyanko/active_if.rb', line 21

def options
  @options
end

Class Method Details

.clearObject



16
17
18
# File 'lib/nyanko/active_if.rb', line 16

def clear
  definitions.clear
end

.define(label, &block) ⇒ Object



4
5
6
# File 'lib/nyanko/active_if.rb', line 4

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

.definitionsObject



12
13
14
# File 'lib/nyanko/active_if.rb', line 12

def definitions
  @definitions ||= {}
end

.find(label) ⇒ Object



8
9
10
# File 'lib/nyanko/active_if.rb', line 8

def find(label)
  definitions[label]
end

Instance Method Details

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

Returns:

  • (Boolean)


29
30
31
# File 'lib/nyanko/active_if.rb', line 29

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

#blocksObject



33
34
35
36
37
38
39
# File 'lib/nyanko/active_if.rb', line 33

def blocks
  @blocks ||= begin
    conditions.map do |condition|
      condition.is_a?(Any) ? condition.to_block : self.class.find(condition)
    end << @block
  end.compact
end