Sha256: 30573dd2dbe78b22c1c7e99e58b9fe1ecacf53fc6efc6b5699f3b3b82407ff8b

Contents?: true

Size: 1.16 KB

Versions: 5

Compression:

Stored size: 1.16 KB

Contents

# encoding: utf-8

require 'monetize'

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
        raise 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
      Array @input.split(LIST_SPLIT)
        .map(&:strip)
    end

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

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
solidus_backend-1.0.0.pre3 vendor/bundle/gems/monetize-1.3.0/lib/collection.rb
solidus_backend-1.0.0.pre2 vendor/bundle/gems/monetize-1.3.0/lib/collection.rb
solidus_backend-1.0.0.pre vendor/bundle/gems/monetize-1.3.0/lib/collection.rb
monetize-1.3.0 lib/collection.rb
monetize-1.2.0 lib/collection.rb