Sha256: 47dfdfca3fb63b14207f1793a1e5808bb300d35af6ed4f6b7cb661ca85352a2a

Contents?: true

Size: 1.13 KB

Versions: 10

Compression:

Stored size: 1.13 KB

Contents

# encoding: utf-8

require 'forwardable'

module Monetize
  class Collection
    extend Forwardable
    include Enumerable
    def_delegators :@list, :[], :each, :last

    attr_reader :input, :currency, :options

    def self.parse(input, currency = Money.default_currency, options = {})
      new(input, currency, options).parse
    end

    def initialize(input, currency = Money.default_currency, options = {})
      if input.respond_to? :strip
        @input = input.clone.strip
      else
        fail ArgumentError, 'Input must be a string'
      end

      @currency = currency
      @options = options
      @list = []
    end

    def parse
      if range?
        @list = split_range.map { |fragment| Monetize.parse(fragment, currency, options) }
      else
        @list = split_list.map { |fragment| Monetize.parse(fragment, currency, options) }
      end

      self
    end

    def range?
      RANGE_SPLIT =~ input
    end

    private

    LIST_SPLIT = %r{[/,]}
    RANGE_SPLIT = /-/

    def split_list
      @input.split(LIST_SPLIT).map(&:strip)
    end

    def split_range
      @input.split(RANGE_SPLIT).map(&:strip)
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
monetize-1.13.0 lib/monetize/collection.rb
monetize-1.12.0 lib/monetize/collection.rb
monetize-1.11.0 lib/monetize/collection.rb
monetize-1.10.0 lib/monetize/collection.rb
monetize-1.9.4 lib/monetize/collection.rb
monetize-1.9.3 lib/monetize/collection.rb
monetize-1.9.2 lib/monetize/collection.rb
monetize-1.9.1 lib/monetize/collection.rb
monetize-1.9.0 lib/monetize/collection.rb
monetize-1.8.0 lib/monetize/collection.rb