Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions .github/workflows/ruby.yml
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.
# This workflow will download a prebuilt Ruby version, install dependencies and run tests with Rake
# For more information see: https://github.com/marketplace/actions/setup-ruby-jruby-and-truffleruby

name: Ruby

on:
push:
branches: [ main ]
branches: [main]
pull_request:
branches: [ main ]
branches: [main]

permissions:
contents: read

jobs:
test:

runs-on: ubuntu-latest
strategy:
matrix:
ruby-version: ['2.6', '2.7', '3.0']
ruby-version: ['3.1', '3.2', '3.3', '3.4', 'jruby-9.4', 'jruby-10.0']

steps:
- uses: actions/checkout@v2
- name: Set up Java for JRuby 10.0
if: matrix.ruby-version == 'jruby-10.0'
uses: actions/setup-java@v5
with:
distribution: 'temurin'
java-version: '21'
- name: Checkout repository
uses: actions/checkout@v6
- name: Set up Ruby
# To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
# change this to (see https://github.com/ruby/setup-ruby#versioning):
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby-version }}
Expand Down
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Changelog

## Unreleased

## 1.0.0

- **Breaking change**: Update Money gem dependency to ~> 7.0. See the [Money 7.0 upgrading guide](https://github.com/RubyMoney/money/blob/main/UPGRADING-7.0.md)
- **Breaking change**: Drop support for Ruby < 3.1

## 0.1.0

- Initial release
- Add `Money::Collection` class for optimized sum/min/max operations on Money objects
- Minimize currency conversions for accuracy and efficiency
- Support enumerable operations with `#concat` and `#<<` methods
11 changes: 9 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
source 'https://rubygems.org'
# frozen_string_literal: true

source "https://rubygems.org"

# Specs
gem "minitest"

# Tasks
gem "rake"

# Specify your gem's dependencies in money-collection.gemspec
gemspec
2 changes: 1 addition & 1 deletion lib/money/collection/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class Money
class Collection
VERSION = "0.1.0"
VERSION = "1.0.0"
end
end
45 changes: 25 additions & 20 deletions money-collection.gemspec
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
# frozen_string_literal: true

lib = File.expand_path("lib", __dir__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'money/collection/version'
require "money/collection/version"

Gem::Specification.new do |s|
s.name = "money-collection"
s.version = Money::Collection::VERSION
s.platform = Gem::Platform::RUBY
s.authors = ["lulalala"]
s.email = ["mark@goodlife.tw"]
s.homepage = "https://github.com/RubyMoney/money-collection"
s.summary = "Optimized operation on collection of Money objects"
s.description = "Optimized operation on collection of Money objects"
s.license = "MIT"

Gem::Specification.new do |spec|
spec.name = "money-collection"
spec.version = Money::Collection::VERSION
spec.authors = ["lulalala"]
spec.email = ["mark@goodlife.tw"]
spec.summary = %q{Optimized operation on collection of Money objects}
spec.description = spec.summary
spec.homepage = "https://github.com/lulalala/money-collection"
spec.license = "MIT"
s.add_dependency "money", "~> 7.0"

spec.files = `git ls-files -z`.split("\x0")
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ["lib"]
s.required_ruby_version = ">= 3.1"

spec.add_dependency "money", "~> 6.3"
s.files = `git ls-files -z -- lib/* CHANGELOG.md LICENSE money-collection.gemspec README.md`.split("\x0")
s.require_paths = ["lib"]

spec.add_development_dependency "bundler"
spec.add_development_dependency "rake"
spec.add_development_dependency "minitest"
if s.respond_to?(:metadata)
s.metadata["changelog_uri"] = "https://github.com/RubyMoney/money-collection/blob/main/CHANGELOG.md"
s.metadata["source_code_uri"] = "https://github.com/RubyMoney/money-collection/"
s.metadata["bug_tracker_uri"] = "https://github.com/RubyMoney/money-collection/issues"
s.metadata["rubygems_mfa_required"] = "true"
end
end
21 changes: 0 additions & 21 deletions spec/helper.rb

This file was deleted.

48 changes: 23 additions & 25 deletions spec/money/collection_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
require 'helper'
require 'money'
require 'money/collection'
require "spec_helper"

describe Money::Collection do
before do
Expand All @@ -25,20 +23,20 @@
end

it 'has version' do
Money::Collection.const_get('VERSION').wont_be_empty
_(Money::Collection.const_get('VERSION')).wont_be_empty
end

describe '#sum' do

it 'sums with no element' do
c = Money::Collection.new
c.sum.must_equal Money.new(0)
_(c.sum).must_equal Money.new(0)
end

it 'sums with single element' do
c = Money::Collection.new
c << Money.new(10,:usd)
c.sum.must_equal Money.new(10,:usd)
_(c.sum).must_equal Money.new(10,:usd)
end

it 'sums same currency' do
Expand All @@ -50,7 +48,7 @@

c = Money::Collection.new(ary)

c.sum.must_equal Money.new(60,:usd)
_(c.sum).must_equal Money.new(60,:usd)
end

it 'sums different currencies' do
Expand All @@ -61,7 +59,7 @@

c = Money::Collection.new(ary)

c.sum.must_equal normal_sum(ary)
_(c.sum).must_equal normal_sum(ary)
end

it 'sums correctly, avoiding rounding down error' do
Expand All @@ -73,7 +71,7 @@

c = Money::Collection.new(ary)

c.sum.must_equal Money.new(11,:usd)
_(c.sum).must_equal Money.new(11,:usd)
end

it 'sums correctly, avoiding rounding twice if possible' do
Expand All @@ -84,7 +82,7 @@

c = Money::Collection.new(ary)

c.sum(:foo).must_equal Money.new(21,:foo)
_(c.sum(:foo)).must_equal Money.new(21,:foo)
end

it 'returns sum in the specified currency' do
Expand All @@ -95,8 +93,8 @@

c = Money::Collection.new(ary)

c.sum('foo').must_equal Money.new(22,:foo)
c.sum('usd').must_equal Money.new(11,:usd)
_(c.sum('foo')).must_equal Money.new(22,:foo)
_(c.sum('usd')).must_equal Money.new(11,:usd)
end

it 'returns sum in the specified currency and without unnecessary intermediate conversion' do
Expand All @@ -107,7 +105,7 @@

c = Money::Collection.new(ary)

c.sum('foo').must_equal Money.new(21,:foo)
_(c.sum('foo')).must_equal Money.new(21,:foo)
end

it 'returns sum large number of Money' do
Expand All @@ -124,7 +122,7 @@
)

c = Money::Collection.new(ary)
c.sum('foo').must_equal normal_sum(ary)
_(c.sum('foo')).must_equal normal_sum(ary)
end
end
end
Expand All @@ -138,7 +136,7 @@
]

c = Money::Collection.new(ary)
c.min.must_equal Money.new(1,:usd)
_(c.min).must_equal Money.new(1,:usd)
end
it 'returns smallest Money objects (from many items)' do
ary = 1000.times.map do
Expand All @@ -147,7 +145,7 @@
c = Money::Collection.new(ary)
min = Money.new(1, :usd)
c << min
c.min.must_equal min
_(c.min).must_equal min
end
end

Expand All @@ -160,7 +158,7 @@
]

c = Money::Collection.new(ary)
c.max.must_equal Money.new(30,:foo)
_(c.max).must_equal Money.new(30,:foo)
end
it 'returns biggest Money objects (from many items)' do
ary = 1000.times.map do
Expand All @@ -169,7 +167,7 @@
c = Money::Collection.new(ary)
max = Money.new(100001, :usd)
c << max
c.max.must_equal max
_(c.max).must_equal max
end
end

Expand All @@ -183,8 +181,8 @@
c = Money::Collection.new
c.concat ary

c.size.must_equal 2
c.sum('foo').must_equal Money.new(22,:foo)
_(c.size).must_equal 2
_(c.sum('foo')).must_equal Money.new(22,:foo)
end
it 'concats Money objects to collection multiple times' do
ary = [
Expand All @@ -196,8 +194,8 @@
c.concat ary
c.concat ary

c.size.must_equal 4
c.sum('foo').must_equal Money.new(44,:foo)
_(c.size).must_equal 4
_(c.sum('foo')).must_equal Money.new(44,:foo)
end
end

Expand All @@ -208,7 +206,7 @@
c << Money.new(10,:foo)
c << Money.new(10,:usd)

c.sum.must_equal Money.new(25,:usd)
_(c.sum).must_equal Money.new(25,:usd)
end
end

Expand All @@ -220,8 +218,8 @@
c = Money::Collection.new(ary)
c << Money.new(1,:usd)

ary.size.must_equal 1
ary[0].must_equal m1
_(ary.size).must_equal 1
_(ary[0]).must_equal m1
end
end
end
Expand Down
7 changes: 7 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

require "minitest/autorun"
require "money"
require "money/collection"

Money.default_currency = Money::Currency.new("USD")