formatted_attributes 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +3 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +79 -0
- data/README.rdoc +65 -0
- data/Rakefile +5 -0
- data/formatted_attributes.gemspec +25 -0
- data/lib/formatted_attributes.rb +5 -0
- data/lib/formatted_attributes/active_record.rb +39 -0
- data/lib/formatted_attributes/version.rb +8 -0
- data/spec/formatted_attributes_spec.rb +44 -0
- data/spec/schema.rb +5 -0
- data/spec/spec_helper.rb +25 -0
- metadata +144 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
formatted_attributes (0.1.0)
|
5
|
+
activerecord
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: http://rubygems.org/
|
9
|
+
specs:
|
10
|
+
abstract (1.0.0)
|
11
|
+
actionpack (3.0.1)
|
12
|
+
activemodel (= 3.0.1)
|
13
|
+
activesupport (= 3.0.1)
|
14
|
+
builder (~> 2.1.2)
|
15
|
+
erubis (~> 2.6.6)
|
16
|
+
i18n (~> 0.4.1)
|
17
|
+
rack (~> 1.2.1)
|
18
|
+
rack-mount (~> 0.6.12)
|
19
|
+
rack-test (~> 0.5.4)
|
20
|
+
tzinfo (~> 0.3.23)
|
21
|
+
activemodel (3.0.1)
|
22
|
+
activesupport (= 3.0.1)
|
23
|
+
builder (~> 2.1.2)
|
24
|
+
i18n (~> 0.4.1)
|
25
|
+
activerecord (3.0.1)
|
26
|
+
activemodel (= 3.0.1)
|
27
|
+
activesupport (= 3.0.1)
|
28
|
+
arel (~> 1.0.0)
|
29
|
+
tzinfo (~> 0.3.23)
|
30
|
+
activesupport (3.0.1)
|
31
|
+
archive-tar-minitar (0.5.2)
|
32
|
+
arel (1.0.1)
|
33
|
+
activesupport (~> 3.0.0)
|
34
|
+
builder (2.1.2)
|
35
|
+
columnize (0.3.1)
|
36
|
+
diff-lcs (1.1.2)
|
37
|
+
erubis (2.6.6)
|
38
|
+
abstract (>= 1.0.0)
|
39
|
+
i18n (0.4.1)
|
40
|
+
linecache19 (0.5.11)
|
41
|
+
ruby_core_source (>= 0.1.4)
|
42
|
+
rack (1.2.1)
|
43
|
+
rack-mount (0.6.13)
|
44
|
+
rack (>= 1.0.0)
|
45
|
+
rack-test (0.5.6)
|
46
|
+
rack (>= 1.0)
|
47
|
+
rspec (2.0.1)
|
48
|
+
rspec-core (~> 2.0.1)
|
49
|
+
rspec-expectations (~> 2.0.1)
|
50
|
+
rspec-mocks (~> 2.0.1)
|
51
|
+
rspec-core (2.0.1)
|
52
|
+
rspec-expectations (2.0.1)
|
53
|
+
diff-lcs (>= 1.1.2)
|
54
|
+
rspec-mocks (2.0.1)
|
55
|
+
rspec-core (~> 2.0.1)
|
56
|
+
rspec-expectations (~> 2.0.1)
|
57
|
+
ruby-debug-base19 (0.11.24)
|
58
|
+
columnize (>= 0.3.1)
|
59
|
+
linecache19 (>= 0.5.11)
|
60
|
+
ruby_core_source (>= 0.1.4)
|
61
|
+
ruby-debug19 (0.11.6)
|
62
|
+
columnize (>= 0.3.1)
|
63
|
+
linecache19 (>= 0.5.11)
|
64
|
+
ruby-debug-base19 (>= 0.11.19)
|
65
|
+
ruby_core_source (0.1.4)
|
66
|
+
archive-tar-minitar (>= 0.5.2)
|
67
|
+
sqlite3-ruby (1.3.1)
|
68
|
+
tzinfo (0.3.23)
|
69
|
+
|
70
|
+
PLATFORMS
|
71
|
+
ruby
|
72
|
+
|
73
|
+
DEPENDENCIES
|
74
|
+
actionpack
|
75
|
+
activerecord
|
76
|
+
formatted_attributes!
|
77
|
+
rspec (>= 2.0.0)
|
78
|
+
ruby-debug19
|
79
|
+
sqlite3-ruby
|
data/README.rdoc
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
= Formatted Attributes
|
2
|
+
|
3
|
+
Sometimes you want to use a proxy attribute that will be formatted while displaying its value or receiving data from user.
|
4
|
+
|
5
|
+
== Install
|
6
|
+
|
7
|
+
gem install formatted_attributes
|
8
|
+
|
9
|
+
== Usage
|
10
|
+
|
11
|
+
A formatted attribute will require two methods: <tt>format_to_#{formatter}</tt> and <tt>format_from_#{formatter}</tt>. The <tt>format_to_*</tt> method will convert the formatted value to the original value and the <tt>format_from_*</tt> method will do the other way around, converting the original value to the formatted version.
|
12
|
+
|
13
|
+
See an example on how to format prices from strings like <tt>1,23</tt> to cents like <tt>123</tt> and vice-versa.
|
14
|
+
|
15
|
+
class Product < ActiveRecord::Base
|
16
|
+
include ActionView::Helpers::NumberHelper
|
17
|
+
|
18
|
+
formatted :price, :with => :cents
|
19
|
+
|
20
|
+
private
|
21
|
+
def format_to_cents(amount)
|
22
|
+
_, operator, number, precision = *number.to_s.match(/^([+-])?(\d+)(?:[,.](\d+))?$/)
|
23
|
+
(BigDecimal("#{operator}#{number}.#{precision.to_i}") * 100).to_i
|
24
|
+
end
|
25
|
+
|
26
|
+
def format_from_cents(amount)
|
27
|
+
number = BigDecimal(number.to_s) / 100
|
28
|
+
number_to_currency(number, :unit => "", :separator => ",", :delimiter => "")
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
product = Product.new(:formatted_price => "1,23")
|
33
|
+
product.price
|
34
|
+
#=> 123
|
35
|
+
|
36
|
+
product.price = 456
|
37
|
+
product.formatted_price
|
38
|
+
#=> "4,56"
|
39
|
+
|
40
|
+
== Maintainer
|
41
|
+
|
42
|
+
* Nando Vieira (http://nandovieira.com.br)
|
43
|
+
|
44
|
+
== License
|
45
|
+
|
46
|
+
(The MIT License)
|
47
|
+
|
48
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
49
|
+
a copy of this software and associated documentation files (the
|
50
|
+
'Software'), to deal in the Software without restriction, including
|
51
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
52
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
53
|
+
permit persons to whom the Software is furnished to do so, subject to
|
54
|
+
the following conditions:
|
55
|
+
|
56
|
+
The above copyright notice and this permission notice shall be
|
57
|
+
included in all copies or substantial portions of the Software.
|
58
|
+
|
59
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
60
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
61
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
62
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
63
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
64
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
65
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "formatted_attributes/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "formatted_attributes"
|
7
|
+
s.version = FormattedAttributes::Version::STRING
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Nando Vieira"]
|
10
|
+
s.email = ["fnando.vieira@gmail.com"]
|
11
|
+
s.homepage = "http://rubygems.org/gems/formatted_attributes"
|
12
|
+
s.summary = "Add formatted methods for ActiveRecord attributes"
|
13
|
+
s.description = "Sometimes you need to expose a helper method that will convert its value before saving it to the database. This gem will add `formatted_` suffix to the attributes you specify."
|
14
|
+
|
15
|
+
s.files = `git ls-files`.split("\n")
|
16
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
17
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
18
|
+
s.require_paths = ["lib"]
|
19
|
+
|
20
|
+
s.add_dependency "activerecord"
|
21
|
+
s.add_development_dependency "rspec", ">= 2.0.0"
|
22
|
+
s.add_development_dependency "sqlite3-ruby"
|
23
|
+
s.add_development_dependency "actionpack"
|
24
|
+
s.add_development_dependency "ruby-debug19"
|
25
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module FormattedAttributes
|
2
|
+
module ActiveRecord
|
3
|
+
def self.included(base)
|
4
|
+
base.instance_eval do
|
5
|
+
include InstanceMethods
|
6
|
+
extend ClassMethods
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
def formatted(*args)
|
12
|
+
options = args.extract_options!
|
13
|
+
|
14
|
+
args.each do |attr|
|
15
|
+
accessible_attributes << "formatted_#{attr}"
|
16
|
+
|
17
|
+
class_eval <<-RUBY
|
18
|
+
def #{attr}=(value)
|
19
|
+
@formatted_#{attr} = nil
|
20
|
+
write_attribute :#{attr}, value
|
21
|
+
end
|
22
|
+
|
23
|
+
def formatted_#{attr}
|
24
|
+
@formatted_#{attr} ||= format_from_#{options[:with]}(send(:#{attr}))
|
25
|
+
end
|
26
|
+
|
27
|
+
def formatted_#{attr}=(value)
|
28
|
+
self.send :#{attr}=, format_to_#{options[:with]}(value)
|
29
|
+
@formatted_#{attr} = value
|
30
|
+
end
|
31
|
+
RUBY
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
module InstanceMethods
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe FormattedAttributes do
|
4
|
+
subject { Product.new }
|
5
|
+
|
6
|
+
context "formatted methods" do
|
7
|
+
it "should add getters" do
|
8
|
+
subject.should respond_to(:formatted_price)
|
9
|
+
subject.should respond_to(:formatted_shipping)
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should add setters" do
|
13
|
+
subject.should respond_to(:formatted_price=)
|
14
|
+
subject.should respond_to(:formatted_shipping=)
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should set initial value" do
|
18
|
+
subject.price = 12300
|
19
|
+
subject.formatted_price.should == "123,00"
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should update formatted price" do
|
23
|
+
subject.price = 12300
|
24
|
+
subject.formatted_price.should == "123,00"
|
25
|
+
|
26
|
+
subject.price = 45600
|
27
|
+
subject.formatted_price.should == "456,00"
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should set formatted value" do
|
31
|
+
subject.price = 12300
|
32
|
+
subject.formatted_price = "456,78"
|
33
|
+
subject.formatted_price.should == "456,78"
|
34
|
+
subject.price.should == 45678
|
35
|
+
|
36
|
+
subject.shipping = 67800
|
37
|
+
subject.formatted_shipping = "345,67"
|
38
|
+
subject.formatted_shipping.should == "345,67"
|
39
|
+
subject.shipping.should == 34567
|
40
|
+
|
41
|
+
subject.should be_valid
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
data/spec/schema.rb
ADDED
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require "formatted_attributes"
|
2
|
+
require "active_support/all"
|
3
|
+
require "action_view/helpers/number_helper"
|
4
|
+
|
5
|
+
ActiveRecord::Base.establish_connection :adapter => "sqlite3", :database => ":memory:"
|
6
|
+
|
7
|
+
load File.dirname(__FILE__) + "/schema.rb"
|
8
|
+
|
9
|
+
class Product < ActiveRecord::Base
|
10
|
+
include ActionView::Helpers::NumberHelper
|
11
|
+
formatted :price, :shipping, :with => :cents
|
12
|
+
|
13
|
+
validates_numericality_of :price, :shipping, :only_integer => true
|
14
|
+
|
15
|
+
private
|
16
|
+
def format_to_cents(number)
|
17
|
+
_, operator, number, precision = *number.to_s.match(/^([+-])?(\d+)(?:[,.](\d+))?$/)
|
18
|
+
(BigDecimal("#{operator}#{number}.#{precision.to_i}") * 100).to_i
|
19
|
+
end
|
20
|
+
|
21
|
+
def format_from_cents(number)
|
22
|
+
number = BigDecimal(number.to_s) / 100
|
23
|
+
number_to_currency(number, :unit => "", :separator => ",", :delimiter => "")
|
24
|
+
end
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,144 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: formatted_attributes
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Nando Vieira
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-11-06 00:00:00 -02:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: activerecord
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
segments:
|
29
|
+
- 0
|
30
|
+
version: "0"
|
31
|
+
type: :runtime
|
32
|
+
version_requirements: *id001
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: rspec
|
35
|
+
prerelease: false
|
36
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
37
|
+
none: false
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
segments:
|
42
|
+
- 2
|
43
|
+
- 0
|
44
|
+
- 0
|
45
|
+
version: 2.0.0
|
46
|
+
type: :development
|
47
|
+
version_requirements: *id002
|
48
|
+
- !ruby/object:Gem::Dependency
|
49
|
+
name: sqlite3-ruby
|
50
|
+
prerelease: false
|
51
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
segments:
|
57
|
+
- 0
|
58
|
+
version: "0"
|
59
|
+
type: :development
|
60
|
+
version_requirements: *id003
|
61
|
+
- !ruby/object:Gem::Dependency
|
62
|
+
name: actionpack
|
63
|
+
prerelease: false
|
64
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
segments:
|
70
|
+
- 0
|
71
|
+
version: "0"
|
72
|
+
type: :development
|
73
|
+
version_requirements: *id004
|
74
|
+
- !ruby/object:Gem::Dependency
|
75
|
+
name: ruby-debug19
|
76
|
+
prerelease: false
|
77
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
78
|
+
none: false
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
segments:
|
83
|
+
- 0
|
84
|
+
version: "0"
|
85
|
+
type: :development
|
86
|
+
version_requirements: *id005
|
87
|
+
description: Sometimes you need to expose a helper method that will convert its value before saving it to the database. This gem will add `formatted_` suffix to the attributes you specify.
|
88
|
+
email:
|
89
|
+
- fnando.vieira@gmail.com
|
90
|
+
executables: []
|
91
|
+
|
92
|
+
extensions: []
|
93
|
+
|
94
|
+
extra_rdoc_files: []
|
95
|
+
|
96
|
+
files:
|
97
|
+
- .gitignore
|
98
|
+
- Gemfile
|
99
|
+
- Gemfile.lock
|
100
|
+
- README.rdoc
|
101
|
+
- Rakefile
|
102
|
+
- formatted_attributes.gemspec
|
103
|
+
- lib/formatted_attributes.rb
|
104
|
+
- lib/formatted_attributes/active_record.rb
|
105
|
+
- lib/formatted_attributes/version.rb
|
106
|
+
- spec/formatted_attributes_spec.rb
|
107
|
+
- spec/schema.rb
|
108
|
+
- spec/spec_helper.rb
|
109
|
+
has_rdoc: true
|
110
|
+
homepage: http://rubygems.org/gems/formatted_attributes
|
111
|
+
licenses: []
|
112
|
+
|
113
|
+
post_install_message:
|
114
|
+
rdoc_options: []
|
115
|
+
|
116
|
+
require_paths:
|
117
|
+
- lib
|
118
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
119
|
+
none: false
|
120
|
+
requirements:
|
121
|
+
- - ">="
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
segments:
|
124
|
+
- 0
|
125
|
+
version: "0"
|
126
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
127
|
+
none: false
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
segments:
|
132
|
+
- 0
|
133
|
+
version: "0"
|
134
|
+
requirements: []
|
135
|
+
|
136
|
+
rubyforge_project:
|
137
|
+
rubygems_version: 1.3.7
|
138
|
+
signing_key:
|
139
|
+
specification_version: 3
|
140
|
+
summary: Add formatted methods for ActiveRecord attributes
|
141
|
+
test_files:
|
142
|
+
- spec/formatted_attributes_spec.rb
|
143
|
+
- spec/schema.rb
|
144
|
+
- spec/spec_helper.rb
|