Sha256: 19c810ee533c3b74b3480368f865b2d1b1798c7e70cc5e608c5369cde2759cb6

Contents?: true

Size: 1.05 KB

Versions: 2

Compression:

Stored size: 1.05 KB

Contents

require "galaxy_converter/converter"
require "galaxy_converter/note"

module GalaxyConverter
  class Detector
    MAPPING_RULE = /(\w+) is (\w+)/
    GOODS_RULE = /([\w+\s]+) (\w+) is (\d+) credits/i

    attr_reader :converter

    def initialize(notes, converter = Converter)
      notes = notes.reject(&:question?)
      @credits, @assertions = notes.partition { |note| note.instance_of? Credit }
      @converter = converter.new(mapping)
    end

    def goods
      @goods ||= @credits.reduce({}) do |acc, note|
        matching = note.body.match(GOODS_RULE)
        next acc unless matching
        units, name, credits = matching.captures 
        value = @converter.call(units)
        next acc if value.zero?
        acc[name] = credits.to_f / value
        acc
      end
    end

    private def mapping
      @mapping ||= @assertions.reduce({}) do |acc, note|
        matching = note.body.match(MAPPING_RULE)
        next acc unless matching
        unit, roman = matching.captures
        acc[unit.strip] = roman.upcase
        acc
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
galaxy_converter-3.1.2 lib/galaxy_converter/detector.rb
galaxy_converter-3.1.1 lib/galaxy_converter/detector.rb