acts_as_percentage 0.1.0
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.
- data/.gitignore +5 -0
- data/Gemfile +4 -0
- data/README.md +25 -0
- data/Rakefile +1 -0
- data/acts_as_percentage.gemspec +28 -0
- data/lib/acts_as_percentage.rb +50 -0
- data/lib/acts_as_percentage/version.rb +3 -0
- data/spec/lib/acts_as_percentage_spec.rb +39 -0
- data/spec/spec_helper.rb +7 -0
- data/spec/support/data.rb +1 -0
- data/spec/support/models.rb +3 -0
- data/spec/support/schema.rb +8 -0
- metadata +108 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# Installing
|
2
|
+
`gem "acts_as_percentage"`
|
3
|
+
|
4
|
+
# Requirements
|
5
|
+
Rails 3.1.x
|
6
|
+
|
7
|
+
# Usage
|
8
|
+
|
9
|
+
Allows you to store percent values as an integer.
|
10
|
+
|
11
|
+
class ProgressBar < ActiveRecord::Base
|
12
|
+
percentage :completed
|
13
|
+
end
|
14
|
+
|
15
|
+
|
16
|
+
This assumes there is a column in the database named `completed_basis_point (Integer)`. It creates
|
17
|
+
several helper methods for you:
|
18
|
+
|
19
|
+
pb = ProgressBar.create(:completed => 59.87) # => <ProgressBar: @completed_basis_point=5987>
|
20
|
+
pb.completed # => 59.87
|
21
|
+
pb.completed_ratio # => 0.5987
|
22
|
+
pb.completed_basis_point # => 5987
|
23
|
+
|
24
|
+
|
25
|
+
Raises `ArgumentError` if the column specified is not present
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "acts_as_percentage/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "acts_as_percentage"
|
7
|
+
s.version = ActsAsPercentage::VERSION
|
8
|
+
s.authors = ["Jared McFarland"]
|
9
|
+
s.email = ["jared.online@gmail.com"]
|
10
|
+
s.homepage = ""
|
11
|
+
s.summary = "Adds simple percentage and ratio helpers to ActiveRecord and allows percents to be stored as an Integer"
|
12
|
+
s.description = "Because most percentages only need to be accurate to two decimals, this gem has you store them as basis points and does the conversion for you."
|
13
|
+
|
14
|
+
s.rubyforge_project = "acts_as_percentage"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
s.add_dependency "activerecord", "~> 3.1.0"
|
22
|
+
|
23
|
+
# specify any dependencies here; for example:
|
24
|
+
s.add_development_dependency "rspec", "~> 2.6"
|
25
|
+
s.add_development_dependency "bundler", "~> 1.0.0"
|
26
|
+
s.add_development_dependency "sqlite3-ruby"
|
27
|
+
# s.add_runtime_dependency "rest-client"
|
28
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require "active_record"
|
2
|
+
require "acts_as_percentage/version"
|
3
|
+
|
4
|
+
module ActsAsPercentage #:nodoc:
|
5
|
+
def self.extended(base)
|
6
|
+
base.extend(ClassMethods)
|
7
|
+
end
|
8
|
+
|
9
|
+
module ClassMethods #:nodoc:
|
10
|
+
|
11
|
+
# Allows you to store percent values as an integer.
|
12
|
+
#
|
13
|
+
# class ProgressBar < ActiveRecord::Base
|
14
|
+
# percentage :completed
|
15
|
+
# end
|
16
|
+
#
|
17
|
+
# This assumes there is a column in the database named <tt>completed_basis_point (Integer)</tt>. It creates
|
18
|
+
# several helper methods for you:
|
19
|
+
#
|
20
|
+
# pb = ProgressBar.create(:completed => 59.87) # => <ProgressBar: @completed_basis_point=5987>
|
21
|
+
# pb.completed # => 59.87
|
22
|
+
# pb.completed_ratio # => 0.5987
|
23
|
+
# pb.completed_basis_point # => 5987
|
24
|
+
#
|
25
|
+
# Raises <tt>ArgumentError</tt> if the column specified is not present
|
26
|
+
#
|
27
|
+
def percentage(*attributes)
|
28
|
+
attributes.each do |attribute|
|
29
|
+
basis_point_attribute = "#{attribute}_basis_point"
|
30
|
+
raise ArgumentError.new("#{self} does not respond to #{basis_point_attribute}") unless self.column_names.include?("#{basis_point_attribute}")
|
31
|
+
|
32
|
+
define_method("#{attribute}") do
|
33
|
+
self.__send__(basis_point_attribute) / 100.0 rescue nil
|
34
|
+
end
|
35
|
+
|
36
|
+
define_method("#{attribute}_ratio") do
|
37
|
+
self.__send__(basis_point_attribute) / 10000.0 rescue nil
|
38
|
+
end
|
39
|
+
|
40
|
+
define_method("#{attribute}=") do |percentage|
|
41
|
+
basis_point = percentage ? (Float(percentage) * 100).round : nil
|
42
|
+
self.__send__("#{basis_point_attribute}=", basis_point)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
|
50
|
+
ActiveRecord::Base.extend ActsAsPercentage
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "acts_as_percentage" do
|
4
|
+
let(:progress_bar) { ProgressBar.create(:completed_basis_point => 7569) }
|
5
|
+
|
6
|
+
it "has completed" do
|
7
|
+
progress_bar.completed.should eql(75.69)
|
8
|
+
end
|
9
|
+
|
10
|
+
it "has completed_basis_point" do
|
11
|
+
progress_bar.completed_basis_point.should eql(7569)
|
12
|
+
end
|
13
|
+
|
14
|
+
it "has completed_ratio" do
|
15
|
+
progress_bar.completed_ratio.should eql(0.7569)
|
16
|
+
end
|
17
|
+
|
18
|
+
it "allows the setting of a perctage" do
|
19
|
+
progress_bar.completed = 89.19
|
20
|
+
progress_bar.completed_basis_point.should eql(8919)
|
21
|
+
progress_bar.completed.should eql(89.19)
|
22
|
+
progress_bar.completed_ratio.should eql(0.8919)
|
23
|
+
end
|
24
|
+
|
25
|
+
it "allows setting of nil" do
|
26
|
+
progress_bar.completed = nil
|
27
|
+
progress_bar.completed_basis_point.should be_nil
|
28
|
+
progress_bar.completed.should be_nil
|
29
|
+
progress_bar.completed_ratio.should be_nil
|
30
|
+
end
|
31
|
+
|
32
|
+
it "allows setting with a string" do
|
33
|
+
lambda { ProgressBar.create(:completed => "59.87") }.should_not raise_error
|
34
|
+
end
|
35
|
+
|
36
|
+
it "raises an error if an unspecified column is present" do
|
37
|
+
lambda { class ProgressBar < ActiveRecord::Base; percentage :foo; end }.should raise_error(ArgumentError)
|
38
|
+
end
|
39
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
require 'acts_as_percentage'
|
2
|
+
|
3
|
+
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => File.dirname(__FILE__) + "/acts_as_percentage.sqlite3")
|
4
|
+
|
5
|
+
load File.dirname(__FILE__) + '/support/schema.rb'
|
6
|
+
load File.dirname(__FILE__) + '/support/models.rb'
|
7
|
+
load File.dirname(__FILE__) + '/support/data.rb'
|
@@ -0,0 +1 @@
|
|
1
|
+
# Put seed data here
|
metadata
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: acts_as_percentage
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Jared McFarland
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-01-21 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activerecord
|
16
|
+
requirement: &70265346777660 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 3.1.0
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70265346777660
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rspec
|
27
|
+
requirement: &70265346777020 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ~>
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '2.6'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70265346777020
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: bundler
|
38
|
+
requirement: &70265346776520 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ~>
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 1.0.0
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *70265346776520
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: sqlite3-ruby
|
49
|
+
requirement: &70265346775980 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *70265346775980
|
58
|
+
description: Because most percentages only need to be accurate to two decimals, this
|
59
|
+
gem has you store them as basis points and does the conversion for you.
|
60
|
+
email:
|
61
|
+
- jared.online@gmail.com
|
62
|
+
executables: []
|
63
|
+
extensions: []
|
64
|
+
extra_rdoc_files: []
|
65
|
+
files:
|
66
|
+
- .gitignore
|
67
|
+
- Gemfile
|
68
|
+
- README.md
|
69
|
+
- Rakefile
|
70
|
+
- acts_as_percentage.gemspec
|
71
|
+
- lib/acts_as_percentage.rb
|
72
|
+
- lib/acts_as_percentage/version.rb
|
73
|
+
- spec/lib/acts_as_percentage_spec.rb
|
74
|
+
- spec/spec_helper.rb
|
75
|
+
- spec/support/data.rb
|
76
|
+
- spec/support/models.rb
|
77
|
+
- spec/support/schema.rb
|
78
|
+
homepage: ''
|
79
|
+
licenses: []
|
80
|
+
post_install_message:
|
81
|
+
rdoc_options: []
|
82
|
+
require_paths:
|
83
|
+
- lib
|
84
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
85
|
+
none: false
|
86
|
+
requirements:
|
87
|
+
- - ! '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
|
+
none: false
|
92
|
+
requirements:
|
93
|
+
- - ! '>='
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '0'
|
96
|
+
requirements: []
|
97
|
+
rubyforge_project: acts_as_percentage
|
98
|
+
rubygems_version: 1.8.6
|
99
|
+
signing_key:
|
100
|
+
specification_version: 3
|
101
|
+
summary: Adds simple percentage and ratio helpers to ActiveRecord and allows percents
|
102
|
+
to be stored as an Integer
|
103
|
+
test_files:
|
104
|
+
- spec/lib/acts_as_percentage_spec.rb
|
105
|
+
- spec/spec_helper.rb
|
106
|
+
- spec/support/data.rb
|
107
|
+
- spec/support/models.rb
|
108
|
+
- spec/support/schema.rb
|