Sha256: ebf711b922fad61a1e15da7a00949f5d52ce1a84a7be9dd68c5b17d4bdc454e4

Contents?: true

Size: 1.03 KB

Versions: 1

Compression:

Stored size: 1.03 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?)
      @commercials, @assertions = notes.partition(&:commercial?)
      @converter = converter.new(mapping)
    end

    def goods
      @goods ||= @commercials.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

1 entries across 1 versions & 1 rubygems

Version Path
galaxy_converter-3.0.1 lib/galaxy_converter/detector.rb