gematria 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,9 @@
1
1
  # Changelog
2
2
 
3
+ ## v0.0.4
4
+
5
+ * Ability to set global table so as to not have to specify a table name with every new instance
6
+
3
7
  ## v0.0.3
4
8
 
5
9
  * Removes support for 1.8.7 (due to encoding problems with Hebrew) (Now compatible with 1.9.2 - head)
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("Adam", :english)
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("Gematria is fun!", english)
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
- alephbet = Gematria::Calculator.new('אבגדהוזחטיכלמנסעפצקרשתךםןףץ', :hebrew)
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
- # defining a custom table
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('abcd', :mini)
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
@@ -2,7 +2,7 @@
2
2
 
3
3
  ## Near future
4
4
 
5
- * Ability to configure global default table so as to not have to specify a table name with every new instance
5
+ * Nothing for now.
6
6
 
7
7
  ## Far future
8
8
 
@@ -8,7 +8,7 @@ module Gematria
8
8
  if table_name.is_a? Symbol
9
9
  @table = Tables.fetch(table_name)
10
10
  else
11
- @table = {}
11
+ @table = Tables.current
12
12
  end
13
13
  end
14
14
 
@@ -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
- module_function
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
 
@@ -1,4 +1,4 @@
1
1
  # -*- encoding : utf-8 -*-
2
2
  module Gematria
3
- VERSION = "0.0.3"
3
+ VERSION = "0.0.4"
4
4
  end
@@ -2,35 +2,29 @@
2
2
  require 'spec_helper'
3
3
 
4
4
  describe Gematria::Calculator do
5
- describe "with no table specified" do
6
- it "is instantiated with text and provides access to that text" do
7
- gematria = Gematria::Calculator.new('mytext')
8
- expect(gematria.text).to eq 'mytext'
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
- describe "#mapped" do
12
- it "returns an array of values for each character in text" do
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 [0,0,0]
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
- describe "#reduced" do
26
- it "returns a single digit" do
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.reduced).to eq 0
22
+ expect(gematria.mapped).to eq [0,0,0]
29
23
  end
30
- end
24
+ end
31
25
  end
32
26
 
33
- describe "with English table specified" do
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 "with Hebrew table specified" do
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('אבג', :hebrew)
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('אבגדהוזחטיכלמנסעפצקרשתךםןףץ', :hebrew)
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('אבג', :hebrew)
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('אבגדהוזחטיכלמנסעפצקרשתךםןףץ', :hebrew)
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
- describe Gematria::Tables do
5
- it "has a hash to store the tables" do
6
- expect(Gematria::Tables::TABLES).to be_a Hash
7
- end
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
- describe "#add_table" do
10
- context "when table is valid" do
11
- it "adds the table to the TABLES store" do
12
- Gematria::Tables.add_table :valid, 'a'=>1,'b'=>2
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
- context "when table is invalid" do
17
- it "raises a TypeError" do
18
- expect {
19
- Gematria::Tables.add_table :invalid, [['a',1],['b',2]]
20
- }.to raise_error TypeError
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
- describe "#fetch" do
26
- context "when table exists" do
27
- it "returns the table" do
28
- Gematria::Tables.add_table :mytable, 'a'=>1
29
- expect(Gematria::Tables.fetch(:mytable)).to eq 'a'=>1
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
- context "when table does not exist" do
33
- it "raises a KeyError" do
34
- expect {
35
- Gematria::Tables.fetch(:non_existant_table)
36
- }.to raise_error KeyError
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
- describe "#[]" do
42
- it "delegates to #fetch" do
43
- Gematria::Tables.should_receive(:fetch).with(:key)
44
- Gematria::Tables[:key]
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gematria
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: