Class: ConditionalMessages::Rule

Inherits:
Object
  • Object
show all
Defined in:
lib/conditional_messages/rule.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, &condition) ⇒ Rule



7
8
9
10
# File 'lib/conditional_messages/rule.rb', line 7

def initialize(options = {}, &condition)
  @options   = options
  @condition = condition || raise(UndefinedRuleError)
end

Instance Attribute Details

#conditionObject (readonly)

Returns the value of attribute condition.



5
6
7
# File 'lib/conditional_messages/rule.rb', line 5

def condition
  @condition
end

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/conditional_messages/rule.rb', line 5

def options
  @options
end

Instance Method Details

#apply(context) ⇒ Object



24
25
26
# File 'lib/conditional_messages/rule.rb', line 24

def apply(context)
  context.instance_eval(&condition)
end

#default_pointsObject



20
21
22
# File 'lib/conditional_messages/rule.rb', line 20

def default_points
  required? ? 100 : 10
end

#pointsObject



16
17
18
# File 'lib/conditional_messages/rule.rb', line 16

def points
  options.fetch(:points) { default_points }
end

#required?Boolean



12
13
14
# File 'lib/conditional_messages/rule.rb', line 12

def required?
  options.fetch(:required) { false }
end