Class: Glimmer::Calculator::Command

Inherits:
Object
  • Object
show all
Includes:
EasilyTypable
Defined in:
lib/models/glimmer/calculator/command.rb,
lib/models/glimmer/calculator/command/point.rb,
lib/models/glimmer/calculator/command/equals.rb,
lib/models/glimmer/calculator/command/number.rb,
lib/models/glimmer/calculator/command/all_clear.rb,
lib/models/glimmer/calculator/command/operation.rb,
lib/models/glimmer/calculator/command/operation/add.rb,
lib/models/glimmer/calculator/command/operation/divide.rb,
lib/models/glimmer/calculator/command/operation/multiply.rb,
lib/models/glimmer/calculator/command/operation/subtract.rb

Direct Known Subclasses

AllClear, Equals, Number, Operation, Point

Defined Under Namespace

Classes: AllClear, Equals, Number, Operation, Point

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(button) ⇒ Command

Returns a new instance of Command.



42
43
44
# File 'lib/models/glimmer/calculator/command.rb', line 42

def initialize(button)
  @button = button
end

Class Attribute Details

.number1Object

Returns the value of attribute number1.



6
7
8
# File 'lib/models/glimmer/calculator/command.rb', line 6

def number1
  @number1
end

.number2Object

Returns the value of attribute number2.



6
7
8
# File 'lib/models/glimmer/calculator/command.rb', line 6

def number2
  @number2
end

.operationObject

Returns the value of attribute operation.



6
7
8
# File 'lib/models/glimmer/calculator/command.rb', line 6

def operation
  @operation
end

Instance Attribute Details

#buttonObject (readonly)

Returns the value of attribute button.



39
40
41
# File 'lib/models/glimmer/calculator/command.rb', line 39

def button
  @button
end

#resultObject

Returns the value of attribute result.



40
41
42
# File 'lib/models/glimmer/calculator/command.rb', line 40

def result
  @result
end

Class Method Details

.command_historyObject



26
27
28
# File 'lib/models/glimmer/calculator/command.rb', line 26

def command_history
  @command_history ||= []
end

.for(button) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/models/glimmer/calculator/command.rb', line 30

def for(button)
  command_class = keyword_to_command_class_mapping[button]
  command_class&.new(button)&.tap do |command|
    command.execute
    command_history << command
  end
end

.keyword(keyword_text) ⇒ Object

Keyword string representing calculator command (e.g. ‘+’ for Add command) Subclasses must call to define a single keyword



10
11
12
# File 'lib/models/glimmer/calculator/command.rb', line 10

def keyword(keyword_text)         
  Command.keyword_to_command_class_mapping[keyword_text] = self
end

.keyword_to_command_class_mappingObject



22
23
24
# File 'lib/models/glimmer/calculator/command.rb', line 22

def keyword_to_command_class_mapping
  @keyword_to_command_class_mapping ||= {}
end

.keywords(*keyword_text_array) ⇒ Object

Keyword string array representing calculator command (e.g. (‘0’..‘9’).to_a) Subclasses must call to define multiple keywords



16
17
18
19
20
# File 'lib/models/glimmer/calculator/command.rb', line 16

def keywords(*keyword_text_array)
  keyword_text_array.flatten.each do |keyword_text|
    keyword(keyword_text)
  end
end

Instance Method Details

#command_historyObject



78
79
80
# File 'lib/models/glimmer/calculator/command.rb', line 78

def command_history
  Command.command_history
end

#executeObject



82
83
84
# File 'lib/models/glimmer/calculator/command.rb', line 82

def execute
  raise 'Not implemented! Please override in a subclass.'
end

#last_commandObject



74
75
76
# File 'lib/models/glimmer/calculator/command.rb', line 74

def last_command
  command_history.last
end

#last_resultObject



70
71
72
# File 'lib/models/glimmer/calculator/command.rb', line 70

def last_result
  last_command&.result
end

#number1Object



46
47
48
# File 'lib/models/glimmer/calculator/command.rb', line 46

def number1
  Command.number1
end

#number1=(value) ⇒ Object



50
51
52
# File 'lib/models/glimmer/calculator/command.rb', line 50

def number1=(value)
  Command.number1 = value.to_f
end

#number2Object



54
55
56
# File 'lib/models/glimmer/calculator/command.rb', line 54

def number2
  Command.number2
end

#number2=(value) ⇒ Object



58
59
60
# File 'lib/models/glimmer/calculator/command.rb', line 58

def number2=(value)
  Command.number2 = value.to_f
end

#operationObject



62
63
64
# File 'lib/models/glimmer/calculator/command.rb', line 62

def operation
  Command.operation
end

#operation=(op) ⇒ Object



66
67
68
# File 'lib/models/glimmer/calculator/command.rb', line 66

def operation=(op)
  Command.operation = op
end