priceable 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +20 -13
- data/lib/priceable.rb +5 -1
- data/lib/priceable/version.rb +1 -1
- data/spec/priceable_spec.rb +6 -2
- metadata +4 -4
data/README.md
CHANGED
@@ -6,7 +6,9 @@ Easily manage prices in your Rails application
|
|
6
6
|
|
7
7
|
Add this line to your application's Gemfile:
|
8
8
|
|
9
|
-
|
9
|
+
```ruby
|
10
|
+
gem 'priceable'
|
11
|
+
```
|
10
12
|
|
11
13
|
And then execute:
|
12
14
|
|
@@ -21,26 +23,31 @@ Say you have a model, `Llama`. You can buy `Llama`s for a `cost`. Let's add th
|
|
21
23
|
|
22
24
|
Now we just need to tell Rails that the `cost` field is priceable
|
23
25
|
|
24
|
-
|
26
|
+
```ruby
|
27
|
+
# app/models/llama.rb
|
25
28
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
+
class Llama < ActiveRecord::Base
|
30
|
+
priceable :cost
|
31
|
+
end
|
32
|
+
```
|
29
33
|
|
30
|
-
And now lets check it out
|
34
|
+
And now lets check it out in the console
|
31
35
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
36
|
+
```ruby
|
37
|
+
>> llama = Llama.last
|
38
|
+
>> llama.cost # => nil
|
39
|
+
>> llama.cost = 100
|
40
|
+
>> llama.cost_in_cents # => 10000
|
41
|
+
>> llama.cost # => 100
|
42
|
+
```
|
38
43
|
|
39
44
|
Isn't that special. So go, and add prices easily to your models, with Priceable.
|
40
45
|
|
41
46
|
PS: `priceable` can take more than one field at a time, so go ahead and load it up like a baked potato.
|
42
47
|
|
43
|
-
|
48
|
+
```ruby
|
49
|
+
priceable :retail_price, :employee_price, :cost
|
50
|
+
```
|
44
51
|
|
45
52
|
## Contributing
|
46
53
|
|
data/lib/priceable.rb
CHANGED
@@ -7,7 +7,11 @@ module Priceable
|
|
7
7
|
suffix = SUFFIXES.detect { |suffix| self.attribute_method? "#{price_field}#{suffix}".to_sym }
|
8
8
|
raise ArgumentError, "Unable to find valid database field for `#{price_field}'" unless suffix
|
9
9
|
define_method price_field do
|
10
|
-
send("#{price_field}#{suffix}".to_sym)
|
10
|
+
unless send("#{price_field}#{suffix}".to_sym).nil?
|
11
|
+
send("#{price_field}#{suffix}".to_sym) / 100.0
|
12
|
+
else
|
13
|
+
0.0
|
14
|
+
end
|
11
15
|
end
|
12
16
|
define_method "#{price_field}=".to_sym do |new_price|
|
13
17
|
send("#{price_field}#{suffix}=".to_sym, new_price.to_f * 100)
|
data/lib/priceable/version.rb
CHANGED
data/spec/priceable_spec.rb
CHANGED
@@ -9,10 +9,14 @@ describe Priceable do
|
|
9
9
|
end
|
10
10
|
|
11
11
|
it "should create a setter method" do
|
12
|
-
llama.price = 200
|
13
|
-
llama.price_in_cents.should ==
|
12
|
+
llama.price = 200.10
|
13
|
+
llama.price_in_cents.should == 20010
|
14
14
|
end
|
15
15
|
|
16
|
+
it "should return 0.0 if nil" do
|
17
|
+
llama.price.should == 0.0
|
18
|
+
end
|
19
|
+
|
16
20
|
it "should set it as attr_accessible" do
|
17
21
|
Llama.accessible_attributes.to_a.should include("price")
|
18
22
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: priceable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-05-
|
12
|
+
date: 2012-05-11 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
@@ -132,7 +132,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
132
132
|
version: '0'
|
133
133
|
segments:
|
134
134
|
- 0
|
135
|
-
hash: -
|
135
|
+
hash: -1066120859654096857
|
136
136
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
137
137
|
none: false
|
138
138
|
requirements:
|
@@ -141,7 +141,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
141
141
|
version: '0'
|
142
142
|
segments:
|
143
143
|
- 0
|
144
|
-
hash: -
|
144
|
+
hash: -1066120859654096857
|
145
145
|
requirements: []
|
146
146
|
rubyforge_project:
|
147
147
|
rubygems_version: 1.8.23
|