tape_measure 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +22 -0
- data/.rspec +2 -0
- data/.travis.yml +6 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +59 -0
- data/README.md +44 -0
- data/Rakefile +7 -0
- data/lib/tape_measure/formatter.rb +45 -0
- data/lib/tape_measure/length_grammar.citrus +122 -0
- data/lib/tape_measure/parser.rb +29 -0
- data/lib/tape_measure/version.rb +3 -0
- data/lib/tape_measure.rb +8 -0
- data/spec/spec_helper.rb +19 -0
- data/spec/tape_measure/formatter_spec.rb +26 -0
- data/spec/tape_measure/parser_spec.rb +182 -0
- data/tape_measure.gemspec +26 -0
- metadata +134 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 93b93a3d35fe674240ee1b437dfce34bf07f8b79
|
4
|
+
data.tar.gz: a15b6939c253701e737ccdda1abc1a809e874d72
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 987529dd302b6245d280e3ddb27b60418d8b56dc72ac69910cb30a4eeee2b676273eeb936ddab2f2a35208ad3d92c3e1b21ce0f8fa03f79e747c6e6977f8912d
|
7
|
+
data.tar.gz: e88bf6448952931a795942eaecbb7ea97f1c41b4318ad0fa594f3ccf32c18b1e9e76c3968d01feea990fb2c7b58a7cfa8569a64de2049dddeb194c8553047693
|
data/.gitignore
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
Copyright (c) 2014 burningpony corp
|
2
|
+
|
3
|
+
GNU LESSER GENERAL PUBLIC LICENSE
|
4
|
+
|
5
|
+
Version 3, 29 June 2007
|
6
|
+
|
7
|
+
Copyright © 2007 Free Software Foundation, Inc. <http://fsf.org/>
|
8
|
+
|
9
|
+
Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed.
|
10
|
+
|
11
|
+
This version of the GNU Lesser General Public License incorporates the terms and conditions of version 3 of the GNU General Public License, supplemented by the additional permissions listed below.
|
12
|
+
|
13
|
+
0. Additional Definitions.
|
14
|
+
As used herein, “this License” refers to version 3 of the GNU Lesser General Public License, and the “GNU GPL” refers to version 3 of the GNU General Public License.
|
15
|
+
|
16
|
+
“The Library” refers to a covered work governed by this License, other than an Application or a Combined Work as defined below.
|
17
|
+
|
18
|
+
An “Application” is any work that makes use of an interface provided by the Library, but which is not otherwise based on the Library. Defining a subclass of a class defined by the Library is deemed a mode of using an interface provided by the Library.
|
19
|
+
|
20
|
+
A “Combined Work” is a work produced by combining or linking an Application with the Library. The particular version of the Library with which the Combined Work was made is also called the “Linked Version”.
|
21
|
+
|
22
|
+
The “Minimal Corresponding Source” for a Combined Work means the Corresponding Source for the Combined Work, excluding any source code for portions of the Combined Work that, considered in isolation, are based on the Application, and not on the Linked Version.
|
23
|
+
|
24
|
+
The “Corresponding Application Code” for a Combined Work means the object code and/or source code for the Application, including any data and utility programs needed for reproducing the Combined Work from the Application, but excluding the System Libraries of the Combined Work.
|
25
|
+
|
26
|
+
1. Exception to Section 3 of the GNU GPL.
|
27
|
+
You may convey a covered work under sections 3 and 4 of this License without being bound by section 3 of the GNU GPL.
|
28
|
+
|
29
|
+
2. Conveying Modified Versions.
|
30
|
+
If you modify a copy of the Library, and, in your modifications, a facility refers to a function or data to be supplied by an Application that uses the facility (other than as an argument passed when the facility is invoked), then you may convey a copy of the modified version:
|
31
|
+
|
32
|
+
a) under this License, provided that you make a good faith effort to ensure that, in the event an Application does not supply the function or data, the facility still operates, and performs whatever part of its purpose remains meaningful, or
|
33
|
+
b) under the GNU GPL, with none of the additional permissions of this License applicable to that copy.
|
34
|
+
3. Object Code Incorporating Material from Library Header Files.
|
35
|
+
The object code form of an Application may incorporate material from a header file that is part of the Library. You may convey such object code under terms of your choice, provided that, if the incorporated material is not limited to numerical parameters, data structure layouts and accessors, or small macros, inline functions and templates (ten or fewer lines in length), you do both of the following:
|
36
|
+
|
37
|
+
a) Give prominent notice with each copy of the object code that the Library is used in it and that the Library and its use are covered by this License.
|
38
|
+
b) Accompany the object code with a copy of the GNU GPL and this license document.
|
39
|
+
4. Combined Works.
|
40
|
+
You may convey a Combined Work under terms of your choice that, taken together, effectively do not restrict modification of the portions of the Library contained in the Combined Work and reverse engineering for debugging such modifications, if you also do each of the following:
|
41
|
+
|
42
|
+
a) Give prominent notice with each copy of the Combined Work that the Library is used in it and that the Library and its use are covered by this License.
|
43
|
+
b) Accompany the Combined Work with a copy of the GNU GPL and this license document.
|
44
|
+
c) For a Combined Work that displays copyright notices during execution, include the copyright notice for the Library among these notices, as well as a reference directing the user to the copies of the GNU GPL and this license document.
|
45
|
+
d) Do one of the following:
|
46
|
+
0) Convey the Minimal Corresponding Source under the terms of this License, and the Corresponding Application Code in a form suitable for, and under terms that permit, the user to recombine or relink the Application with a modified version of the Linked Version to produce a modified Combined Work, in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source.
|
47
|
+
1) Use a suitable shared library mechanism for linking with the Library. A suitable mechanism is one that (a) uses at run time a copy of the Library already present on the user's computer system, and (b) will operate properly with a modified version of the Library that is interface-compatible with the Linked Version.
|
48
|
+
e) Provide Installation Information, but only if you would otherwise be required to provide such information under section 6 of the GNU GPL, and only to the extent that such information is necessary to install and execute a modified version of the Combined Work produced by recombining or relinking the Application with a modified version of the Linked Version. (If you use option 4d0, the Installation Information must accompany the Minimal Corresponding Source and Corresponding Application Code. If you use option 4d1, you must provide the Installation Information in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source.)
|
49
|
+
5. Combined Libraries.
|
50
|
+
You may place library facilities that are a work based on the Library side by side in a single library together with other library facilities that are not Applications and are not covered by this License, and convey such a combined library under terms of your choice, if you do both of the following:
|
51
|
+
|
52
|
+
a) Accompany the combined library with a copy of the same work based on the Library, uncombined with any other library facilities, conveyed under the terms of this License.
|
53
|
+
b) Give prominent notice with the combined library that part of it is a work based on the Library, and explaining where to find the accompanying uncombined form of the same work.
|
54
|
+
6. Revised Versions of the GNU Lesser General Public License.
|
55
|
+
The Free Software Foundation may publish revised and/or new versions of the GNU Lesser General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns.
|
56
|
+
|
57
|
+
Each version is given a distinguishing version number. If the Library as you received it specifies that a certain numbered version of the GNU Lesser General Public License “or any later version” applies to it, you have the option of following the terms and conditions either of that published version or of any later version published by the Free Software Foundation. If the Library as you received it does not specify a version number of the GNU Lesser General Public License, you may choose any version of the GNU Lesser General Public License ever published by the Free Software Foundation.
|
58
|
+
|
59
|
+
If the Library as you received it specifies that a proxy can decide whether future versions of the GNU Lesser General Public License shall apply, that proxy's public statement of acceptance of any version is permanent authorization for you to choose that version for the Library.
|
data/README.md
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
# TapeMeasure
|
2
|
+
|
3
|
+
[![Code Climate](https://codeclimate.com/github/burningpony/tape_measure.png)](https://codeclimate.com/github/burningpony/tape_measure)
|
4
|
+
[![Build Status](https://travis-ci.org/burningpony/tape_measure.svg?branch=master)](https://travis-ci.org/burningpony/tape_measure)
|
5
|
+
|
6
|
+
Contains a text parser for converting various measurements into their inch :/ equivilent, as well as doing multiplication with those numbers:
|
7
|
+
|
8
|
+
TapeMeasure::Parser.new('6ft').parse = 72
|
9
|
+
TapeMeasure::Parser.new('6in').parse = 6
|
10
|
+
TapeMeasure::Parser.new('6ft 6in').parse = 78
|
11
|
+
TapeMeasure::Parser.new('6 * 6').parse = 36
|
12
|
+
TapeMeasure::Parser.new('(6ft 6in) + 6in').parse = 84
|
13
|
+
|
14
|
+
Also contains a formatter that converts a measurement to all of it's corresponding measurements:
|
15
|
+
|
16
|
+
.to_mixed_number('12"') = ['1ft', '12in', '#m', etc]
|
17
|
+
|
18
|
+
## Installation:
|
19
|
+
|
20
|
+
|
21
|
+
Add this line to your application's Gemfile:
|
22
|
+
|
23
|
+
gem 'tape_measure'
|
24
|
+
|
25
|
+
And then execute:
|
26
|
+
|
27
|
+
$ bundle
|
28
|
+
|
29
|
+
Or install it yourself as:
|
30
|
+
|
31
|
+
$ gem install tape_measure
|
32
|
+
|
33
|
+
## Usage
|
34
|
+
|
35
|
+
Used for parsing various measurements into a constant measurement. m, cm, ft, in all converted to the corresponding inch
|
36
|
+
value. Useful for unpredictable input types, common in things such as room size and lumber measurements.
|
37
|
+
|
38
|
+
## Contributing
|
39
|
+
|
40
|
+
1. Fork it ( https://github.com/[my-github-username]/tape_measure/fork )
|
41
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
42
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
43
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
44
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
module TapeMeasure
|
2
|
+
# Format numbers in mixed numbers
|
3
|
+
class Formatter
|
4
|
+
attr_reader :mixed_number
|
5
|
+
|
6
|
+
def initialize(number = 0.0)
|
7
|
+
@number = number
|
8
|
+
@unit_array = []
|
9
|
+
@feet = @number.div(12)
|
10
|
+
@raw_inches = @number.remainder(12)
|
11
|
+
@inches = @raw_inches.to_i
|
12
|
+
@fraction = @raw_inches.remainder(1).to_r.rationalize(0.01)
|
13
|
+
|
14
|
+
format_feet
|
15
|
+
format_inches
|
16
|
+
format_fraction
|
17
|
+
|
18
|
+
@mixed_number = @unit_array.compact.join
|
19
|
+
end
|
20
|
+
|
21
|
+
def format_feet
|
22
|
+
@unit_array << "#{@feet}'" if @feet > 0
|
23
|
+
end
|
24
|
+
|
25
|
+
def format_inches
|
26
|
+
if @inches > 0
|
27
|
+
@unit_array << ' ' if @feet > 0
|
28
|
+
@unit_array << "#{@inches}"
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def format_fraction
|
33
|
+
@unit_array << ' ' if @fraction > 0 && @inches > 0
|
34
|
+
@unit_array << "#{@fraction}" if @fraction > 0
|
35
|
+
@unit_array << "\"" if @raw_inches > 0
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
# Class method helper to make call easier
|
40
|
+
class << self
|
41
|
+
def format(number)
|
42
|
+
Formatter.new(number).mixed_number
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,122 @@
|
|
1
|
+
grammar LengthGrammar
|
2
|
+
|
3
|
+
rule term
|
4
|
+
additive | factor
|
5
|
+
end
|
6
|
+
|
7
|
+
rule additive
|
8
|
+
(factor operator:('+' | '-') space* term) {
|
9
|
+
capture(:factor).value.send(capture(:operator).to_s, capture(:term).value)
|
10
|
+
}
|
11
|
+
end
|
12
|
+
|
13
|
+
rule factor
|
14
|
+
multiplicative | prefix
|
15
|
+
end
|
16
|
+
|
17
|
+
rule multiplicative
|
18
|
+
(prefix operator:('*' | '/' | '%') space* factor) {
|
19
|
+
capture(:prefix).value.send(capture(:operator).to_s, capture(:factor).value)
|
20
|
+
}
|
21
|
+
end
|
22
|
+
|
23
|
+
rule prefix
|
24
|
+
prefixed | number_group
|
25
|
+
end
|
26
|
+
|
27
|
+
rule prefixed
|
28
|
+
(operator:('-' | '+' | '~') space* prefix) {
|
29
|
+
s = capture(:operator).to_s
|
30
|
+
s += '@' unless s == '~' # Unary + and - require an @.
|
31
|
+
capture(:prefix).value.send(s)
|
32
|
+
}
|
33
|
+
end
|
34
|
+
|
35
|
+
rule number_group
|
36
|
+
number_set | exponent
|
37
|
+
end
|
38
|
+
|
39
|
+
rule number_set
|
40
|
+
(primary space* number_group){
|
41
|
+
capture(:primary).value + capture(:number_group).value
|
42
|
+
}
|
43
|
+
end
|
44
|
+
|
45
|
+
rule exponent
|
46
|
+
exponential | primary
|
47
|
+
end
|
48
|
+
|
49
|
+
rule exponential
|
50
|
+
(primary operator:'**' space* prefix) {
|
51
|
+
capture(:primary).value.send(capture(:operator).to_s, capture(:prefix).value)
|
52
|
+
}
|
53
|
+
end
|
54
|
+
|
55
|
+
rule primary
|
56
|
+
group | mixed_number
|
57
|
+
end
|
58
|
+
|
59
|
+
rule group
|
60
|
+
(lparen term rparen) {
|
61
|
+
capture(:term).value
|
62
|
+
}
|
63
|
+
end
|
64
|
+
|
65
|
+
## Lexical syntax
|
66
|
+
|
67
|
+
rule mixed_number
|
68
|
+
digits_with_unit | number
|
69
|
+
end
|
70
|
+
|
71
|
+
rule number
|
72
|
+
irrational | integer
|
73
|
+
end
|
74
|
+
|
75
|
+
rule irrational
|
76
|
+
rational | float
|
77
|
+
end
|
78
|
+
|
79
|
+
rule float
|
80
|
+
decimal | integer
|
81
|
+
end
|
82
|
+
|
83
|
+
rule decimal
|
84
|
+
(integer '.' integer) { to_str.to_f }
|
85
|
+
end
|
86
|
+
|
87
|
+
rule integer
|
88
|
+
(digits space*) { capture(:digits).to_str.to_i }
|
89
|
+
end
|
90
|
+
|
91
|
+
rule rational
|
92
|
+
leading_whole | fraction
|
93
|
+
end
|
94
|
+
|
95
|
+
rule leading_whole
|
96
|
+
(integer fraction){
|
97
|
+
capture(:integer).value + capture(:fraction).value
|
98
|
+
}
|
99
|
+
end
|
100
|
+
|
101
|
+
rule fraction
|
102
|
+
(float '/' float) { to_str.to_r }
|
103
|
+
end
|
104
|
+
|
105
|
+
rule digits_with_unit
|
106
|
+
(number unit space*){
|
107
|
+
(RubyUnits::Unit.new( capture(:number).value.to_s.strip + ( capture(:unit).value.empty? ? "in" : capture(:unit).value)) >> "in").scalar.to_f.round(4)
|
108
|
+
}
|
109
|
+
end
|
110
|
+
|
111
|
+
rule unit
|
112
|
+
([A-z]+ | '"' | '\'' | '˚' )*
|
113
|
+
end
|
114
|
+
|
115
|
+
rule digits
|
116
|
+
[0-9]+ ('_' [0-9]+)* # mixed_numbers may contain underscores.
|
117
|
+
end
|
118
|
+
|
119
|
+
rule lparen '(' space* end
|
120
|
+
rule rparen ')' space* end
|
121
|
+
rule space [ \t\n\r] end
|
122
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'citrus'
|
2
|
+
require 'ruby_units/namespaced'
|
3
|
+
Citrus.load 'lib/tape_measure/length_grammar'
|
4
|
+
|
5
|
+
module TapeMeasure
|
6
|
+
# Parse Strings into units and math them
|
7
|
+
class Parser
|
8
|
+
attr_reader :value
|
9
|
+
|
10
|
+
def initialize(string)
|
11
|
+
@string = string
|
12
|
+
return @string.blank? if @string == 0.0
|
13
|
+
parse
|
14
|
+
end
|
15
|
+
|
16
|
+
def parse
|
17
|
+
@value = LengthGrammar.parse(@string.strip).value
|
18
|
+
rescue => ex
|
19
|
+
ex.message
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
# Class method helper to make call easier
|
24
|
+
class << self
|
25
|
+
def parse(string)
|
26
|
+
Parser.new(string).value
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
data/lib/tape_measure.rb
ADDED
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# Require this file using `require "spec_helper"` to ensure that it is only
|
4
|
+
# loaded once.
|
5
|
+
#
|
6
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
7
|
+
require 'tape_measure'
|
8
|
+
|
9
|
+
RSpec.configure do |config|
|
10
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
11
|
+
config.run_all_when_everything_filtered = true
|
12
|
+
config.filter_run :focus
|
13
|
+
|
14
|
+
# Run specs in random order to surface order dependencies. If you find an
|
15
|
+
# order dependency and want to debug it, you can fix the order by providing
|
16
|
+
# the seed, which is printed after each run.
|
17
|
+
# --seed 1234
|
18
|
+
config.order = 'random'
|
19
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe TapeMeasure::Formatter do
|
4
|
+
|
5
|
+
it 'when simple number' do
|
6
|
+
expect(TapeMeasure::Formatter.new(1).mixed_number).to eq("1\"")
|
7
|
+
end
|
8
|
+
|
9
|
+
it 'when multiple feet' do
|
10
|
+
expect(TapeMeasure::Formatter.new(47).mixed_number).to eq("3' 11\"")
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'when multiple feet with fraction' do
|
14
|
+
expect(TapeMeasure::Formatter.new(47.333).mixed_number)
|
15
|
+
.to eq("3' 11 1/3\"")
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'when no length' do
|
19
|
+
expect(TapeMeasure::Formatter.new(0.0).mixed_number)
|
20
|
+
.to eq('')
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'when called via class method helper' do
|
24
|
+
expect(TapeMeasure.format(1)).to eq("1\"")
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,182 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe TapeMeasure do
|
4
|
+
# 6 inches
|
5
|
+
# 6in * 4ft
|
6
|
+
# 6"
|
7
|
+
# 4 feet 2 inches
|
8
|
+
# 4'2"
|
9
|
+
# 4 ' 2 "
|
10
|
+
# 3 feet
|
11
|
+
# 3'
|
12
|
+
# 3 '
|
13
|
+
# 3ft
|
14
|
+
# 3ft10in
|
15
|
+
# 3ft 13in (should convert to 4'1")
|
16
|
+
# 1' 2 1/3"
|
17
|
+
# 1' 2 1/3"
|
18
|
+
# 1' 2.24"
|
19
|
+
# 1.1'
|
20
|
+
# 1 1/10'
|
21
|
+
# 1' 2' 3' 127cm
|
22
|
+
# 5 (assumes inches)
|
23
|
+
# 3-3/4" = 2.75
|
24
|
+
describe :grammar do
|
25
|
+
it 'can add' do
|
26
|
+
TapeMeasure::Parser.new('(8+2)').value.should eq 10
|
27
|
+
TapeMeasure::Parser.new('8+2').value.should eq 10
|
28
|
+
TapeMeasure::Parser.new('(8 + 2)').value.should eq 10
|
29
|
+
TapeMeasure::Parser.new('8 + 2').value.should eq 10
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'can subtract' do
|
33
|
+
TapeMeasure::Parser.new('4 - 2').value.should eq 2
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'can evaluate parens' do
|
37
|
+
TapeMeasure::Parser.new('(8*2)').value.should eq 16
|
38
|
+
TapeMeasure::Parser.new('(8)*(2)*(2)').value.should eq 32
|
39
|
+
TapeMeasure::Parser.new('(8)*(2)').value.should eq 16
|
40
|
+
TapeMeasure::Parser.new('(8*2)').value.should eq 16
|
41
|
+
TapeMeasure::Parser.new('(8)(2)').value.should eq 10
|
42
|
+
TapeMeasure::Parser.new('(2+8*2+2)*2').value.should eq 40
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'will perform flops' do
|
46
|
+
expect(TapeMeasure::Parser.new('15.5 + 12.7').value).to eq(28.2)
|
47
|
+
expect(TapeMeasure::Parser.new('7.24 - 4.12').value).to eq(3.12)
|
48
|
+
expect(TapeMeasure::Parser.new('4.5 * 3').value).to eq(13.5)
|
49
|
+
expect(TapeMeasure::Parser.new('5.5 / 11').value).to eq(0.5)
|
50
|
+
end
|
51
|
+
|
52
|
+
it 'can multiply rationals' do
|
53
|
+
TapeMeasure::Parser.new('(4/4 + 4)').value.should eq 5
|
54
|
+
TapeMeasure::Parser.new('(3/4 +4/4)').value.should eq '7/4'.to_r
|
55
|
+
TapeMeasure::Parser.new('(3/4 *4/1)').value.should eq '12/4'.to_r
|
56
|
+
end
|
57
|
+
|
58
|
+
it 'can multiply rationals and fractions' do
|
59
|
+
TapeMeasure::Parser.new('(3/4 +1.0)').value.should eq 7.0 / 4.0
|
60
|
+
end
|
61
|
+
|
62
|
+
describe 'recognizes mixed numbers' do
|
63
|
+
|
64
|
+
it 'recognizes inches' do
|
65
|
+
TapeMeasure::Parser.new('2 inches').value.should eq 2
|
66
|
+
TapeMeasure::Parser.new('2 "').value.should eq 2
|
67
|
+
TapeMeasure::Parser.new('2in').value.should eq 2
|
68
|
+
end
|
69
|
+
|
70
|
+
it 'recognizes feet' do
|
71
|
+
TapeMeasure::Parser.new('2 feet').value.should eq 24
|
72
|
+
TapeMeasure::Parser.new('2\'').value.should eq 24
|
73
|
+
TapeMeasure::Parser.new('2ft').value.should eq 24
|
74
|
+
end
|
75
|
+
|
76
|
+
it 'converts meters' do
|
77
|
+
TapeMeasure::Parser.new('1 meter').value.should eq 39.3701
|
78
|
+
TapeMeasure::Parser.new('1m').value.should eq 39.3701
|
79
|
+
end
|
80
|
+
|
81
|
+
describe 'converts centimeters' do
|
82
|
+
it 'with whitespace' do
|
83
|
+
TapeMeasure::Parser.new('127 cm').value.should eq 50
|
84
|
+
end
|
85
|
+
it 'without whitespace' do
|
86
|
+
TapeMeasure::Parser.new('127cm').value.should eq 50
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
it 'converts millimeters' do
|
91
|
+
TapeMeasure::Parser.new('1270 mm').value.should eq 50
|
92
|
+
TapeMeasure::Parser.new('1270mm').value.should eq 50
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
describe :unit_conversion do
|
98
|
+
|
99
|
+
it 'number + space + string unit twice to inches' do
|
100
|
+
TapeMeasure::Parser.new('4feet 2inches').value.should eq 50
|
101
|
+
end
|
102
|
+
|
103
|
+
it 'number + string unit twice to inches' do
|
104
|
+
TapeMeasure::Parser.new("4'2\"").value.should eq 50
|
105
|
+
end
|
106
|
+
|
107
|
+
it 'number + string unit twice to inches' do
|
108
|
+
TapeMeasure::Parser.new('3ft10in').value.should eq 46
|
109
|
+
end
|
110
|
+
|
111
|
+
it 'number + string unit twice to inches' do
|
112
|
+
TapeMeasure::Parser.new('3ft13in').value.should eq 49
|
113
|
+
end
|
114
|
+
|
115
|
+
it 'converts single quotes to feet' do
|
116
|
+
TapeMeasure::Parser.new("1\' 2 1/3\"").value
|
117
|
+
.should eq(12 + 2 + (1 / 3).to_f.round(4))
|
118
|
+
end
|
119
|
+
|
120
|
+
it 'lots of junk spaces' do
|
121
|
+
TapeMeasure::Parser.new(" 1\' 2 1/3\" ").value
|
122
|
+
.should eq(12 + 2 + (1 / 3).to_f.round(4))
|
123
|
+
end
|
124
|
+
|
125
|
+
it 'converts decimal inches' do
|
126
|
+
TapeMeasure::Parser.new(" 1\' 2.24\" ").value.should eq 14.24
|
127
|
+
end
|
128
|
+
|
129
|
+
it 'converts decimal feet' do
|
130
|
+
TapeMeasure::Parser.new(" 1.1\' ").value.should eq 13.2
|
131
|
+
end
|
132
|
+
|
133
|
+
it 'converts rational feet' do
|
134
|
+
TapeMeasure::Parser.new(" 1 1/10\' ").value.should eq 13.2
|
135
|
+
end
|
136
|
+
|
137
|
+
it 'handle a shit ton of units when someone thinks they are funny' do
|
138
|
+
TapeMeasure::Parser.new("1' 2' 3' 127cm").value.should eq 122.0
|
139
|
+
end
|
140
|
+
|
141
|
+
it 'handles no units' do
|
142
|
+
TapeMeasure::Parser.new('5').value.should eq 5
|
143
|
+
end
|
144
|
+
|
145
|
+
# expected to hand parens in this way
|
146
|
+
it 'handles paren parsing as predicted' do
|
147
|
+
TapeMeasure::Parser.new('(5) 5').value.should eq 10
|
148
|
+
TapeMeasure::Parser.new('(5)(5)(5)').value.should eq 15
|
149
|
+
end
|
150
|
+
|
151
|
+
it 'handles no units' do
|
152
|
+
TapeMeasure::Parser.new('(5+5) 5 (5+5)').value.should eq 25
|
153
|
+
end
|
154
|
+
|
155
|
+
it 'order of operations' do
|
156
|
+
TapeMeasure::Parser.new('(5+5*5) 5/5').value.should eq 31
|
157
|
+
end
|
158
|
+
|
159
|
+
end
|
160
|
+
|
161
|
+
describe :calculator do
|
162
|
+
it 'with length style input' do
|
163
|
+
expect(TapeMeasure::Parser.new('15.5\' + 12.7').value).to eq(198.7)
|
164
|
+
expect(TapeMeasure::Parser.new('7 1/3 inches - 4.12mm').value)
|
165
|
+
.to eq(7.1711)
|
166
|
+
expect(TapeMeasure::Parser.new('4.5 * 3').value).to eq(13.5)
|
167
|
+
expect(TapeMeasure::Parser.new('5.5 / 11').value).to eq(0.5)
|
168
|
+
expect(TapeMeasure::Parser.new('1 ft * 1 inch').value).to eq(12)
|
169
|
+
expect(TapeMeasure::Parser.new('(3 ft 1/4") * 3 1/2 inch').value).to eq(126.875)
|
170
|
+
expect(TapeMeasure::Parser.new('(3 ft 1/4") * 3 1/2 inch * 4/4" ').value).to eq(126.875)
|
171
|
+
expect(TapeMeasure::Parser.new('(3 ft 1/4") * 3 1/2 inch * 3/4" ').value).to eq(95.15625)
|
172
|
+
end
|
173
|
+
it 'when no brackets to define order of operations' do
|
174
|
+
expect(TapeMeasure::Parser.new('3\' 1/4" * 3 1/2"').value).to eq(126.875)
|
175
|
+
expect(TapeMeasure::Parser.new('3\' 1/4" * 3 1/2" * 4/4" ').value).to eq(126.875)
|
176
|
+
expect(TapeMeasure::Parser.new('3\' 1/4" * 3 1/2" * 3/4" ').value).to eq(95.15625)
|
177
|
+
end
|
178
|
+
end
|
179
|
+
it 'when called via class method helper' do
|
180
|
+
expect(TapeMeasure.parse("1\"")).to eq(1)
|
181
|
+
end
|
182
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'tape_measure/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
|
8
|
+
spec.name = 'tape_measure'
|
9
|
+
spec.version = TapeMeasure::VERSION
|
10
|
+
spec.authors = ['Sam Congleton']
|
11
|
+
spec.email = ['sam@burningpony.com']
|
12
|
+
spec.summary = %q(Measurements parser and converter.)
|
13
|
+
spec.description = %q(Contains a text parser for converting various measurements into a base unit, as well as doing several mathematical functions with those numbers)
|
14
|
+
spec.homepage = 'https://github.com/burningpony/tape_measure'
|
15
|
+
spec.license = 'GNU v3'
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0")
|
18
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
19
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
20
|
+
spec.require_paths = ['lib']
|
21
|
+
spec.add_development_dependency 'bundler', '~> 1.6'
|
22
|
+
spec.add_development_dependency 'rake'
|
23
|
+
spec.add_development_dependency 'rspec'
|
24
|
+
spec.add_dependency 'citrus'
|
25
|
+
spec.add_dependency 'ruby-units'
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,134 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: tape_measure
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Sam Congleton
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-05-28 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.6'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.6'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: citrus
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: ruby-units
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: Contains a text parser for converting various measurements into a base
|
84
|
+
unit, as well as doing several mathematical functions with those numbers
|
85
|
+
email:
|
86
|
+
- sam@burningpony.com
|
87
|
+
executables: []
|
88
|
+
extensions: []
|
89
|
+
extra_rdoc_files: []
|
90
|
+
files:
|
91
|
+
- .gitignore
|
92
|
+
- .rspec
|
93
|
+
- .travis.yml
|
94
|
+
- Gemfile
|
95
|
+
- LICENSE.txt
|
96
|
+
- README.md
|
97
|
+
- Rakefile
|
98
|
+
- lib/tape_measure.rb
|
99
|
+
- lib/tape_measure/formatter.rb
|
100
|
+
- lib/tape_measure/length_grammar.citrus
|
101
|
+
- lib/tape_measure/parser.rb
|
102
|
+
- lib/tape_measure/version.rb
|
103
|
+
- spec/spec_helper.rb
|
104
|
+
- spec/tape_measure/formatter_spec.rb
|
105
|
+
- spec/tape_measure/parser_spec.rb
|
106
|
+
- tape_measure.gemspec
|
107
|
+
homepage: https://github.com/burningpony/tape_measure
|
108
|
+
licenses:
|
109
|
+
- GNU v3
|
110
|
+
metadata: {}
|
111
|
+
post_install_message:
|
112
|
+
rdoc_options: []
|
113
|
+
require_paths:
|
114
|
+
- lib
|
115
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
116
|
+
requirements:
|
117
|
+
- - '>='
|
118
|
+
- !ruby/object:Gem::Version
|
119
|
+
version: '0'
|
120
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - '>='
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
requirements: []
|
126
|
+
rubyforge_project:
|
127
|
+
rubygems_version: 2.0.3
|
128
|
+
signing_key:
|
129
|
+
specification_version: 4
|
130
|
+
summary: Measurements parser and converter.
|
131
|
+
test_files:
|
132
|
+
- spec/spec_helper.rb
|
133
|
+
- spec/tape_measure/formatter_spec.rb
|
134
|
+
- spec/tape_measure/parser_spec.rb
|