woahdae-units 1.0.3 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/units/active_record.rb +36 -0
- data/test/units/active_record_test.rb +41 -0
- data/units.gemspec +5 -2
- metadata +5 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.0
|
1
|
+
1.1.0
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module Units
|
2
|
+
module ActiveRecord
|
3
|
+
def acts_as_units(*methods)
|
4
|
+
methods.each do |method|
|
5
|
+
define_method(method) do
|
6
|
+
unit_attr = "#{method}_unit".to_sym
|
7
|
+
self[unit_attr].blank? ? self[method] : self[method].send(self[unit_attr])
|
8
|
+
end
|
9
|
+
|
10
|
+
define_method("#{method}=".to_sym) do |value|
|
11
|
+
if value.is_a?(String) && value =~ /^(.*?) (\w.*?)$/
|
12
|
+
amount = $1
|
13
|
+
unit = $2
|
14
|
+
elsif value.respond_to?(:unit) && value.unit
|
15
|
+
amount = value
|
16
|
+
unit = value.unit
|
17
|
+
else
|
18
|
+
amount = value
|
19
|
+
unit = nil
|
20
|
+
end
|
21
|
+
|
22
|
+
amount = amount.to_f
|
23
|
+
amount = amount.send(unit) if unit
|
24
|
+
|
25
|
+
self[method] = amount
|
26
|
+
self["#{method}_unit".to_sym] = self.attributes["#{method}_unit"] || unit
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
class ActiveRecord::Base
|
34
|
+
extend Units::ActiveRecord
|
35
|
+
end
|
36
|
+
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../test_helper'
|
2
|
+
require 'rubygems'
|
3
|
+
require 'activerecord'
|
4
|
+
require 'units/active_record'
|
5
|
+
require 'ruby-debug'
|
6
|
+
ActiveRecord::Base.establish_connection(
|
7
|
+
:adapter => 'sqlite3',
|
8
|
+
:database => ":memory:"
|
9
|
+
)
|
10
|
+
|
11
|
+
ActiveRecord::Schema.define do
|
12
|
+
create_table "items", :force => true do |t|
|
13
|
+
t.float :quantity
|
14
|
+
t.string :quantity_unit
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
class Item < ActiveRecord::Base
|
19
|
+
acts_as_units :quantity
|
20
|
+
end
|
21
|
+
|
22
|
+
|
23
|
+
class ActiveRecordTest < Test::Unit::TestCase
|
24
|
+
def test_accessor_gets_float_with_unit
|
25
|
+
item = Item.create!(:quantity => 5, :quantity_unit => "lbs")
|
26
|
+
item.reload
|
27
|
+
assert_equal :pounds, item.quantity.unit
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_writer_sets_qty_and_qty_unit_given_a_string
|
31
|
+
item = Item.new(:quantity => "5 lbs")
|
32
|
+
assert_equal :pounds, item.quantity.unit
|
33
|
+
assert_equal "lbs", item.quantity_unit
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_writer_sets_qty_and_qty_unit_given_a_numeric_with_unit
|
37
|
+
item = Item.new(:quantity => 5.pounds)
|
38
|
+
assert_equal :pounds, item.quantity.unit
|
39
|
+
assert_equal :pounds, item.quantity_unit
|
40
|
+
end
|
41
|
+
end
|
data/units.gemspec
CHANGED
@@ -2,11 +2,11 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = %q{units}
|
5
|
-
s.version = "1.0
|
5
|
+
s.version = "1.1.0"
|
6
6
|
|
7
7
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
8
|
s.authors = ["Lucas Carlson"]
|
9
|
-
s.date = %q{2009-07-
|
9
|
+
s.date = %q{2009-07-31}
|
10
10
|
s.description = %q{A simple way to add units and conversion ability to numbers in Ruby}
|
11
11
|
s.email = %q{lucas@rufy.com}
|
12
12
|
s.extra_rdoc_files = [
|
@@ -19,10 +19,12 @@ Gem::Specification.new do |s|
|
|
19
19
|
"Rakefile",
|
20
20
|
"VERSION",
|
21
21
|
"lib/units.rb",
|
22
|
+
"lib/units/active_record.rb",
|
22
23
|
"lib/units/base.rb",
|
23
24
|
"lib/units/currency.rb",
|
24
25
|
"lib/units/standard.rb",
|
25
26
|
"test/test_helper.rb",
|
27
|
+
"test/units/active_record_test.rb",
|
26
28
|
"test/units/base_test.rb",
|
27
29
|
"test/units/currency_test.rb",
|
28
30
|
"test/units/standard_test.rb",
|
@@ -36,6 +38,7 @@ Gem::Specification.new do |s|
|
|
36
38
|
s.summary = %q{units/conversions Gem}
|
37
39
|
s.test_files = [
|
38
40
|
"test/test_helper.rb",
|
41
|
+
"test/units/active_record_test.rb",
|
39
42
|
"test/units/base_test.rb",
|
40
43
|
"test/units/currency_test.rb",
|
41
44
|
"test/units/standard_test.rb"
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: woahdae-units
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Lucas Carlson
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-07-
|
12
|
+
date: 2009-07-31 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -28,10 +28,12 @@ files:
|
|
28
28
|
- Rakefile
|
29
29
|
- VERSION
|
30
30
|
- lib/units.rb
|
31
|
+
- lib/units/active_record.rb
|
31
32
|
- lib/units/base.rb
|
32
33
|
- lib/units/currency.rb
|
33
34
|
- lib/units/standard.rb
|
34
35
|
- test/test_helper.rb
|
36
|
+
- test/units/active_record_test.rb
|
35
37
|
- test/units/base_test.rb
|
36
38
|
- test/units/currency_test.rb
|
37
39
|
- test/units/standard_test.rb
|
@@ -65,6 +67,7 @@ specification_version: 2
|
|
65
67
|
summary: units/conversions Gem
|
66
68
|
test_files:
|
67
69
|
- test/test_helper.rb
|
70
|
+
- test/units/active_record_test.rb
|
68
71
|
- test/units/base_test.rb
|
69
72
|
- test/units/currency_test.rb
|
70
73
|
- test/units/standard_test.rb
|