i_ching 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 531d9ba9e9ea652115bc43e588d987de585c14b9
4
- data.tar.gz: bcbd208b4cde44b40bcf3990336bd1ad27efbb31
3
+ metadata.gz: 659be3dc9e13bf0554d94913f7a5dc605aff503a
4
+ data.tar.gz: ae844f40d018e5d1b16fef8481ee6a4ae98e2598
5
5
  SHA512:
6
- metadata.gz: c2ba838d3924b345b836f862020b7ca9ed6fdf0cd100ca90bbc93c9bfbacaa93e265154959bdae0136e9a7f8cc90fe7cdf2583d991f6d880ae444f7b2bb494bb
7
- data.tar.gz: 16943c0cdf2cc4adc18ab1a3a6b7742c6a6b7a3b06124a721535da7203e6fc4a12c05f0ea692fd7403d3afb0e7d132d3ca5c6c3030bd19099d4eafb953c2f7f8
6
+ metadata.gz: d055ff4d53c1223b1213525e56f5b2871655937872941bf285e5f0094701d3d05167786adc039288569a76e4cbcb5e618fb4e8028bb6013cd1c2a77ec90efc72
7
+ data.tar.gz: 2f7456e3983607f35a4fc6de8711db9ca5cc3e0d5cdf8f0229b26582b98a9518856ac43e8fc46542a1fa02f588880176fd06b36cea29ac78dfcab75d2b0d332d
@@ -1,13 +1,26 @@
1
1
  require "sequel"
2
+ require "json"
2
3
 
3
4
  require "i_ching/has_attributes"
5
+ require "i_ching/cast_hexagram"
6
+ require "i_ching/caster"
7
+ require "i_ching/casters/base"
8
+ require "i_ching/casters/coins"
9
+ require "i_ching/casters/random"
10
+ require "i_ching/casters/yarrow"
4
11
  require "i_ching/hexagram"
5
12
  require "i_ching/line"
6
13
  require "i_ching/trigram"
7
14
  require "i_ching/version"
8
15
 
9
16
  module IChing
10
- # Your code goes here...
17
+ extend self
18
+
19
+ def cast_hexagram(method=:yarrow)
20
+ caster = IChing::Caster.with_method(method)
21
+ caster.cast_hexagram
22
+ end
23
+
11
24
  module DB
12
25
  extend self
13
26
 
@@ -0,0 +1,58 @@
1
+ module IChing
2
+ class CastHexagram
3
+ class InvalidCast < ArgumentError; end
4
+
5
+ CHANGES = {
6
+ 6 => 7,
7
+ 9 => 8
8
+ }
9
+
10
+ STABILIZATIONS = {
11
+ 6 => 8,
12
+ 9 => 7
13
+ }
14
+
15
+ BINARY = {
16
+ 7 => 1,
17
+ 8 => 0
18
+ }
19
+
20
+ attr_reader :lines
21
+
22
+ def initialize(*lines)
23
+ @lines = lines
24
+ validate
25
+ end
26
+
27
+ def change
28
+ CastHexagram.new(*process(CHANGES))
29
+ end
30
+
31
+ def stabilize
32
+ CastHexagram.new(*process(STABILIZATIONS))
33
+ end
34
+
35
+ def binary
36
+ stabilize.lines.map { |line| BINARY[line] }.join("").to_i(2)
37
+ end
38
+
39
+ def hexagram
40
+ Hexagram.get_by_binary(self.binary)
41
+ end
42
+
43
+ def changed_hexagram
44
+ change.hexagram
45
+ end
46
+
47
+ private
48
+
49
+ def process(conversion_hash)
50
+ lines.map { |line| conversion_hash.fetch(line, line) }
51
+ end
52
+
53
+ def validate
54
+ raise InvalidCast unless @lines.count == 6
55
+ raise InvalidCast unless @lines.all? { |line| line.between?(6, 9) }
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,24 @@
1
+ module IChing
2
+ module Caster
3
+ extend self
4
+ class InvalidCaster < Exception
5
+ end
6
+
7
+ def with_method(method)
8
+ if casting_method = available_casting_method(method)
9
+ casting_method.new
10
+ else
11
+ raise InvalidCaster
12
+ end
13
+ end
14
+
15
+ def available_casting_method(method)
16
+ case method.to_sym
17
+ when :yarrow then IChing::Casters::Yarrow
18
+ when :coins then IChing::Casters::Coins
19
+ when :random then IChing::Casters::Random
20
+ else nil
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,17 @@
1
+ module IChing
2
+ module Casters
3
+ class Base
4
+ def cast_hexagram
5
+ CastHexagram.new(*generate_lines)
6
+ end
7
+
8
+ def generate_lines
9
+ 6.times.map { generate_line }
10
+ end
11
+
12
+ def generate_line
13
+ raise NotImplementedError
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,12 @@
1
+ module IChing
2
+ module Casters
3
+ class Coins < Base
4
+
5
+ private
6
+
7
+ def generate_line
8
+ 3.times.map { rand(2) + 2 }.inject(0) { |t, n| t + n }
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ module IChing
2
+ module Casters
3
+ class Random < Base
4
+
5
+ private
6
+
7
+ def generate_line
8
+ rand(4) + 6
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,82 @@
1
+ module IChing
2
+ module Casters
3
+ class Yarrow < Base
4
+
5
+ private
6
+
7
+ def generate_line
8
+ @remainder_sets = []
9
+ @stalks = 50
10
+ @remainder = 0
11
+
12
+ put_single_stalk_aside
13
+
14
+ 3.times do
15
+ divide_stalks
16
+ take_one_stalk_from_right_hand_pile
17
+ take_least_mod_four_from_left
18
+ take_least_mod_four_from_right
19
+ set_remainder_aside
20
+
21
+ reset_stalks
22
+ end
23
+
24
+ convert_remainders_to_line
25
+ end
26
+
27
+ def put_single_stalk_aside
28
+ @stalks -= 1
29
+ end
30
+
31
+ def divide_stalks
32
+ @left = (@stalks / 2) + (rand(20) - 10)
33
+ @right = @stalks - @left
34
+ end
35
+
36
+ def take_one_stalk_from_right_hand_pile
37
+ @remainder += 1
38
+ @right -= 1
39
+ end
40
+
41
+ def take_least_mod_four_from_left
42
+ r = @left % 4
43
+ r = case r
44
+ when 0 then 4
45
+ else r
46
+ end
47
+ @left -= r
48
+ @remainder += r
49
+ end
50
+
51
+ def take_least_mod_four_from_right
52
+ r = @right % 4
53
+ r = case r
54
+ when 0 then 4
55
+ else r
56
+ end
57
+ @right -= r
58
+ @remainder += r
59
+ end
60
+
61
+ def set_remainder_aside
62
+ @remainder_sets << @remainder
63
+ end
64
+
65
+ def reset_stalks
66
+ @remainder = 0
67
+ @stalks = @left + @right
68
+ @left = 0
69
+ @right = 0
70
+ end
71
+
72
+ def convert_remainders_to_line
73
+ @remainder_sets.map do |r|
74
+ case r
75
+ when 8, 9 then 2
76
+ when 4, 5 then 3
77
+ end
78
+ end.inject(0) { |t, n| t + n }
79
+ end
80
+ end
81
+ end
82
+ end
@@ -16,4 +16,14 @@ module HasAttributes
16
16
  instance_variable_set("@#{k}", v)
17
17
  end
18
18
  end
19
+
20
+ def to_h
21
+ Hash[self.class.attributes.map do |key|
22
+ [key, self.send(key)]
23
+ end]
24
+ end
25
+
26
+ def to_json(*args)
27
+ JSON.generate(self.to_h)
28
+ end
19
29
  end
@@ -8,6 +8,14 @@ module IChing
8
8
  new(table[king_wen_number: king_wen_number])
9
9
  end
10
10
 
11
+ def get_by_binary(binary)
12
+ new(table[binary: binary])
13
+ end
14
+
15
+ def all
16
+ table.map { |hexagram| new(hexagram) }
17
+ end
18
+
11
19
  private
12
20
 
13
21
  def table
@@ -4,14 +4,14 @@ module IChing
4
4
  has_attributes :king_wen_number, :place, :meaning
5
5
 
6
6
  class << self
7
- def get(king_wen_number, place)
8
- new(table[king_wen_number: king_wen_number, place: place])
9
- end
10
-
11
7
  def all(king_wen_number)
12
8
  table.where(king_wen_number: king_wen_number).order(:place).map { |line| new(line) }
13
9
  end
14
10
 
11
+ def get(king_wen_number, place)
12
+ new(table[king_wen_number: king_wen_number, place: place])
13
+ end
14
+
15
15
  private
16
16
 
17
17
  def table
@@ -4,6 +4,10 @@ module IChing
4
4
  has_attributes :english_name, :chinese_name, :number, :characters, :binary
5
5
 
6
6
  class << self
7
+ def all
8
+ table.map { |trigram_data| new(trigram_data) }
9
+ end
10
+
7
11
  def get(number)
8
12
  new(table[number: number])
9
13
  end
@@ -1,3 +1,3 @@
1
1
  module IChing
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: i_ching
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Berkowitz
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-11-05 00:00:00.000000000 Z
11
+ date: 2015-11-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sequel
@@ -84,6 +84,12 @@ files:
84
84
  - i_ching.gemspec
85
85
  - lib/i_ching.db
86
86
  - lib/i_ching.rb
87
+ - lib/i_ching/cast_hexagram.rb
88
+ - lib/i_ching/caster.rb
89
+ - lib/i_ching/casters/base.rb
90
+ - lib/i_ching/casters/coins.rb
91
+ - lib/i_ching/casters/random.rb
92
+ - lib/i_ching/casters/yarrow.rb
87
93
  - lib/i_ching/has_attributes.rb
88
94
  - lib/i_ching/hexagram.rb
89
95
  - lib/i_ching/line.rb