Class: Nyanko::ActiveIf
- Inherits:
-
Object
- Object
- Nyanko::ActiveIf
- Defined in:
- lib/nyanko/active_if.rb
Defined Under Namespace
Classes: Any
Instance Attribute Summary collapse
-
#conditions ⇒ Object
readonly
Returns the value of attribute conditions.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Class Method Summary collapse
Instance Method Summary collapse
- #active?(context, options = {}) ⇒ Boolean
- #blocks ⇒ Object
-
#initialize(*conditions, &block) ⇒ ActiveIf
constructor
A new instance of ActiveIf.
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. @conditions = conditions @block = block end |
Instance Attribute Details
#conditions ⇒ Object (readonly)
Returns the value of attribute conditions.
21 22 23 |
# File 'lib/nyanko/active_if.rb', line 21 def conditions @conditions end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
21 22 23 |
# File 'lib/nyanko/active_if.rb', line 21 def @options end |
Class Method Details
.clear ⇒ Object
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 |
.definitions ⇒ Object
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
29 30 31 |
# File 'lib/nyanko/active_if.rb', line 29 def active?(context, = {}) blocks.all? {|block| block.call(context, ) } end |
#blocks ⇒ Object
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 |