Class: RubyMVC::Action

Inherits:
Object
  • Object
show all
Includes:
Toolkit::PropertyChangeNotifier
Defined in:
lib/ruby_mvc/controllers/action.rb

Overview

This class provides the ability to define discrete operations a la the Command pattern. Actions have keys, labels and a block that is called when they are activated. Additionally, they may be initialized with options that define when they are available.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Toolkit::SignalHandler::ClassMethods

#signal, #signals, #valid_signal!, #valid_signal?

Methods included from Toolkit::SignalHandler

#signal_connect, #signal_disconnect, #signal_emit

Constructor Details

#initialize(key, options = {}, &block) ⇒ Action

Returns a new instance of Action.



38
39
40
41
42
43
44
45
46
47
# File 'lib/ruby_mvc/controllers/action.rb', line 38

def initialize(key, options = {}, &block)
  @key = key.to_sym
  @options = options
  @block = block
  @sensitive = true
  @sensitive = false if !@options[:enable].nil?
  if !(x = options[:sensitive]).nil?
    @sensitive = x
  end
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



36
37
38
# File 'lib/ruby_mvc/controllers/action.rb', line 36

def key
  @key
end

Instance Method Details

#[](key) ⇒ Object

This method is used to access the options for this action.



52
53
54
# File 'lib/ruby_mvc/controllers/action.rb', line 52

def [](key)
  @options[key]
end

#call(*args) ⇒ Object



60
61
62
# File 'lib/ruby_mvc/controllers/action.rb', line 60

def call(*args)
  @block.call(*args) if @block
end

#labelObject



56
57
58
# File 'lib/ruby_mvc/controllers/action.rb', line 56

def label
  @options[:label] || @key.to_s.capitalize
end

#selection(sender, model, sel) ⇒ Object

This method is used to allow the action instances to determine internal state changes based on selection state changes in their view.



83
84
85
86
87
88
89
90
# File 'lib/ruby_mvc/controllers/action.rb', line 83

def selection(sender, model, sel)
  case @options[:enable]
  when :select_multi
    self.sensitive = sel.size >= 1
  when :select_single
    self.sensitive = sel.size == 1
  end
end

#sensitiveObject

This method may be overridden by action instances to have more control over when the action should be enabled or not.



75
76
77
# File 'lib/ruby_mvc/controllers/action.rb', line 75

def sensitive
  @sensitive
end

#sensitive=(val) ⇒ Object



64
65
66
67
68
69
# File 'lib/ruby_mvc/controllers/action.rb', line 64

def sensitive=(val)
  if val != @sensitive
    @sensitive = val
    property_changed(:sensitive, !val, val)
  end
end