Class: Glimmer::Calculator::Command
- Inherits:
-
Object
- Object
- Glimmer::Calculator::Command
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
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
.number1 ⇒ Object
Returns the value of attribute number1.
6
7
8
|
# File 'lib/models/glimmer/calculator/command.rb', line 6
def number1
@number1
end
|
.number2 ⇒ Object
Returns the value of attribute number2.
6
7
8
|
# File 'lib/models/glimmer/calculator/command.rb', line 6
def number2
@number2
end
|
.operation ⇒ Object
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
Returns the value of attribute button.
39
40
41
|
# File 'lib/models/glimmer/calculator/command.rb', line 39
def button
@button
end
|
#result ⇒ Object
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_history ⇒ Object
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
.keyword_to_command_class_mapping ⇒ Object
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_history ⇒ Object
78
79
80
|
# File 'lib/models/glimmer/calculator/command.rb', line 78
def command_history
Command.command_history
end
|
#execute ⇒ Object
82
83
84
|
# File 'lib/models/glimmer/calculator/command.rb', line 82
def execute
raise 'Not implemented! Please override in a subclass.'
end
|
#last_command ⇒ Object
74
75
76
|
# File 'lib/models/glimmer/calculator/command.rb', line 74
def last_command
command_history.last
end
|
#last_result ⇒ Object
70
71
72
|
# File 'lib/models/glimmer/calculator/command.rb', line 70
def last_result
last_command&.result
end
|
#number1 ⇒ Object
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
|
#number2 ⇒ Object
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
|
#operation ⇒ Object
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
|