Module: Redcar::Sensitive

Included in:
Command
Defined in:
plugins/application/lib/application/sensitive.rb

Overview

Sensitive abstracts the concept of an object having an enabled/disabled property that is 'sensitive' to whether a number of other things are true. The “things that can be true” are Redcar::Sensitivitys, and the disabled, enabled property is only enabled when all the Sensitivitys are active.

For instance, if :is_tuesday is the name of a Redcar::Sensitivity that is only active when it is Tuesday, you can make an object sensitive to whether it is Tuesday by including this module and sensitizing the instance.

class MySensitiveClass
  include Redcar::Sensitive
end

obj = MySensitiveClass.new
obj.sensitize :is_tuesday
obj.active?  # => true|false

Instance Method Summary (collapse)

Instance Method Details

- (Boolean) active?

Whether all the sensitivities of this object are active.

Returns:

  • (Boolean)


53
54
55
# File 'plugins/application/lib/application/sensitive.rb', line 53

def active?
  sensitivities.all? {|s| s.active? }
end

- (Array<Sensitivity>) sensitivities

List of senstivities

Returns:



48
49
50
# File 'plugins/application/lib/application/sensitive.rb', line 48

def sensitivities
  sensitivity_names.map {|n| Sensitivity.get(n) }.compact
end

- (Array<Symbol>) sensitivity_names

List of senstivity names

Returns:

  • (Array<Symbol>)

    list of senstivity names



43
44
45
# File 'plugins/application/lib/application/sensitive.rb', line 43

def sensitivity_names
  @sensitivity_names || []
end

- (Object) sensitize(*sensitivity_names)

Sensitize the object to a list of sensitivities. May be called with multiple sensitivities multiple times.

obj.sensitize :is_tuesday, :raining
obj.sensitize :month_has_y

Parameters:

  • list (Array<Symbol>)

    of sensitivity_names



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'plugins/application/lib/application/sensitive.rb', line 28

def sensitize(*sensitivity_names)
  @sensitivity_names ||= []
  @sensitivity_names += sensitivity_names
  sensitivity_names.each do |sensitivity_name|
    Sensitivity.add_listener(Sensitivity.event_name(sensitivity_name)) do
      old_sensitivity = @current_sensitivity
      @current_sensitivity = active?
      if old_sensitivity != @current_sensitivity
        active_changed(@current_sensitivity)
      end
    end
  end
end