dicebag 3.2.0 → 3.2.1

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: 755294b13e5e461c7cefc63493214beb228d539a
4
- data.tar.gz: 340241ef792274c0da3a67b97f64ba81f18e6163
3
+ metadata.gz: dc6f9c347f892aeb166fcae63db74f852f4f78d5
4
+ data.tar.gz: 4b719b9fa625bef4c4cc5a9bf531249d1cd366b6
5
5
  SHA512:
6
- metadata.gz: 5efb90bebf321dd2a721f367ec7b1a3c4e57963858210393a0f246646d4cb3865c6f36260c390639007d824100fac44cd16707c9c539db41cf92c833d2073827
7
- data.tar.gz: d2b2871d3940d219091daedc85c63771b69a1a789d087e972cb50ae41a0279880733abfabb61199b4d3bb9a35f8f52d00f202d97a4bad8ca47c3e6d5ac7c7a6a
6
+ metadata.gz: 78bf99b571914d1a28c26f964cdaf23ffbdb1bbb923527bacf1f613785f46b82cbfba283471cc080bdcc53a5c95263550472294ee4e3c22f6e5eaa77d3907769
7
+ data.tar.gz: da473d03f4524f4e0824d02b2875b705a2ef41c5254f6ad002ec1816696f2d78e348a5fe1ed3b636199b91587e6d8da4614b5d70f2ec6f2e67eedf03b84d8b03
@@ -40,32 +40,16 @@ module DiceBag
40
40
  # happens in the Roll class. It will convert all
41
41
  # values into the correct *Part class.
42
42
  def self.normalize_tree(tree)
43
- tree.collect do |part|
44
- case part
45
- when Hash
46
- normalize_hash part
47
- when Array
48
- normalize_array part
49
- else
50
- part
51
- end
52
- end
53
- end
54
-
55
- def self.normalize_hash(part)
56
- return [:label, LabelPart.new(part[:label])] if part.key? :label
57
-
58
- if part.key? :start
59
- xdx = normalize_xdx(part[:start])
43
+ tree = [tree] unless tree.first.is_a? Array
60
44
 
61
- return [:start, RollPart.new(xdx)]
62
- end
63
-
64
- part
45
+ tree.map { |part| normalize part }
65
46
  end
66
47
 
67
- def self.normalize_array(part)
68
- [normalize_op(part.first), normalize_value(part.last)]
48
+ def self.normalize(part)
49
+ [
50
+ normalize_op(part.first),
51
+ normalize_value(part.last)
52
+ ]
69
53
  end
70
54
 
71
55
  def self.normalize_op(op)
@@ -84,40 +68,47 @@ module DiceBag
84
68
  end
85
69
 
86
70
  def self.normalize_value(val)
87
- val.is_a?(Hash) ? RollPart.new(normalize_xdx(val)) : StaticPart.new(val)
71
+ case val
72
+ when String
73
+ LabelPart.new val
74
+ when Hash
75
+ RollPart.new normalize_xdx(val)
76
+ else
77
+ StaticPart.new val
78
+ end
88
79
  end
89
80
 
90
81
  # This further massages the xDx hashes.
91
- def self.normalize_xdx(xdx)
92
- count = xdx[:xdx][:count]
93
- sides = xdx[:xdx][:sides]
82
+ def self.normalize_xdx(hash)
83
+ count = hash[:xdx][:count]
84
+ sides = hash[:xdx][:sides]
94
85
 
95
86
  # Delete the no longer needed :xdx key.
96
- xdx.delete(:xdx)
87
+ hash.delete(:xdx)
97
88
 
98
89
  # Default to at least 1 die.
99
90
  count = 1 if count.zero? || count.nil?
100
91
 
101
92
  # Set the :count and :sides keys directly
102
93
  # and set the notes array.
103
- xdx[:count] = count
104
- xdx[:sides] = sides
105
- xdx[:notes] = []
94
+ hash[:count] = count
95
+ hash[:sides] = sides
96
+ hash[:notes] = []
106
97
 
107
- normalize_options xdx
98
+ normalize_options hash
108
99
  end
109
100
 
110
- def self.normalize_options(xdx)
111
- if xdx[:options].empty?
112
- xdx.delete(:options)
101
+ def self.normalize_options(hash)
102
+ if hash[:options].empty?
103
+ hash.delete(:options)
113
104
  else
114
- normalize_explode xdx
115
- normalize_reroll xdx
116
- normalize_drop_keep xdx
117
- normalize_target xdx
105
+ normalize_explode hash
106
+ normalize_reroll hash
107
+ normalize_drop_keep hash
108
+ normalize_target hash
118
109
  end
119
110
 
120
- xdx
111
+ hash
121
112
  end
122
113
 
123
114
  # Prevent Explosion abuse.
@@ -5,16 +5,6 @@ module DiceBag
5
5
  # components. To understand this code, please refer to
6
6
  # the Parslet library's documentation.
7
7
  class Parser < Parslet::Parser
8
- # We override the #parse method, so that we can
9
- # assure that we always return an Array.
10
- def parse(dstr)
11
- result = super dstr
12
-
13
- result = [result] unless result.is_a? Array
14
-
15
- result
16
- end
17
-
18
8
  # Base rules.
19
9
  rule(:space?) { str(' ').repeat }
20
10
 
@@ -64,10 +54,11 @@ module DiceBag
64
54
  # This allows options to be defined in any order and
65
55
  # even have more than one of the same option, however
66
56
  # only the last option of a given key will be kept.
67
- rule(:options) do
68
- space? >> (drop | explode | keep | reroll | target).repeat >> space?
57
+ rule(:option) do
58
+ (drop | explode | keep | reroll | target)
69
59
  end
70
60
 
61
+ rule(:options) { space? >> option.repeat >> space? }
71
62
  rule(:options?) { options.maybe.as(:options) }
72
63
 
73
64
  # Part Operators.
@@ -81,11 +72,7 @@ module DiceBag
81
72
  # A part is an operator, followed by either an xDx
82
73
  # string or a static number value.
83
74
  rule(:part) do
84
- space? >>
85
- op >>
86
- space? >>
87
- (xdx | number).as(:value) >>
88
- space?
75
+ space? >> op >> space? >> (xdx | number).as(:value) >> space?
89
76
  end
90
77
 
91
78
  # All parts of a dice roll MUST start with an xDx
@@ -7,8 +7,7 @@ module RollPartString
7
7
  # string. Optionally, passing true to the method will
8
8
  # remove spaces form the finished string.
9
9
  def to_s(no_spaces = false)
10
- @string = ''
11
- @space = (no_spaces ? '' : ' ')
10
+ @parts = []
12
11
 
13
12
  to_s_xdx
14
13
  to_s_explode
@@ -17,7 +16,9 @@ module RollPartString
17
16
  to_s_reroll
18
17
  to_s_target
19
18
 
20
- @string
19
+ join_str = no_spaces ? '' : ' '
20
+
21
+ @parts.join join_str
21
22
  end
22
23
 
23
24
  private
@@ -26,38 +27,38 @@ module RollPartString
26
27
  c = count.zero? ? '' : count.to_s
27
28
  s = sides.to_s
28
29
 
29
- @string += format('%sd%s', c, s)
30
+ @parts.push format('%sd%s', c, s)
30
31
  end
31
32
 
32
33
  def to_s_explode
33
- return unless @options[:explode].zero?
34
+ return if @options[:explode].zero?
34
35
 
35
36
  e = (@options[:explode] == sides) ? @options[:explode] : ''
36
37
 
37
- @string += format('%se%s', @space, e)
38
+ @parts.push format('e%s', e)
38
39
  end
39
40
 
40
41
  def to_s_drop
41
- return unless @options[:drop].zero?
42
+ return if @options[:drop].zero?
42
43
 
43
- @string += format('%sd%d', @space, @options[:drop])
44
+ @parts.push format('d%d', @options[:drop])
44
45
  end
45
46
 
46
47
  def to_s_keep
47
- return unless @options[:keep].zero?
48
+ return if @options[:keep].zero?
48
49
 
49
- @string += format('%sk%s', @space, @options[:keep])
50
+ @parts.push format('k%s', @options[:keep])
50
51
  end
51
52
 
52
53
  def to_s_reroll
53
- return unless @options[:reroll].zero?
54
+ return if @options[:reroll].zero?
54
55
 
55
- @string += format('%sr%s', @space, @options[:reroll])
56
+ @parts.push format('r%s', @options[:reroll])
56
57
  end
57
58
 
58
59
  def to_s_target
59
- return unless @options[:target].zero?
60
+ return if @options[:target].zero?
60
61
 
61
- @string += format('%st%s', @space, @options[:target])
62
+ @parts.push format('t%s', @options[:target])
62
63
  end
63
64
  end
@@ -4,41 +4,48 @@
4
4
  # generation methods.
5
5
  module RollString
6
6
  def to_s(no_spaces = false)
7
- @string = ''
8
- @space = no_spaces ? '' : ' '
7
+ @parts = []
9
8
 
10
9
  to_s_tree
11
10
 
12
- @string.strip
11
+ str = @parts.join ' '
12
+
13
+ no_spaces ? str.tr(' ', '') : str
13
14
  end
14
15
 
15
16
  private
16
17
 
17
18
  def to_s_tree
18
- tree.each { |op, value| @string += send("to_s_#{op}", value) }
19
+ tree.each do |op, value|
20
+ @parts.push send("to_s_#{op}", value)
21
+ end
19
22
  end
20
23
 
21
24
  def to_s_label(value)
22
- "#{value}#{@space}"
25
+ value.to_s
23
26
  end
24
27
 
25
28
  def to_s_start(value)
26
- "#{value}#{@space}"
29
+ value.to_s
27
30
  end
28
31
 
29
32
  def to_s_add(value)
30
- "+#{@space}#{value}#{@space}"
33
+ _op_value '+', value
31
34
  end
32
35
 
33
36
  def to_s_sub(value)
34
- "-#{@space}#{value}#{@space}"
37
+ _op_value '-', value
35
38
  end
36
39
 
37
40
  def to_s_mul(value)
38
- "*#{@space}#{value}#{@space}"
41
+ _op_value '*', value
39
42
  end
40
43
 
41
44
  def to_s_div(value)
42
- "/#{@space}#{value}#{@space}"
45
+ _op_value '/', value
46
+ end
47
+
48
+ def _op_value(op, value)
49
+ "#{op}#{value}"
43
50
  end
44
51
  end
@@ -9,7 +9,7 @@ module DiceBag
9
9
  def self.hashify_options(options)
10
10
  opts = {}
11
11
 
12
- options.each { |opt, val| opts[opt] = val } if options.is_a?(Array)
12
+ options.each { |val| opts.update val } if options.respond_to? :each
13
13
 
14
14
  opts
15
15
  end
@@ -18,16 +18,14 @@ module DiceBag
18
18
  # 2-element arrays ('tagged arrays'), which is then
19
19
  # hashified later. (There is no way to update the
20
20
  # options when these rules are matched.)
21
- rule(drop: simple(:x)) { [:drop, Integer(x)] }
22
- rule(keep: simple(:x)) { [:keep, Integer(x)] }
23
- rule(reroll: simple(:x)) { [:reroll, Integer(x)] }
24
- rule(target: simple(:x)) { [:target, Integer(x)] }
21
+ rule(drop: simple(:x)) { { drop: Integer(x) } }
22
+ rule(keep: simple(:x)) { { keep: Integer(x) } }
23
+ rule(reroll: simple(:x)) { { reroll: Integer(x) } }
24
+ rule(target: simple(:x)) { { target: Integer(x) } }
25
25
 
26
26
  # Explode is special, in that if it is nil, then it
27
27
  # must remain that way.
28
- rule(explode: simple(:x)) do
29
- [:explode, (x ? Integer(x) : nil)]
30
- end
28
+ rule(explode: simple(:x)) { { explode: (x ? Integer(x) : nil) } }
31
29
 
32
30
  # Parts {:ops => (:xdx | :number)}
33
31
  # These are first-match, so the simple number will
@@ -56,27 +54,24 @@ module DiceBag
56
54
  end
57
55
 
58
56
  # Match a label by itself.
59
- rule(label: simple(:s)) { { label: String(s) } }
57
+ rule(label: simple(:s)) { [:label, String(s)] }
60
58
 
61
59
  # Match a label followed by a :start subtree.
62
60
  rule(label: simple(:s), start: subtree(:part)) do
63
- label = String(s)
64
- xdx = part[:xdx]
65
- options = Transform.hashify_options(part[:options])
61
+ label = String(s)
66
62
 
67
- [{ label: label }, { start: { xdx: xdx, options: options } }]
63
+ [[:label, label], [:start, part]]
68
64
  end
69
65
 
70
66
  # Match a :start subtree, with the label not present.
71
67
  # Note that this returns a hash, but the final output
72
68
  # will still be in an array.
73
69
  rule(start: subtree(:part)) do
74
- {
75
- start: {
76
- xdx: part[:xdx],
77
- options: Transform.hashify_options(part[:options])
78
- }
79
- }
70
+ [:start, part]
71
+ end
72
+
73
+ rule(xdx: subtree(:xdx), options: subtree(:opts)) do
74
+ { xdx: xdx, options: Transform.hashify_options(opts) }
80
75
  end
81
76
 
82
77
  # Convert the count and sides of an :xdx part.
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dicebag
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.2.0
4
+ version: 3.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - SynTruth
@@ -14,6 +14,9 @@ dependencies:
14
14
  name: parslet
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.4'
17
20
  - - ">="
18
21
  - !ruby/object:Gem::Version
19
22
  version: 1.4.0
@@ -21,6 +24,9 @@ dependencies:
21
24
  prerelease: false
22
25
  version_requirements: !ruby/object:Gem::Requirement
23
26
  requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '1.4'
24
30
  - - ">="
25
31
  - !ruby/object:Gem::Version
26
32
  version: 1.4.0
@@ -42,7 +48,8 @@ files:
42
48
  - lib/dicebag/static_part.rb
43
49
  - lib/dicebag/transform.rb
44
50
  homepage: https://github.com/syntruth/Dice-Bag
45
- licenses: []
51
+ licenses:
52
+ - MIT
46
53
  metadata: {}
47
54
  post_install_message:
48
55
  rdoc_options: []