Sha256: 25590f45d08761f93056b43b61322a5e4d1c0bed57226e1479a4157f10c173ec

Contents?: true

Size: 1.15 KB

Versions: 4

Compression:

Stored size: 1.15 KB

Contents

# encoding: utf-8

require 'monetize'
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

4 entries across 4 versions & 1 rubygems

Version Path
monetize-1.7.0 lib/collection.rb
monetize-1.6.0 lib/collection.rb
monetize-1.5.0 lib/collection.rb
monetize-1.4.0 lib/collection.rb