centipede 0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ dist/*
data/README ADDED
@@ -0,0 +1,49 @@
1
+ Centipede
2
+ =========
3
+
4
+ Make it easy to store money values in an ActiveRecord model, avoiding the
5
+ annoying floating point math. The idea is to store the monetary values as
6
+ the amount of cents in the database to avoid the math.
7
+
8
+ class Product < ActiveRecord::Base
9
+ attr_cents :price
10
+ end
11
+
12
+ product = Product.new(:price => 10.99)
13
+
14
+ product.price #=> 10.99
15
+ product.attributes["price"] #=> 1099
16
+
17
+ The `products` table should have an integer column `price` for this to work.
18
+
19
+ Why?
20
+ ====
21
+
22
+ Because we only needed this, and most of the gems that do this stuff are too
23
+ damn big and have a ton of un-needed functionality.
24
+
25
+ License
26
+ ======
27
+
28
+ (The MIT License)
29
+
30
+ Copyright (c) 2010 Nicolas Sanguinetti, http://nicolassanguinetti.info
31
+
32
+ Permission is hereby granted, free of charge, to any person obtaining
33
+ a copy of this software and associated documentation files (the
34
+ 'Software'), to deal in the Software without restriction, including
35
+ without limitation the rights to use, copy, modify, merge, publish,
36
+ distribute, sublicense, and/or sell copies of the Software, and to
37
+ permit persons to whom the Software is furnished to do so, subject to
38
+ the following conditions:
39
+
40
+ The above copyright notice and this permission notice shall be
41
+ included in all copies or substantial portions of the Software.
42
+
43
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
44
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
45
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
46
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
47
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
48
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
49
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,25 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "centipede"
3
+ s.version = "0.1"
4
+ s.date = "2010-01-12"
5
+
6
+ s.description = "Simple money storage for ActiveRecord models"
7
+ s.summary = "Simple money storage for ActiveRecord models"
8
+ s.homepage = "http://github.com/foca/centipede"
9
+
10
+ s.authors = ["Nicolás Sanguinetti"]
11
+ s.email = "contacto@nicolassanguinetti.info"
12
+
13
+ s.require_paths = ["lib"]
14
+ s.has_rdoc = true
15
+
16
+ s.files = %w[
17
+ .gitignore
18
+ README
19
+ centipede.gemspec
20
+ lib/centipede.rb
21
+ rails/init.rb
22
+ test/test_centipede.rb
23
+ ]
24
+ end
25
+
@@ -0,0 +1,18 @@
1
+ module Centipede
2
+ def attr_cents(*names)
3
+ names.each do |name|
4
+ define_method name do
5
+ read_attribute(name).to_i / 100.0
6
+ end
7
+
8
+ define_method "#{name}=" do |value|
9
+ if value = value && value.to_s
10
+ value.gsub! /^(\d+)\.?$/, '\1.00'
11
+ value.gsub! /(\.\d)$/, '\10'
12
+ value.gsub! /(\d+)(?:\.(\d{1,2})\d*)?$/, '\1\2'
13
+ end
14
+ write_attribute(name, value.to_i)
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,2 @@
1
+ require "centipede"
2
+ ActiveRecord::Base.extend Centipede
@@ -0,0 +1,49 @@
1
+ require "centipede"
2
+ require "test/unit"
3
+
4
+ class TestCentipede < Test::Unit::TestCase
5
+ extend Centipede
6
+
7
+ def read_attribute(name)
8
+ instance_variable_get(:"@#{name}")
9
+ end
10
+
11
+ def write_attribute(name, value)
12
+ instance_variable_set(:"@#{name}", value)
13
+ end
14
+
15
+ attr_cents :value
16
+
17
+ def test_nil_values_are_stored_as_zero
18
+ self.value = nil
19
+ assert_equal 0, self.value
20
+ end
21
+
22
+ def test_float_values_are_truncated_two_places_after_the_decimal_point
23
+ self.value = 10.437
24
+ assert_equal 10.43, self.value
25
+ end
26
+
27
+ def test_values_are_stored_as_100_times_the_value
28
+ self.value = 10.437
29
+ assert_equal 1043, read_attribute(:value)
30
+ end
31
+
32
+ def test_values_are_returned_as_a_100th_of_the_stored_value
33
+ write_attribute(:value, 1234)
34
+ assert_equal 12.34, self.value
35
+ end
36
+
37
+ def test_converts_up_to_two_decimal_places
38
+ self.value = 19.90
39
+ assert_equal 19.90, self.value
40
+
41
+ self.value = 19.8
42
+ assert_equal 19.8, self.value
43
+ end
44
+
45
+ def test_does_not_break_integers
46
+ self.value = 15
47
+ assert_equal 15, self.value
48
+ end
49
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: centipede
3
+ version: !ruby/object:Gem::Version
4
+ version: "0.1"
5
+ platform: ruby
6
+ authors:
7
+ - "Nicol\xC3\xA1s Sanguinetti"
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-01-12 00:00:00 -02:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Simple money storage for ActiveRecord models
17
+ email: contacto@nicolassanguinetti.info
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - .gitignore
26
+ - README
27
+ - centipede.gemspec
28
+ - lib/centipede.rb
29
+ - rails/init.rb
30
+ - test/test_centipede.rb
31
+ has_rdoc: true
32
+ homepage: http://github.com/foca/centipede
33
+ licenses: []
34
+
35
+ post_install_message:
36
+ rdoc_options: []
37
+
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: "0"
45
+ version:
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: "0"
51
+ version:
52
+ requirements: []
53
+
54
+ rubyforge_project:
55
+ rubygems_version: 1.3.5
56
+ signing_key:
57
+ specification_version: 3
58
+ summary: Simple money storage for ActiveRecord models
59
+ test_files: []
60
+