Class: Dcr::Command
- Inherits:
-
Object
show all
- Includes:
- Glimmer, Strategic
- Defined in:
- app/models/dcr/command.rb,
app/models/dcr/command/left.rb,
app/models/dcr/command/color.rb,
app/models/dcr/command/empty.rb,
app/models/dcr/command/right.rb,
app/models/dcr/command/repeat.rb,
app/models/dcr/command/forward.rb,
app/models/dcr/command/backward.rb
Overview
Follows the Command, Strategy, and Chain of Responsibility Design Patterns
Defined Under Namespace
Classes: Backward, Color, Empty, Forward, Left, Repeat, Right
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(program:, text:) ⇒ Command
Returns a new instance of Command.
76
77
78
79
|
# File 'app/models/dcr/command.rb', line 76
def initialize(program: , text: )
@program = program
@text = text
end
|
Instance Attribute Details
#text ⇒ Object
Returns the value of attribute text.
74
75
76
|
# File 'app/models/dcr/command.rb', line 74
def text
@text
end
|
Class Method Details
.command_alias(alias_value) ⇒ Object
44
45
46
|
# File 'app/models/dcr/command.rb', line 44
def command_alias(alias_value)
command_aliases << alias_value
end
|
.command_aliases ⇒ Object
48
49
50
|
# File 'app/models/dcr/command.rb', line 48
def command_aliases
@command_aliases ||= []
end
|
.command_exclusion(exclusion_value) ⇒ Object
52
53
54
|
# File 'app/models/dcr/command.rb', line 52
def command_exclusion(exclusion_value)
command_exclusions << exclusion_value
end
|
.command_exclusions ⇒ Object
56
57
58
|
# File 'app/models/dcr/command.rb', line 56
def command_exclusions
@command_exclusions ||= []
end
|
.command_operation ⇒ Object
64
65
66
|
# File 'app/models/dcr/command.rb', line 64
def command_operation
name.split('::').last.downcase
end
|
.create(program:, text:) ⇒ Object
29
30
31
32
33
34
|
# File 'app/models/dcr/command.rb', line 29
def create(program: , text: )
operation = parse_operation(text)
operation_strategy = strategies.detect { |strategy| strategy.match?(operation) } unless operation.nil?
operation_strategy ||= Command::Empty
operation_strategy&.new(program: program, text: text)
end
|
.match?(operation) ⇒ Boolean
60
61
62
|
# File 'app/models/dcr/command.rb', line 60
def match?(operation)
(operation_derivatives(command_operation) + command_aliases - command_exclusions).include?(operation.to_s.downcase)
end
|
.operation_derivatives(operation) ⇒ Object
68
69
70
71
|
# File 'app/models/dcr/command.rb', line 68
def operation_derivatives(operation)
operation_length = operation.length
operation_length.times.map {|n| operation.chars.combination(operation_length - n).to_a}.reduce(:+).map(&:join)
end
|
.parse_operation(text) ⇒ Object
36
37
38
|
# File 'app/models/dcr/command.rb', line 36
def parse_operation(text)
text.to_s.strip.downcase.match(/([^ 0-9]+)/).to_a[1]
end
|
.parse_value(text) ⇒ Object
40
41
42
|
# File 'app/models/dcr/command.rb', line 40
def parse_value(text)
text.to_s.strip.downcase.match(/[^ 0-9]+[ ]*([^ ]*)/).to_a[1]
end
|
Instance Method Details
#call ⇒ Object
Subclasses must implement
122
123
124
|
# File 'app/models/dcr/command.rb', line 122
def call
end
|
#normalized_text ⇒ Object
117
118
119
|
# File 'app/models/dcr/command.rb', line 117
def normalized_text
"#{operation} #{value}"
end
|
#operation ⇒ Object
Subclasses must override to provide parsed operation (e.g. ‘f’ becomes ‘forward’)
95
96
97
|
# File 'app/models/dcr/command.rb', line 95
def operation
parsed_operation
end
|
#operation=(new_operation) ⇒ Object
81
82
83
84
85
86
|
# File 'app/models/dcr/command.rb', line 81
def operation=(new_operation)
return if parse_operation(new_operation) == parsed_operation
@text.sub(parsed_operation, new_operation)
command_index_of_self = @program.commands.index(self)
@program.commands[command_index_of_self..command_index_of_self] = create(program: @program, text: @text)
end
|
#operation_options ⇒ Object
103
104
105
|
# File 'app/models/dcr/command.rb', line 103
def operation_options
Command.strategy_names
end
|
#parsed_operation ⇒ Object
99
100
101
|
# File 'app/models/dcr/command.rb', line 99
def parsed_operation
Command.parse_operation(@text)
end
|
#parsed_value ⇒ Object
112
113
114
115
|
# File 'app/models/dcr/command.rb', line 112
def parsed_value
parsed_value = Command.parse_value(@text)
parsed_value.to_s.match(/\d+/) ? parsed_value.to_i : parsed_value
end
|
#value ⇒ Object
Subclasses must override to provide parsed value (e.g. ‘r’ becomes ‘red’ or ‘30’ becomes 30)
108
109
110
|
# File 'app/models/dcr/command.rb', line 108
def value
parsed_value
end
|
#value=(new_value) ⇒ Object
88
89
90
91
92
|
# File 'app/models/dcr/command.rb', line 88
def value=(new_value)
return if parse_value(new_value) == parsed_value
@text.sub(parsed_value, new_value)
@program.notify_observers('commands')
end
|