gematria 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +4 -0
- data/README.md +9 -6
- data/ROADMAP.md +1 -1
- data/lib/gematria/calculator.rb +1 -1
- data/lib/gematria/tables.rb +25 -5
- data/lib/gematria/version.rb +1 -1
- data/spec/gematria/calculator_spec.rb +22 -26
- data/spec/gematria/tables_spec.rb +58 -30
- metadata +1 -1
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -26,22 +26,25 @@ Or install it yourself as:
|
|
26
26
|
|
27
27
|
## Usage
|
28
28
|
|
29
|
-
name = Gematria::Calculator.new
|
29
|
+
name = Gematria::Calculator.new "Adam", :english
|
30
30
|
name.converted # => 46
|
31
31
|
name.mapped.join(" + ") # => "1 + 4 + 1 + 40"
|
32
32
|
name.reduced # => 1
|
33
33
|
|
34
|
-
gematria = Gematria::Calculator.new
|
34
|
+
gematria = Gematria::Calculator.new "Gematria is fun!", :english
|
35
35
|
gematria.converted # => 818
|
36
36
|
gematria.mapped # => [7, 5, 40, 1, 200, 90, 9, 1, 0, 9, 100, 0, 6, 300, 50, 0]
|
37
37
|
|
38
|
-
|
38
|
+
Settings a global table
|
39
|
+
|
40
|
+
Gematria::Tables.set_table :hebrew
|
41
|
+
alephbet = Gematria::Calculator.new 'אבגדהוזחטיכלמנסעפצקרשתךםןףץ'
|
39
42
|
alephbet.converted == 4995 # => true
|
40
43
|
|
41
|
-
|
44
|
+
Defining a custom table
|
42
45
|
|
43
46
|
Gematria::Tables.add_table :mini, 'a' => 1, 'b' => 10, 'c' => 100
|
44
|
-
abcd = Gematria::Calculator.new
|
47
|
+
abcd = Gematria::Calculator.new 'abcd', :mini
|
45
48
|
abcd.mapped # => [1,10,100,0]
|
46
49
|
abcd.converted # => 111
|
47
50
|
abcd.reduced # => 3
|
@@ -83,4 +86,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
83
86
|
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
84
87
|
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
85
88
|
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
86
|
-
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
89
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/ROADMAP.md
CHANGED
data/lib/gematria/calculator.rb
CHANGED
data/lib/gematria/tables.rb
CHANGED
@@ -1,9 +1,29 @@
|
|
1
1
|
# -*- encoding : utf-8 -*-
|
2
2
|
module Gematria
|
3
3
|
module Tables
|
4
|
+
# Stores available tables
|
4
5
|
TABLES = {}
|
5
6
|
|
6
|
-
|
7
|
+
# Sets the global current table
|
8
|
+
#
|
9
|
+
# Tables.set_table(:english)
|
10
|
+
# Tables.current # => {'a'=>1,'b'=>2,...}
|
11
|
+
#
|
12
|
+
# @param table_name [Symbol] table name
|
13
|
+
# @return [Symbol] table name
|
14
|
+
def self.set_table(table_name)
|
15
|
+
@current = table_name
|
16
|
+
end
|
17
|
+
|
18
|
+
# Gets the currently selected table
|
19
|
+
#
|
20
|
+
# Tables.set_table(:english)
|
21
|
+
# Tables.current # => {'a'=>1,'b'=>2,...}
|
22
|
+
#
|
23
|
+
# @return [Hash{String => Number}]
|
24
|
+
def self.current
|
25
|
+
fetch(@current)
|
26
|
+
end
|
7
27
|
|
8
28
|
# Adds a table to the table store. A valid table must be a Hash with single character string keys and numerical values.
|
9
29
|
# A table that is not a Hash will raise a TypeError. The table name will be converted to a Symbol.
|
@@ -12,7 +32,7 @@ module Gematria
|
|
12
32
|
#
|
13
33
|
# @param name [#to_sym] table name
|
14
34
|
# @param table [Hash{String => Number}] table of characters pointing to corresponding values
|
15
|
-
def add_table(name, table)
|
35
|
+
def self.add_table(name, table)
|
16
36
|
if table.is_a? Hash
|
17
37
|
TABLES[name.to_sym] = table
|
18
38
|
else
|
@@ -26,8 +46,8 @@ module Gematria
|
|
26
46
|
#
|
27
47
|
# @param key [Symbol] table name as a Symbol
|
28
48
|
# @return [Hash{String => Number}]
|
29
|
-
def fetch(key)
|
30
|
-
TABLES.fetch(key)
|
49
|
+
def self.fetch(key)
|
50
|
+
TABLES.fetch(key, {})
|
31
51
|
end
|
32
52
|
|
33
53
|
# Fetches specified table (delegates to #fetch)
|
@@ -36,7 +56,7 @@ module Gematria
|
|
36
56
|
#
|
37
57
|
# @param key [Symbol] table name as a Symbol
|
38
58
|
# @return [Hash{String => Number}]
|
39
|
-
def [](key)
|
59
|
+
def self.[](key)
|
40
60
|
fetch(key)
|
41
61
|
end
|
42
62
|
|
data/lib/gematria/version.rb
CHANGED
@@ -2,35 +2,29 @@
|
|
2
2
|
require 'spec_helper'
|
3
3
|
|
4
4
|
describe Gematria::Calculator do
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
end
|
5
|
+
it "is instantiated with text and provides access to that text" do
|
6
|
+
gematria = Gematria::Calculator.new('mytext')
|
7
|
+
expect(gematria.text).to eq 'mytext'
|
8
|
+
end
|
10
9
|
|
11
|
-
|
12
|
-
|
10
|
+
context "when no table is specified" do
|
11
|
+
context "when a table has been set" do
|
12
|
+
it "uses the currently selected table" do
|
13
|
+
Gematria::Tables.set_table(:english)
|
13
14
|
gematria = Gematria::Calculator.new('abc')
|
14
|
-
expect(gematria.mapped).to eq [
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
describe "#converted" do
|
19
|
-
it "returns the sum of the letter values" do
|
20
|
-
gematria = Gematria::Calculator.new('abc')
|
21
|
-
expect(gematria.converted).to eq 0
|
15
|
+
expect(gematria.mapped).to eq [1,2,3]
|
22
16
|
end
|
23
17
|
end
|
24
|
-
|
25
|
-
|
26
|
-
|
18
|
+
context "when no table has been set" do
|
19
|
+
it "uses an empty table (all zeros)" do
|
20
|
+
Gematria::Tables.set_table(nil)
|
27
21
|
gematria = Gematria::Calculator.new('abc')
|
28
|
-
expect(gematria.
|
22
|
+
expect(gematria.mapped).to eq [0,0,0]
|
29
23
|
end
|
30
|
-
end
|
24
|
+
end
|
31
25
|
end
|
32
26
|
|
33
|
-
|
27
|
+
context "when English table is specified" do
|
34
28
|
describe "#mapped" do
|
35
29
|
it "returns an array of values for each character in text" do
|
36
30
|
gematria = Gematria::Calculator.new('abc', :english)
|
@@ -61,17 +55,19 @@ describe Gematria::Calculator do
|
|
61
55
|
end
|
62
56
|
end
|
63
57
|
|
64
|
-
describe "
|
58
|
+
describe "when Hebrew table is specified" do
|
59
|
+
before { Gematria::Tables.set_table :hebrew }
|
60
|
+
|
65
61
|
describe "#mapped" do
|
66
62
|
it "returns an array of values for each character in text" do
|
67
|
-
abg = Gematria::Calculator.new('אבג'
|
63
|
+
abg = Gematria::Calculator.new('אבג')
|
68
64
|
expect(abg.mapped).to eq [1,2,3]
|
69
65
|
end
|
70
66
|
end
|
71
67
|
|
72
68
|
describe "#converted" do
|
73
69
|
it "returns the sum of the letter values" do
|
74
|
-
alephbet = Gematria::Calculator.new('אבגדהוזחטיכלמנסעפצקרשתךםןףץ'
|
70
|
+
alephbet = Gematria::Calculator.new('אבגדהוזחטיכלמנסעפצקרשתךםןףץ')
|
75
71
|
expect(alephbet.converted).to eq 4995
|
76
72
|
end
|
77
73
|
end
|
@@ -79,13 +75,13 @@ describe Gematria::Calculator do
|
|
79
75
|
describe "#reduced" do
|
80
76
|
context "when converted value is a single digit" do
|
81
77
|
it "returns a single digit" do
|
82
|
-
abg = Gematria::Calculator.new('אבג'
|
78
|
+
abg = Gematria::Calculator.new('אבג')
|
83
79
|
expect(abg.reduced).to eq 6
|
84
80
|
end
|
85
81
|
end
|
86
82
|
context "when converted value is more than one digit" do
|
87
83
|
it "returns a single digit" do
|
88
|
-
alephbet = Gematria::Calculator.new('אבגדהוזחטיכלמנסעפצקרשתךםןףץ'
|
84
|
+
alephbet = Gematria::Calculator.new('אבגדהוזחטיכלמנסעפצקרשתךםןףץ')
|
89
85
|
expect(alephbet.reduced).to eq 9 # 4995 -> 27 -> 9
|
90
86
|
end
|
91
87
|
end
|
@@ -1,48 +1,76 @@
|
|
1
1
|
# -*- encoding : utf-8 -*-
|
2
2
|
require 'spec_helper'
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
4
|
+
module Gematria
|
5
|
+
|
6
|
+
describe Tables do
|
7
|
+
it "has a hash to store the tables" do
|
8
|
+
expect(Tables::TABLES).to be_a Hash
|
9
|
+
end
|
8
10
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
expect(Gematria::Tables::TABLES).to include valid: {'a'=>1,'b'=>2}
|
11
|
+
describe "#set_table" do
|
12
|
+
it "assigns a table to @current" do
|
13
|
+
Tables.set_table(:english)
|
14
|
+
expect(Tables.instance_variable_get :@current).to eq :english
|
14
15
|
end
|
15
16
|
end
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
17
|
+
|
18
|
+
describe "#current" do
|
19
|
+
context "when a table has been selected" do
|
20
|
+
it "returns the previously selected table" do
|
21
|
+
Tables.set_table(:hebrew)
|
22
|
+
expect(Tables.current).to eq Tables.fetch(:hebrew)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
context "when a table has not been selected" do
|
26
|
+
before do
|
27
|
+
if Tables.instance_variable_defined? :@current
|
28
|
+
Tables.instance_variable_set :@current, nil
|
29
|
+
end
|
30
|
+
end
|
31
|
+
it "returns an empty hash" do
|
32
|
+
expect(Tables.current).to eq({})
|
33
|
+
end
|
21
34
|
end
|
22
35
|
end
|
23
|
-
end
|
24
36
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
37
|
+
describe "#add_table" do
|
38
|
+
context "when table is valid" do
|
39
|
+
it "adds the table to the TABLES store" do
|
40
|
+
Tables.add_table :valid, 'a'=>1,'b'=>2
|
41
|
+
expect(Tables::TABLES).to include valid: {'a'=>1,'b'=>2}
|
42
|
+
end
|
43
|
+
end
|
44
|
+
context "when table is invalid" do
|
45
|
+
it "raises a TypeError" do
|
46
|
+
expect {
|
47
|
+
Tables.add_table :invalid, [['a',1],['b',2]]
|
48
|
+
}.to raise_error TypeError
|
49
|
+
end
|
30
50
|
end
|
31
51
|
end
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
52
|
+
|
53
|
+
describe "#fetch" do
|
54
|
+
context "when table exists" do
|
55
|
+
it "returns the table" do
|
56
|
+
Tables.add_table :mytable, 'a'=>1
|
57
|
+
expect(Tables.fetch(:mytable)).to eq 'a'=>1
|
58
|
+
end
|
59
|
+
end
|
60
|
+
context "when table does not exist" do
|
61
|
+
it "returns an empty hash" do
|
62
|
+
expect(Tables.fetch :non_existant_table).to eq({})
|
63
|
+
end
|
37
64
|
end
|
38
65
|
end
|
39
|
-
end
|
40
66
|
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
67
|
+
describe "#[]" do
|
68
|
+
it "delegates to #fetch" do
|
69
|
+
Tables.should_receive(:fetch).with(:key)
|
70
|
+
Tables[:key]
|
71
|
+
end
|
45
72
|
end
|
73
|
+
|
46
74
|
end
|
47
75
|
|
48
76
|
end
|