acts_as_priceable 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,18 @@
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
18
+ .idea/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in acts_as_priceable.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Paweł Tarczykowski
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.
@@ -0,0 +1,29 @@
1
+ # ActsAsPriceable
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'acts_as_priceable'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install acts_as_priceable
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require 'rspec/core/rake_task'
4
+ RSpec::Core::RakeTask.new('spec')
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/acts_as_priceable/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Paweł Tarczykowski"]
6
+ gem.email = ["farrel1@wp.pl"]
7
+ gem.description = "Acts as priceable"
8
+ gem.summary = "Acts as priceable"
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 = "acts_as_priceable"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = ActsAsPriceable::VERSION
17
+ gem.add_dependency "activerecord", ">= 3.0.0"
18
+ gem.add_dependency "bigdecimal", "~> 1.1.0"
19
+ gem.add_development_dependency 'rspec', "~> 2.10.0"
20
+ gem.add_development_dependency 'rake'
21
+ gem.add_development_dependency 'sqlite3'
22
+ end
@@ -0,0 +1,8 @@
1
+ require "acts_as_priceable/version"
2
+ require "acts_as_priceable/schema"
3
+ require "acts_as_priceable/base"
4
+ require "active_record"
5
+
6
+
7
+ ActiveRecord::Base.send :include, ActsAsPriceable::Base
8
+ ActiveRecord::Base.send :include, ActsAsPriceable::Schema
@@ -0,0 +1,76 @@
1
+ require 'bigdecimal'
2
+ require 'bigdecimal/util'
3
+
4
+ module ActsAsPriceable
5
+ module Base
6
+
7
+ def self.included(klass)
8
+ klass.class_eval do
9
+ extend ClassMethods
10
+ end
11
+ end
12
+
13
+ module ClassMethods
14
+ def has_price(name = :price, options = {})
15
+ scale = options[:scale] || 2
16
+ without_validations = options[:without_validations] || false
17
+
18
+ ActsAsPriceable::Schema::COLUMNS.each do |column_name, column_type|
19
+ raise AttributeError, "#{name}_#{column_name} is not defined" if columns_hash["#{name}_#{column_name}"].nil?
20
+ raise AttributeError, "#{name}_#{column_name} has wrong column type (is #{columns_hash["#{name}_#{column_name}"].type}) should be #{column_type}" if columns_hash["#{name}_#{column_name}"].type != column_type
21
+ end
22
+
23
+ gross = "#{name}_gross"
24
+ net = "#{name}_net"
25
+ tax_value = "#{name}_tax_value"
26
+ tax = "#{name}_tax"
27
+ send :define_method, gross do
28
+ BigDecimal.new(self[gross.to_sym].to_i) / BigDecimal.new(10**scale.to_i)
29
+ end
30
+
31
+ send :define_method, "#{gross}=" do |value|
32
+ self[gross.to_sym] = (value.to_s.gsub(',', '.').to_d * (10**scale)).to_i
33
+ end
34
+
35
+ send :define_method, net do
36
+ BigDecimal.new(self[net.to_sym].to_i) / BigDecimal.new(10**scale.to_i)
37
+ end
38
+
39
+ send :define_method, "#{net}=" do |value|
40
+ self[net.to_sym] = (value.to_s.gsub(',', '.').to_d * (10**scale)).to_i
41
+ end
42
+
43
+ send :define_method, tax_value do
44
+ send(gross) - send(net)
45
+ end
46
+
47
+ send :attr_accessor, :mode
48
+ send :attr_accessor, name
49
+
50
+ send :define_method, "update_#{name}" do
51
+ if self.mode == 'net'
52
+ self.send "#{net}=", self.send(name)
53
+ self.send "#{gross}=", self.send(net) * ((BigDecimal.new(self.send(tax)) / 100) + 1)
54
+ else
55
+ self.send "#{gross}=", self.send(name)
56
+ self.send "#{net}=", self.send(gross) / ((BigDecimal.new(self.send(tax)) / 100) + 1)
57
+ end
58
+ end
59
+
60
+ send :before_validation do
61
+ if self.send(name) and self.mode
62
+ self.send "update_#{name}"
63
+ end
64
+ end
65
+
66
+ unless without_validations
67
+ send :validates, gross.to_sym, numericality: {greater_than_or_equal_to: 0}
68
+ send :validates, net.to_sym, numericality: {greater_than_or_equal_to: 0}
69
+ send :validates, tax.to_sym, numericality: {greater_than_or_equal_to: 0}
70
+ end
71
+
72
+ end
73
+
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,72 @@
1
+ module ActsAsPriceable
2
+
3
+ module Schema
4
+ COLUMNS = {
5
+ net: :integer,
6
+ gross: :integer,
7
+ tax: :integer
8
+ }
9
+
10
+ def self.included(base)
11
+ ActiveRecord::ConnectionAdapters::Table.send :include, TableDefinition
12
+ ActiveRecord::ConnectionAdapters::TableDefinition.send :include, TableDefinition
13
+ ActiveRecord::ConnectionAdapters::AbstractAdapter.send :include, Statements
14
+
15
+ if defined?(ActiveRecord::Migration::CommandRecorder) # Rails 3.1+
16
+ ActiveRecord::Migration::CommandRecorder.send :include, CommandRecorder
17
+ end
18
+ end
19
+
20
+ module Statements
21
+
22
+ def add_price(table_name, *price_names)
23
+ raise ArgumentError, "Please specify price name in your add_price call in your migration." if price_names.empty?
24
+
25
+ price_names.each do |price_name|
26
+ COLUMNS.each do |column_name, column_type|
27
+ add_column(table_name, "#{price_name}_#{column_name}", column_type)
28
+ end
29
+ end
30
+ end
31
+
32
+ def remove_price(table_name, *price_names)
33
+ raise ArgumentError, "Please specify price name in your remove_price call in your migration." if price_names.empty?
34
+
35
+ price_names.each do |price_name|
36
+ COLUMNS.each do |column_name, _|
37
+ remove_column(table_name, "#{price_name}_#{column_name}")
38
+ end
39
+ end
40
+ end
41
+
42
+ end
43
+
44
+ module TableDefinition
45
+
46
+ def price(*price_names)
47
+ price_names.each do |price_name|
48
+ COLUMNS.each do |column_name, column_type|
49
+ column("#{price_name}_#{column_name}", column_type)
50
+ end
51
+ end
52
+ end
53
+
54
+ end
55
+
56
+ module CommandRecorder
57
+
58
+ def add_price(*args)
59
+ record(:add_price, args)
60
+ end
61
+
62
+ private
63
+
64
+ def invert_add_price(args)
65
+ [:remove_price, args]
66
+ end
67
+
68
+ end
69
+
70
+ end
71
+
72
+ end
@@ -0,0 +1,3 @@
1
+ module ActsAsPriceable
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,118 @@
1
+ require 'spec_helper'
2
+
3
+ describe Dummy do
4
+
5
+ describe 'class methods' do
6
+ it 'should have has_price method' do
7
+ Dummy.should respond_to(:has_price)
8
+ end
9
+ end
10
+
11
+ describe 'instance' do
12
+
13
+ before(:all) do
14
+ Dummy.send :has_price, :price
15
+ @test_value = '10.20'
16
+ end
17
+
18
+ describe 'methods' do
19
+
20
+
21
+ before(:each) do
22
+ @dummy = Dummy.new
23
+ end
24
+
25
+ it 'should have price attr_accessor' do
26
+ @dummy.should respond_to(:price)
27
+ @dummy.should respond_to(:price=)
28
+ end
29
+
30
+ it 'should have mode attr_accessor' do
31
+ @dummy.should respond_to(:mode)
32
+ @dummy.should respond_to(:mode=)
33
+ end
34
+
35
+ it 'should have price_tax_value method' do
36
+ @dummy.should respond_to(:price_tax_value)
37
+ end
38
+
39
+ it 'should have update_price method' do
40
+ @dummy.should respond_to(:update_price)
41
+ end
42
+
43
+ it 'price_net should be converted to integer' do
44
+ @dummy.price_net = @test_value
45
+ @dummy[:price_net].should be(1020)
46
+ end
47
+
48
+ it 'price_net should return correct value' do
49
+ @dummy.price_net = @test_value
50
+ @dummy.price_net.should == BigDecimal.new(@test_value)
51
+ end
52
+
53
+ it 'price_gross should be converted to integer' do
54
+ @dummy.price_gross = @test_value
55
+ @dummy[:price_gross].should be(1020)
56
+ end
57
+
58
+ it 'price_gross should return correct value' do
59
+ @dummy.price_gross = @test_value
60
+ @dummy.price_gross.should == BigDecimal.new(@test_value)
61
+ end
62
+
63
+ it 'calculates price gross from price_net' do
64
+ @dummy.price = '10.00'
65
+ @dummy.price_tax = 23
66
+ @dummy.mode = 'net'
67
+ @dummy.update_price
68
+ @dummy.price_net.should == BigDecimal.new('10.00')
69
+ @dummy.price_gross.should == BigDecimal.new('12.30')
70
+ end
71
+
72
+ it 'calculates price net from price gross' do
73
+ @dummy.price = '12.30'
74
+ @dummy.price_tax = 23
75
+ @dummy.mode = 'gross'
76
+ @dummy.update_price
77
+ @dummy.price_net.should == BigDecimal.new('10.00')
78
+ @dummy.price_gross.should == BigDecimal.new('12.30')
79
+ end
80
+
81
+ it 'calculates tax value properly' do
82
+ @dummy.price = '10.00'
83
+ @dummy.price_tax = 23
84
+ @dummy.mode = 'net'
85
+ @dummy.update_price
86
+ @dummy.price_tax_value.should == BigDecimal.new('2.30')
87
+ end
88
+
89
+ end
90
+
91
+ describe 'validations' do
92
+
93
+ before(:each) do
94
+ @dummy = Dummy.new price_net: '10.00', price_gross: '12.30', price_tax: 23
95
+ end
96
+
97
+ it 'correct should be valid' do
98
+ @dummy.valid?.should be(true)
99
+ end
100
+
101
+ it "price_net can't be negative" do
102
+ @dummy.price_net = -1
103
+ @dummy.valid?.should be(false)
104
+ end
105
+
106
+ it "price_gross can't be negative" do
107
+ @dummy.price_gross = -1
108
+ @dummy.valid?.should be(false)
109
+ end
110
+
111
+ it "price_tax can't be negative" do
112
+ @dummy.price_tax = -1
113
+ @dummy.valid?.should be(false)
114
+ end
115
+ end
116
+ end
117
+
118
+ end
@@ -0,0 +1,3 @@
1
+ test:
2
+ adapter: sqlite3
3
+ database: ":memory:"
@@ -0,0 +1,2 @@
1
+ class Dummy < ActiveRecord::Base
2
+ end
@@ -0,0 +1,57 @@
1
+ require 'spec_helper'
2
+
3
+ describe ActsAsPriceable::Schema do
4
+
5
+ before(:all) do
6
+ ActiveRecord::Migration.verbose = false
7
+ @migration = Class.new(ActiveRecord::Migration) do
8
+ def change
9
+ create_table :migration_tests, force: true do |t|
10
+ t.price :price
11
+ end
12
+ end
13
+ end
14
+
15
+ @add_migration = Class.new(ActiveRecord::Migration) do
16
+ def change
17
+ add_price :migration_tests, :second_price
18
+ end
19
+ end
20
+ @migration.new.migrate(:up)
21
+ @model = Class.new(ActiveRecord::Base) do
22
+ self.table_name = :migration_tests
23
+ end
24
+ end
25
+
26
+ after(:all) do
27
+ @migration.new.migrate(:down)
28
+ end
29
+
30
+ it 'creates table with price_net, price_gross and price_tax with proper types' do
31
+ columns = @model.columns.map{ |column| [column.name, column.type] }
32
+
33
+ columns.should include(['price_net', :integer])
34
+ columns.should include(['price_tax', :integer])
35
+ columns.should include(['price_gross', :integer])
36
+ end
37
+
38
+ it 'adds proper fields and removes it' do
39
+ @add_migration.new.migrate(:up)
40
+ @model.reset_column_information
41
+ columns = @model.columns.map{ |column| [column.name, column.type] }
42
+
43
+ columns.should include(['second_price_net', :integer])
44
+ columns.should include(['second_price_tax', :integer])
45
+ columns.should include(['second_price_gross', :integer])
46
+
47
+ @add_migration.new.migrate(:down)
48
+ @model.reset_column_information
49
+ columns = @model.columns.map{ |column| [column.name, column.type] }
50
+
51
+ columns.should_not include(['second_price_net', :integer])
52
+ columns.should_not include(['second_price_tax', :integer])
53
+ columns.should_not include(['second_price_gross', :integer])
54
+ end
55
+
56
+
57
+ end
@@ -0,0 +1,20 @@
1
+ require 'rspec'
2
+ require 'acts_as_priceable'
3
+ require 'yaml'
4
+
5
+
6
+ def create_tables
7
+ ActiveRecord::Base.connection.create_table :dummies, force: true do |table|
8
+ table.column :price_gross, :integer
9
+ table.column :price_net, :integer
10
+ table.column :price_tax, :integer
11
+ end
12
+ end
13
+
14
+ RSpec.configure do |config|
15
+ ActiveRecord::Base.establish_connection YAML.load_file("#{File.dirname __FILE__}/database.yml")['test']
16
+ create_tables
17
+ require 'models/dummy'
18
+ config.color_enabled = true
19
+ #config.formatter = 'documentation'
20
+ end
metadata ADDED
@@ -0,0 +1,151 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acts_as_priceable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Paweł Tarczykowski
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-25 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activerecord
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 3.0.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 3.0.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: bigdecimal
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 1.1.0
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 1.1.0
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: 2.10.0
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 2.10.0
62
+ - !ruby/object:Gem::Dependency
63
+ name: rake
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: sqlite3
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ description: Acts as priceable
95
+ email:
96
+ - farrel1@wp.pl
97
+ executables: []
98
+ extensions: []
99
+ extra_rdoc_files: []
100
+ files:
101
+ - .gitignore
102
+ - Gemfile
103
+ - LICENSE
104
+ - README.md
105
+ - Rakefile
106
+ - acts_as_priceable.gemspec
107
+ - lib/acts_as_priceable.rb
108
+ - lib/acts_as_priceable/base.rb
109
+ - lib/acts_as_priceable/schema.rb
110
+ - lib/acts_as_priceable/version.rb
111
+ - spec/base_spec.rb
112
+ - spec/database.yml
113
+ - spec/models/dummy.rb
114
+ - spec/schema_spec.rb
115
+ - spec/spec_helper.rb
116
+ homepage: ''
117
+ licenses: []
118
+ post_install_message:
119
+ rdoc_options: []
120
+ require_paths:
121
+ - lib
122
+ required_ruby_version: !ruby/object:Gem::Requirement
123
+ none: false
124
+ requirements:
125
+ - - ! '>='
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ segments:
129
+ - 0
130
+ hash: 4222123605856346252
131
+ required_rubygems_version: !ruby/object:Gem::Requirement
132
+ none: false
133
+ requirements:
134
+ - - ! '>='
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ segments:
138
+ - 0
139
+ hash: 4222123605856346252
140
+ requirements: []
141
+ rubyforge_project:
142
+ rubygems_version: 1.8.24
143
+ signing_key:
144
+ specification_version: 3
145
+ summary: Acts as priceable
146
+ test_files:
147
+ - spec/base_spec.rb
148
+ - spec/database.yml
149
+ - spec/models/dummy.rb
150
+ - spec/schema_spec.rb
151
+ - spec/spec_helper.rb