mongoid_big_decimal_integer 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --colour
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use 1.9.3@mongoid_big_decimal_integer --create
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mongoid-big_decimal_integer.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 TODO: Write your name
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # Mongoid::BigDecimalInteger
2
+
3
+ Mongoid type which stores BigDecimals as Integers.
4
+
5
+ The default BigDecimal type provided in Mongoid stores BigDecimals as Strings. Because of this, stuff like sorts and range queries don't work. This type multiplies the value by a (configurable) power of 10 and stores the resulting Integer.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'mongoid_big_decimal_integer'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install mongoid_big_decimal_integer
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require "rspec/core/rake_task"
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task :default => :spec
@@ -0,0 +1,22 @@
1
+ module Mongoid
2
+ class BigDecimalInteger
3
+
4
+ DEFAULT_DECIMAL_PLACES = 2
5
+
6
+ @@decimal_places = DEFAULT_DECIMAL_PLACES
7
+
8
+ def self.decimal_places=(decimal_places)
9
+ @@decimal_places = decimal_places
10
+ end
11
+
12
+ def self.demongoize(value)
13
+ BigDecimal.new(value.to_s) / (10 ** @@decimal_places)
14
+ end
15
+
16
+ def self.mongoize(value)
17
+ big_decimal_value = value.is_a?(BigDecimal) ? value : BigDecimal.new(value.to_s)
18
+ (big_decimal_value.round(@@decimal_places) * (10 ** @@decimal_places)).to_i
19
+ end
20
+
21
+ end
22
+ end
@@ -0,0 +1,5 @@
1
+ module Mongoid
2
+ class BigDecimalInteger
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,3 @@
1
+ require "bigdecimal"
2
+ require "mongoid/big_decimal_integer"
3
+ require "mongoid/big_decimal_integer/version"
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/mongoid/big_decimal_integer/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Guilherme Cirne"]
6
+ gem.email = ["gcirne@gmail.com"]
7
+ gem.description = %q{The default BigDecimal type provided in Mongoid stores BigDecimals as Strings. Because of this, stuff like sorts and range queries don't work. This type multiplies the value by a (configurable) power of 10 and stores the resulting Integer.}
8
+ gem.summary = %q{Mongoid type which stores BigDecimals as Integers.}
9
+ gem.homepage = ""
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "mongoid_big_decimal_integer"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Mongoid::BigDecimalInteger::VERSION
17
+
18
+ gem.add_development_dependency "rspec"
19
+ end
@@ -0,0 +1,47 @@
1
+ require "spec_helper"
2
+
3
+ describe Mongoid::BigDecimalInteger do
4
+ before(:each) do
5
+ Mongoid::BigDecimalInteger.decimal_places = Mongoid::BigDecimalInteger::DEFAULT_DECIMAL_PLACES
6
+ end
7
+
8
+ describe ".demongoize" do
9
+ specify { Mongoid::BigDecimalInteger.demongoize(1234).should == BigDecimal.new("12.34") }
10
+
11
+ context "with custom decimal places" do
12
+ before(:each) { Mongoid::BigDecimalInteger.decimal_places = 12 }
13
+ specify { Mongoid::BigDecimalInteger.demongoize(12345678901234).should == BigDecimal.new("12.345678901234") }
14
+ end
15
+ end
16
+
17
+ describe ".mongoize" do
18
+ context "when value is an Integer" do
19
+ let(:value) { 1234 }
20
+ specify { Mongoid::BigDecimalInteger.mongoize(value).should == 123400 }
21
+ end
22
+
23
+ context "when value is a Float" do
24
+ let(:value) { 12.34 }
25
+ specify { Mongoid::BigDecimalInteger.mongoize(value).should == 1234 }
26
+ end
27
+
28
+ context "when value is a BigDecimal" do
29
+ let(:value) { BigDecimal.new("1234") }
30
+ specify { Mongoid::BigDecimalInteger.mongoize(value).should == 123400 }
31
+ end
32
+
33
+ context "when value is a String" do
34
+ let(:value) { "1234" }
35
+ specify { Mongoid::BigDecimalInteger.mongoize(value).should == 123400 }
36
+ end
37
+
38
+ context "with custom decimal places" do
39
+ before(:each) { Mongoid::BigDecimalInteger.decimal_places = 12 }
40
+ specify { Mongoid::BigDecimalInteger.mongoize(12.345678901234).should == 12345678901234 }
41
+ end
42
+
43
+ it "should round the value before converting" do
44
+ Mongoid::BigDecimalInteger.mongoize(1234.567).should == 123457
45
+ end
46
+ end
47
+ end
@@ -0,0 +1 @@
1
+ require "mongoid_big_decimal_integer"
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongoid_big_decimal_integer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Guilherme Cirne
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-12-01 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: The default BigDecimal type provided in Mongoid stores BigDecimals as
31
+ Strings. Because of this, stuff like sorts and range queries don't work. This type
32
+ multiplies the value by a (configurable) power of 10 and stores the resulting Integer.
33
+ email:
34
+ - gcirne@gmail.com
35
+ executables: []
36
+ extensions: []
37
+ extra_rdoc_files: []
38
+ files:
39
+ - .gitignore
40
+ - .rspec
41
+ - .rvmrc
42
+ - Gemfile
43
+ - LICENSE
44
+ - README.md
45
+ - Rakefile
46
+ - lib/mongoid/big_decimal_integer.rb
47
+ - lib/mongoid/big_decimal_integer/version.rb
48
+ - lib/mongoid_big_decimal_integer.rb
49
+ - mongoid_big_decimal_integer.gemspec
50
+ - spec/mongoid/big_decimal_integer_spec.rb
51
+ - spec/spec_helper.rb
52
+ homepage: ''
53
+ licenses: []
54
+ post_install_message:
55
+ rdoc_options: []
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ! '>='
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ requirements: []
71
+ rubyforge_project:
72
+ rubygems_version: 1.8.23
73
+ signing_key:
74
+ specification_version: 3
75
+ summary: Mongoid type which stores BigDecimals as Integers.
76
+ test_files:
77
+ - spec/mongoid/big_decimal_integer_spec.rb
78
+ - spec/spec_helper.rb