galaxy_converter 3.0.1 → 3.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 87b623ff14335cdeefbdd51a3576e5c17873cce273ff1000285d5e42ab5ed5e1
4
- data.tar.gz: fcd3f01c18c0c377fc6756c1e5d02a4ae64f0cdd6f571b0642bda9aa6147313a
3
+ metadata.gz: 818318a9be3585e70bd5e6bf29377b441325385e41430454421dd0d5f7ad8b05
4
+ data.tar.gz: 18db9e32c0ffa14551bae511f98d42d9bd841485b3680c4004dd22b1db66d0b2
5
5
  SHA512:
6
- metadata.gz: d01ea972a57072f1704ca1b96f1e7a26d2b8c3d3c0b0a1b63fdb2b68f382a56a9a4417ae11d3bb466fbfbeaf47ece3b924d4b4010673bf04704ddb9933abde4f
7
- data.tar.gz: '0592b1427e97f9f8fd7664708d130695faaab4d53e93d3bcbb391dea33c6250606aa3657f7221c7530c3fc106f6f7b5f175b3ef1b095ab623a97db46865004d5'
6
+ metadata.gz: 3403a2ec655b81beb9a216ec394a7fc08a0019f964bc1fd4bbb47b66baf9abc0ccbb08c7830e02dc29b578ea83cec3c667584a0a4609406eec492d4202aaed40
7
+ data.tar.gz: 5f896f540740558594bb98291378b643391e64660371fb87085a908fe6904a3dbf4dd7e5bbd07f266e351cf2b1f4db30eacb2a3ddcf0fb767def8b98107eb543
@@ -10,12 +10,12 @@ module GalaxyConverter
10
10
 
11
11
  def initialize(notes, converter = Converter)
12
12
  notes = notes.reject(&:question?)
13
- @commercials, @assertions = notes.partition(&:commercial?)
13
+ @credits, @assertions = notes.partition { |note| note.instance_of? Credit }
14
14
  @converter = converter.new(mapping)
15
15
  end
16
16
 
17
17
  def goods
18
- @goods ||= @commercials.reduce({}) do |acc, note|
18
+ @goods ||= @credits.reduce({}) do |acc, note|
19
19
  matching = note.body.match(GOODS_RULE)
20
20
  next acc unless matching
21
21
  units, name, credits = matching.captures
@@ -5,7 +5,10 @@ module GalaxyConverter
5
5
  NO_IDEA = "I have no idea what you are talking about"
6
6
 
7
7
  def self.from(notes)
8
- Array(notes).map { |body| new(body) }
8
+ Array(notes).map do |body|
9
+ klass = body.index(/credits/i) ? Credit : Note
10
+ klass.new(body)
11
+ end
9
12
  end
10
13
 
11
14
  attr_reader :body, :units, :good
@@ -19,10 +22,6 @@ module GalaxyConverter
19
22
  @body.end_with?(QUESTION)
20
23
  end
21
24
 
22
- def commercial?
23
- !!@body.index("credits")
24
- end
25
-
26
25
  def answer(total = 0)
27
26
  return NO_IDEA if total.zero?
28
27
  [].tap do |s|
@@ -30,19 +29,28 @@ module GalaxyConverter
30
29
  s << good.to_s.capitalize
31
30
  s << "is"
32
31
  s << "%g" % total
33
- s << "Credits" if commercial?
34
32
  end.reject(&:empty?).join(" ")
35
33
  end
36
34
 
37
35
  private def detect
38
- tokens = stripped.split
39
- return [tokens.join(" "), nil] unless commercial?
40
- good = tokens.pop
41
- [tokens.join(" "), good]
36
+ return stripped, nil
42
37
  end
43
38
 
44
39
  private def stripped
45
40
  @body.sub(/#{PREFIXES.join("|")}/, "").sub(QUESTION, "").strip
46
41
  end
47
42
  end
43
+
44
+ class Credit < Note
45
+ def answer(total = 0)
46
+ s = super
47
+ s << " Credits"
48
+ end
49
+
50
+ private def detect
51
+ tokens = stripped.split
52
+ good = tokens.pop
53
+ [tokens.join(" "), good]
54
+ end
55
+ end
48
56
  end
@@ -1,5 +1,4 @@
1
1
  require "galaxy_converter/constraint"
2
- require "galaxy_converter/stretcher"
3
2
 
4
3
  module GalaxyConverter
5
4
  class RomanNumeral
@@ -13,10 +12,18 @@ module GalaxyConverter
13
12
  "I" => 1
14
13
  }
15
14
 
16
- def initialize(value, constraint = Constraint, stretcher = Stretcher)
15
+ STRETCH_MAP = {
16
+ "DCCCC" => "CM", # 900
17
+ "CCCC" => "CD", # 400
18
+ "LXXXX" => "XC", # 90
19
+ "XXXX" => "XL", # 40
20
+ "VIIII" => "IX", # 9
21
+ "IIII" => "IV" # 4
22
+ }
23
+
24
+ def initialize(value, constraint = Constraint)
17
25
  @value = value.to_s.upcase
18
26
  @constraint = constraint
19
- @stretcher = stretcher
20
27
  end
21
28
 
22
29
  def to_s
@@ -25,7 +32,13 @@ module GalaxyConverter
25
32
 
26
33
  def to_i
27
34
  return 0 unless valid?
28
- @stretcher.call(@value).chars.sum { |symbol| SYMBOLS.fetch(symbol, 0) }
35
+ stretched.chars.sum { |symbol| SYMBOLS.fetch(symbol, 0) }
36
+ end
37
+
38
+ private def stretched
39
+ @stretched ||= STRETCH_MAP.reduce(@value) do |value, (long, short)|
40
+ value.gsub(short, long)
41
+ end
29
42
  end
30
43
 
31
44
  private def valid?
@@ -1,3 +1,3 @@
1
1
  module GalaxyConverter
2
- VERSION = "3.0.1"
2
+ VERSION = "3.1.1"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: galaxy_converter
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.1
4
+ version: 3.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - costajob
@@ -76,7 +76,6 @@ files:
76
76
  - lib/galaxy_converter/note.rb
77
77
  - lib/galaxy_converter/responder.rb
78
78
  - lib/galaxy_converter/roman_numeral.rb
79
- - lib/galaxy_converter/stretcher.rb
80
79
  - lib/galaxy_converter/version.rb
81
80
  - notes.txt
82
81
  homepage: https://github.com/costajob/galaxy_converter
@@ -1,20 +0,0 @@
1
- module GalaxyConverter
2
- module Stretcher
3
- extend self
4
-
5
- LONG_TO_SHORT = {
6
- "DCCCC" => "CM", # 900
7
- "CCCC" => "CD", # 400
8
- "LXXXX" => "XC", # 90
9
- "XXXX" => "XL", # 40
10
- "VIIII" => "IX", # 9
11
- "IIII" => "IV" # 4
12
- }
13
-
14
- def call(value)
15
- LONG_TO_SHORT.reduce(value) do |to_convert, (long, short)|
16
- to_convert.gsub(short, long)
17
- end
18
- end
19
- end
20
- end