Class: IronNails::View::Command

Inherits:
Object
  • Object
show all
Includes:
Core::Observable, Logging::ClassLogger
Defined in:
lib/ironnails/view/commands/command.rb

Overview

The base class for view commands in IronNails.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Core::Observable

#add_observer, #count_observers, #delete_observer, #delete_observers, #notify_observers

Methods included from Logging::ClassLogger

#log_on_error, #logger

Constructor Details

#initialize(options) ⇒ Command

Returns a new instance of Command.



32
33
34
# File 'lib/ironnails/view/commands/command.rb', line 32

def initialize(options)
  read_options options
end

Instance Attribute Details

#actionObject

the action that will be triggered



21
22
23
# File 'lib/ironnails/view/commands/command.rb', line 21

def action
  @action
end

#callbackObject

for asynchronous actions this gets executed after the view has been refreshed as a new command



30
31
32
# File 'lib/ironnails/view/commands/command.rb', line 30

def callback
  @callback
end

#conditionObject

the predicate that decides whether this command can execute or not



24
25
26
# File 'lib/ironnails/view/commands/command.rb', line 24

def condition
  @condition
end

#controllerObject

the name of the controller that built this command



27
28
29
# File 'lib/ironnails/view/commands/command.rb', line 27

def controller
  @controller
end

#modeObject

indicates whether to execute this command on the ui thread or on a different thread.



15
16
17
# File 'lib/ironnails/view/commands/command.rb', line 15

def mode
  @mode
end

#nameObject

the name of this command



18
19
20
# File 'lib/ironnails/view/commands/command.rb', line 18

def name
  @name
end

#viewObject

the view this command is bound to



12
13
14
# File 'lib/ironnails/view/commands/command.rb', line 12

def view
  @view
end

Instance Method Details

#<=>(command) ⇒ Object Also known as: compare_to



92
93
94
# File 'lib/ironnails/view/commands/command.rb', line 92

def <=>(command)
  self.name <=> command.name
end

#==(command) ⇒ Object Also known as: ===, equals



85
86
87
# File 'lib/ironnails/view/commands/command.rb', line 85

def ==(command)
  self.name == command.name
end

#asynchronous?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/ironnails/view/commands/command.rb', line 54

def asynchronous?
  mode == :asynchronous
end

#attached?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/ironnails/view/commands/command.rb', line 71

def attached?
  !view.nil?
end

#can_execute?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/ironnails/view/commands/command.rb', line 50

def can_execute?
  !!(condition.nil?||condition.call)
end

#changed?Boolean

flag to indicate whether this command needs a refresh in the view model

Returns:

  • (Boolean)


46
47
48
# File 'lib/ironnails/view/commands/command.rb', line 46

def changed?
  !!@changed
end

#executeObject

executes this command (it calls the action)



76
77
78
79
80
81
82
83
# File 'lib/ironnails/view/commands/command.rb', line 76

def execute
  #log_on_error do
  puts "calling the action"
  synchronise_viewmodel_with_controller
  action.call
  refresh_view unless asynchronous?
  #end if can_execute?
end

#has_callback?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/ironnails/view/commands/command.rb', line 58

def has_callback?
  !callback.nil?
end

#read_options(options) ⇒ Object

Raises:

  • (ArgumentError)


36
37
38
39
40
41
42
43
# File 'lib/ironnails/view/commands/command.rb', line 36

def read_options(options)
  raise ArgumentError.new("A name is necesary") if options[:name].nil?
  raise ArgumentError.new("An action is necesary") if options[:action].nil?
  options.each do |k, v|
    instance_variable_set "@#{k}", v
  end
  @mode ||= :synchronous
end

#refresh_viewObject



62
63
64
65
# File 'lib/ironnails/view/commands/command.rb', line 62

def refresh_view
  callback.call if asynchronous? && has_callback?
  notify_observers :refreshing_view, self
end

#synchronise_viewmodel_with_controllerObject



67
68
69
# File 'lib/ironnails/view/commands/command.rb', line 67

def synchronise_viewmodel_with_controller
  notify_observers :reading_input, self
end