morse-code-rb 0.3.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/LICENSE.md +21 -0
- data/bin/mc +2 -1
- data/lib/morse_code/base.rb +28 -0
- data/lib/morse_code/cli.rb +4 -2
- data/lib/morse_code/decoder.rb +8 -4
- data/lib/morse_code/decoders/chinese.rb +5 -4
- data/lib/morse_code/decoders/english.rb +4 -3
- data/lib/morse_code/encoder.rb +7 -3
- data/lib/morse_code/encoders/chinese.rb +5 -4
- data/lib/morse_code/encoders/english.rb +4 -3
- data/lib/morse_code/error.rb +2 -0
- data/lib/morse_code/version.rb +3 -1
- metadata +6 -6
- data/lib/morse_code/decoders/base.rb +0 -29
- data/lib/morse_code/encoders/base.rb +0 -29
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: c054c4bd196b1c6b5f94bed7a2cdf1e6ec5ca3f88d894895409e92b6e23ffa35
|
4
|
+
data.tar.gz: e55557f5696b0f0f723f60e8b10a5560723fd065447f99107c1db2fd6deae7ee
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4346ca4e6cfcd235cb4fffdcf522cf4dfba0b588a5affde4f47d3ddd7b86dd2e005fe39743bf1d30bbd3f11285d2584441225107ccaab172f4190ece69f1d8a8
|
7
|
+
data.tar.gz: 41504f9978c115307745703d532be5cb53b3dca42e21f943adc226054fee900dcf46dafc54fb272fd7c3670114c2f2153dce2adce9e8fd605228e570e9153eda
|
data/LICENSE.md
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Superiorlu
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/bin/mc
CHANGED
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'morse_code/error'
|
4
|
+
|
5
|
+
module MorseCode
|
6
|
+
class Base
|
7
|
+
def self.inherited(subclass)
|
8
|
+
@@subclasses ||= []
|
9
|
+
@@subclasses << subclass
|
10
|
+
end
|
11
|
+
|
12
|
+
attr_reader :word
|
13
|
+
|
14
|
+
def initialize(word = '')
|
15
|
+
@word = word
|
16
|
+
end
|
17
|
+
|
18
|
+
def supported?
|
19
|
+
raise NotImplementedError, 'Subclass must override supported? method'
|
20
|
+
end
|
21
|
+
|
22
|
+
def supported_classes
|
23
|
+
supported_classes = Array(@@subclasses).select { |subclass| subclass.new(word).supported? }
|
24
|
+
puts "[WARING] Not Classs can support this #{word}!" if supported_classes.empty?
|
25
|
+
supported_classes
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
data/lib/morse_code/cli.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'thor'
|
2
4
|
|
3
5
|
module MorseCode
|
@@ -6,7 +8,7 @@ module MorseCode
|
|
6
8
|
map '-e' => :encode
|
7
9
|
map '-d' => :decode
|
8
10
|
|
9
|
-
desc 'encode MESSAGE', 'encode message as Morse Code'
|
11
|
+
desc 'encode MESSAGE', 'encode message as Morse Code'
|
10
12
|
method_option :dit_dah, aliases: '-d', type: :boolean, desc: 'encode DITDAH message'
|
11
13
|
def encode(message = '')
|
12
14
|
if options[:dit_dah]
|
@@ -16,7 +18,7 @@ module MorseCode
|
|
16
18
|
end
|
17
19
|
end
|
18
20
|
|
19
|
-
desc 'decode MESSAGE', 'decode Morse Code as original message'
|
21
|
+
desc 'decode MESSAGE', 'decode Morse Code as original message'
|
20
22
|
method_option :dit_dah, aliases: '-d', type: :boolean, desc: 'decode from DITDAH message'
|
21
23
|
def decode(message = '')
|
22
24
|
if options[:dit_dah]
|
data/lib/morse_code/decoder.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'morse_code/error'
|
2
|
-
require 'morse_code/
|
4
|
+
require 'morse_code/base'
|
3
5
|
require 'morse_code/decoders/chinese'
|
4
6
|
require 'morse_code/decoders/english'
|
5
7
|
|
@@ -20,7 +22,7 @@ module MorseCode
|
|
20
22
|
word.push(decode_char(char))
|
21
23
|
end
|
22
24
|
end
|
23
|
-
decode_words.push(word.join)
|
25
|
+
decode_words.push(word.join) unless word.empty?
|
24
26
|
end.join(' ')
|
25
27
|
end
|
26
28
|
|
@@ -33,9 +35,11 @@ module MorseCode
|
|
33
35
|
private
|
34
36
|
|
35
37
|
def decode_char(char)
|
36
|
-
|
38
|
+
supported_classes = MorseCode::Base.new(char).supported_classes
|
39
|
+
supported_class = supported_classes.detect { |clazz| clazz.name.start_with?('MorseCode::Decoder') }
|
37
40
|
return supported_class.new(char).decode if supported_class
|
38
|
-
|
41
|
+
|
42
|
+
char
|
39
43
|
end
|
40
44
|
end
|
41
45
|
end
|
@@ -1,10 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'morse_code/error'
|
2
|
-
|
4
|
+
require 'morse_code/base'
|
3
5
|
|
4
6
|
module MorseCode
|
5
7
|
module Decoders
|
6
|
-
class Chinese < Base
|
7
|
-
|
8
|
+
class Chinese < MorseCode::Base
|
8
9
|
MIN_CHINESE_CODEPOINT = 19_968 # \u4e00
|
9
10
|
MAX_CHINESE_CODEPOINT = 40_869 # \u9fa5
|
10
11
|
|
@@ -19,7 +20,7 @@ module MorseCode
|
|
19
20
|
private
|
20
21
|
|
21
22
|
def decode_binary_word
|
22
|
-
@decode_binary_word ||= word.
|
23
|
+
@decode_binary_word ||= word.tr('.', '0').tr('-', '1').to_i(2)
|
23
24
|
end
|
24
25
|
end
|
25
26
|
end
|
@@ -1,10 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'morse_code/error'
|
2
|
-
|
4
|
+
require 'morse_code/base'
|
3
5
|
|
4
6
|
module MorseCode
|
5
7
|
module Decoders
|
6
|
-
class English < Base
|
7
|
-
|
8
|
+
class English < MorseCode::Base
|
8
9
|
def supported?
|
9
10
|
!MorseCode::DECODE_MAP[word].nil?
|
10
11
|
end
|
data/lib/morse_code/encoder.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'morse_code/error'
|
2
|
-
require 'morse_code/
|
4
|
+
require 'morse_code/base'
|
3
5
|
require 'morse_code/encoders/chinese'
|
4
6
|
require 'morse_code/encoders/english'
|
5
7
|
|
@@ -32,9 +34,11 @@ module MorseCode
|
|
32
34
|
end
|
33
35
|
|
34
36
|
def encode_letter(letter)
|
35
|
-
|
37
|
+
supported_classes = MorseCode::Base.new(letter).supported_classes
|
38
|
+
supported_class = supported_classes.detect { |clazz| clazz.name.start_with?('MorseCode::Encoder') }
|
36
39
|
return supported_class.new(letter).encode if supported_class
|
37
|
-
|
40
|
+
|
41
|
+
letter
|
38
42
|
end
|
39
43
|
end
|
40
44
|
end
|
@@ -1,16 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'morse_code/error'
|
2
|
-
|
4
|
+
require 'morse_code/base'
|
3
5
|
|
4
6
|
module MorseCode
|
5
7
|
module Encoders
|
6
|
-
class Chinese < Base
|
7
|
-
|
8
|
+
class Chinese < MorseCode::Base
|
8
9
|
def supported?
|
9
10
|
!/\p{Han}/.match(word).nil? # \u4e00-\u9fa5
|
10
11
|
end
|
11
12
|
|
12
13
|
def encode
|
13
|
-
word.ord.to_s(2).
|
14
|
+
word.ord.to_s(2).tr!('0', '.').tr!('1', '-')
|
14
15
|
end
|
15
16
|
end
|
16
17
|
end
|
@@ -1,10 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'morse_code/error'
|
2
|
-
|
4
|
+
require 'morse_code/base'
|
3
5
|
|
4
6
|
module MorseCode
|
5
7
|
module Encoders
|
6
|
-
class English < Base
|
7
|
-
|
8
|
+
class English < MorseCode::Base
|
8
9
|
def supported?
|
9
10
|
!MorseCode::ENCODE_MAP[word].nil?
|
10
11
|
end
|
data/lib/morse_code/error.rb
CHANGED
data/lib/morse_code/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: morse-code-rb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- YingRui Lu
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-10-
|
11
|
+
date: 2018-10-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -158,16 +158,16 @@ executables:
|
|
158
158
|
extensions: []
|
159
159
|
extra_rdoc_files: []
|
160
160
|
files:
|
161
|
+
- LICENSE.md
|
161
162
|
- README.md
|
162
163
|
- bin/mc
|
163
164
|
- lib/morse_code.rb
|
165
|
+
- lib/morse_code/base.rb
|
164
166
|
- lib/morse_code/cli.rb
|
165
167
|
- lib/morse_code/decoder.rb
|
166
|
-
- lib/morse_code/decoders/base.rb
|
167
168
|
- lib/morse_code/decoders/chinese.rb
|
168
169
|
- lib/morse_code/decoders/english.rb
|
169
170
|
- lib/morse_code/encoder.rb
|
170
|
-
- lib/morse_code/encoders/base.rb
|
171
171
|
- lib/morse_code/encoders/chinese.rb
|
172
172
|
- lib/morse_code/encoders/english.rb
|
173
173
|
- lib/morse_code/error.rb
|
@@ -185,7 +185,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
185
185
|
requirements:
|
186
186
|
- - ">="
|
187
187
|
- !ruby/object:Gem::Version
|
188
|
-
version: 2.
|
188
|
+
version: 2.2.0
|
189
189
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
190
190
|
requirements:
|
191
191
|
- - ">="
|
@@ -193,7 +193,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
193
193
|
version: '0'
|
194
194
|
requirements: []
|
195
195
|
rubyforge_project:
|
196
|
-
rubygems_version: 2.
|
196
|
+
rubygems_version: 2.7.7
|
197
197
|
signing_key:
|
198
198
|
specification_version: 4
|
199
199
|
summary: Simple Morse Code Encode and Decode Tool
|
@@ -1,29 +0,0 @@
|
|
1
|
-
require 'morse_code/error'
|
2
|
-
|
3
|
-
module MorseCode
|
4
|
-
module Decoders
|
5
|
-
class Base
|
6
|
-
|
7
|
-
def self.inherited(subclass)
|
8
|
-
@@subclasses ||= []
|
9
|
-
@@subclasses << subclass
|
10
|
-
end
|
11
|
-
|
12
|
-
attr_reader :word
|
13
|
-
|
14
|
-
def initialize(word = '')
|
15
|
-
@word = word
|
16
|
-
end
|
17
|
-
|
18
|
-
def supported?
|
19
|
-
raise NotImplementedError, 'Subclass must override supported? method'
|
20
|
-
end
|
21
|
-
|
22
|
-
def supported_class
|
23
|
-
supported_class = Array(@@subclasses).detect { |subclass| subclass.new(word).supported? }
|
24
|
-
puts "[WARING] Not Classs can decode this #{word}!" unless supported_class
|
25
|
-
supported_class
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
@@ -1,29 +0,0 @@
|
|
1
|
-
require 'morse_code/error'
|
2
|
-
|
3
|
-
module MorseCode
|
4
|
-
module Encoders
|
5
|
-
class Base
|
6
|
-
|
7
|
-
def self.inherited(subclass)
|
8
|
-
@@subclasses ||= []
|
9
|
-
@@subclasses << subclass
|
10
|
-
end
|
11
|
-
|
12
|
-
attr_reader :word
|
13
|
-
|
14
|
-
def initialize(word = '')
|
15
|
-
@word = word
|
16
|
-
end
|
17
|
-
|
18
|
-
def supported?
|
19
|
-
raise NotImplementedError, 'Subclass must override supported? method'
|
20
|
-
end
|
21
|
-
|
22
|
-
def supported_class
|
23
|
-
supported_class = Array(@@subclasses).detect { |subclass| subclass.new(word).supported? }
|
24
|
-
puts "[WARING] Not Classs can encode #{word}!" unless supported_class
|
25
|
-
supported_class
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|