farski-systeme 0.3.2 → 0.4.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/README.rdoc CHANGED
@@ -44,6 +44,23 @@ Conversions allow you to display or pass data in a unit other than the SI base.
44
44
  => 3.28083989501312
45
45
  # 5280 was presumed to be in meters, if you were looking for a result of 1 you should do 5280.feet.in_miles
46
46
 
47
+ == Advanced Usage
48
+
49
+ One useful application of systeme is to take human readable input to produce usable and storable data
50
+
51
+ # How far did you walk today?
52
+ input = "6.25mi"
53
+
54
+ distance = input.to_f
55
+ unit = ((unit_data = input.match(/([a-zA-Z]+)/)) ? unit_data[1] : "m" )
56
+
57
+ si_distance = distance.send(unit)
58
+ => 10058.4
59
+
60
+ == Parsing
61
+
62
+ The parse method provides very lax natural language parsing of dimensioned numbers in strings. It works much as one would expect, but two things to note: it will simply ignore any numbers associated with labels it doesn't understand, and it doesn't care if all the numbers in the string are of the same dimensionality. A string like "1 meter 1 centimeter" 1.01 and the string "1 meter 1 kilogram" will return 2.0. The same unit label can appear many times in a single string ("1m 1m 1m" => 3.0). If the parser finds a number with no label, it is technically being dimensioned as 'meter', which has no effect on the value.
63
+
47
64
  = Installation
48
65
 
49
66
  === As a gem
@@ -54,7 +71,7 @@ The official distributions of this gem are available on RubyForge under the proj
54
71
 
55
72
  $ gem install systeme
56
73
 
57
- Development can be followed on GitHub at http://www.gihub.com/farski/systeme
74
+ Development can be followed on GitHub at http://www.github.com/farski/systeme
58
75
 
59
76
  $ gem install farski-systeme
60
77
 
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
- :minor: 3
3
- :patch: 2
2
+ :minor: 4
3
+ :patch: 0
4
4
  :major: 0
data/lib/systeme.rb CHANGED
@@ -13,6 +13,22 @@ module Systeme
13
13
  include Systeme::Imperial
14
14
  include Systeme::Length
15
15
  include Systeme::Conversions
16
+
17
+ def self.parse(string)
18
+ return nil if (!string || !string.match(/\d/))
19
+
20
+ string.gsub!(/ */, '').gsub!(/([a-zA-Z])(\d)/, '\1 \2')
21
+ parts = string.split(' ')
22
+ total = parts.inject(0) do |total, part|
23
+ value = part.to_f
24
+ label = ((l = part.match(/([a-zA-Z]+)/)) ? l[1] : "m")
25
+ total += (value.respond_to?(label) ? value.send(label) : 0)
26
+ end
27
+
28
+ return total
29
+ end
16
30
  end
17
31
 
18
- Numeric.send :include, Systeme
32
+ Numeric.send :include, Systeme
33
+
34
+ puts Systeme.parse('1m')
data/test/metric_test.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  require File.dirname(__FILE__) + '/test_helper'
2
2
 
3
- class ImperialTest < Test::Unit::TestCase
3
+ class MetricTest < Test::Unit::TestCase
4
4
  should "convert carat to kilogram" do
5
5
  assert_equal 0.0002, 1.carat
6
6
  end
@@ -0,0 +1,33 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class ParseTest < Test::Unit::TestCase
4
+ should "parse a single base unit string" do
5
+ assert_equal 1, Systeme.parse('1m')
6
+ assert_equal 1, Systeme.parse('1 meter')
7
+ assert_equal 1, Systeme.parse('1 meters')
8
+ assert_equal 1, Systeme.parse('1 m')
9
+ end
10
+
11
+ should "parse a single non-base unit string" do
12
+ assert_equal 1000, Systeme.parse('1km')
13
+ assert_equal 1000, Systeme.parse('1 kilometer')
14
+ assert_equal 1000, Systeme.parse('1 kilometers')
15
+ assert_equal 1000, Systeme.parse('1 km')
16
+ end
17
+
18
+ should "parse a complex string" do
19
+ assert_equal 1001, Systeme.parse('1km 1m')
20
+ assert_equal 1001, Systeme.parse('1 km 1 m')
21
+ assert_equal 1001, Systeme.parse('1kilometer1meter')
22
+ assert_equal 1001, Systeme.parse('1 kilometer 1 meter')
23
+ end
24
+
25
+ should "parse a complex string with non-SI units" do
26
+ assert_equal 1609.344, Systeme.parse('1 mile')
27
+ assert_equal 1609.3694, Systeme.parse('1 mi 1in')
28
+ end
29
+
30
+ should "parse a complex string with units from different systems" do
31
+ assert_equal 1610.344, Systeme.parse('1 mile 1m')
32
+ end
33
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: farski-systeme
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Kalafarski
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-01-17 00:00:00 -08:00
12
+ date: 2009-02-25 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -36,12 +36,14 @@ files:
36
36
  - test/imperial_test.rb
37
37
  - test/international_test.rb
38
38
  - test/metric_test.rb
39
+ - test/parse_test.rb
39
40
  - test/test_helper.rb
40
- has_rdoc: false
41
+ has_rdoc: true
41
42
  homepage: http://github.com/farski/systeme
42
43
  post_install_message:
43
- rdoc_options: []
44
-
44
+ rdoc_options:
45
+ - --inline-source
46
+ - --charset=UTF-8
45
47
  require_paths:
46
48
  - lib
47
49
  required_ruby_version: !ruby/object:Gem::Requirement