cardname 0.13.4 → 0.14.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fec4ca9f2762b2046c7897c254af22a4b91b594ebbfa9d33f79dba177e4b17f3
4
- data.tar.gz: 75bf8e0d3cfa758a07a6f280b433815be3e9562454446775d97c926b0ca7e3d5
3
+ metadata.gz: d42f369012c637f78375a460e4814d841ed45fee9f4f3d7eb5f9dee84f725dd0
4
+ data.tar.gz: 5bbfbdfcaaf320fa1e0853e5ec781c54b38cd9a38e41bffdb12a52820fc768ed
5
5
  SHA512:
6
- metadata.gz: f0e9a1ace8af5e1dbc94547bb6ff30523eb46451dcab4e7ec0f504e3ccdf0a64a89efc36279464e98f8f0f166c691ed61e881ee3b5a5c8b81f9289eb1e0c8b3b
7
- data.tar.gz: 531c17b9e620f59a7f56dbb5bf8388b29ba21a987e49a5db0870b58d5d8ec509ad058aa5b9b723f9d4262b0138743e9cf41abd4d32740272f3a1095c50fff1fc
6
+ metadata.gz: a3376035abe1d69ed768e6f3eb040106a87985c6a20fb9dfb939d416589f804e6c820ff325e9e819ac43d3529513cdc6fd6a03e01fd50301c7b1d958a8ad4d2d
7
+ data.tar.gz: 0ec2da901b07b528bfe7ddaaecc50a4ff8e65533d4282e51f74e9595214ebfe012cccbdff2729d4b3fd0613f7fa24eac091b25444375745a038a93d6c8583c5a
@@ -34,7 +34,8 @@ class Cardname
34
34
  name_from(*from).s
35
35
  end
36
36
 
37
- # if possible, relativize name into one beginning with a "+". The new name must absolutize back to the correct
37
+ # if possible, relativize name into one beginning with a "+".
38
+ # The new name must absolutize back to the correct
38
39
  # original name in the context of "from"
39
40
  def name_from *from
40
41
  return self unless (remaining = remove_context(*from))
@@ -38,6 +38,10 @@ class Cardname
38
38
  parts.length
39
39
  end
40
40
 
41
+ def [] *args
42
+ self.class.new part_names[*args]
43
+ end
44
+
41
45
  def prepend_joint
42
46
  joint = self.class.joint
43
47
  self =~ /^#{Regexp.escape joint}/ ? self : (joint + self)
@@ -3,106 +3,92 @@ class Cardname
3
3
  # methods that end with _name return name objects
4
4
  # the same methods without _name return strings
5
5
  module Parts
6
- attr_reader :parts, :part_keys, :simple
7
- alias_method :to_a, :parts
6
+ # PARTS
7
+ def part_names
8
+ @part_names ||= generate_part_names
9
+ end
8
10
 
9
11
  def parts
10
- @parts = Cardname.split_parts s
12
+ part_names.map(&:s)
11
13
  end
14
+ alias_method :to_a, :parts
12
15
 
13
16
  def part_keys
14
- @part_keys ||= simple ? [simple_key] : parts.map { |p| p.to_name.simple_key }
17
+ part_names.map(&:key)
15
18
  end
16
19
 
17
- def left
18
- @left ||= simple? ? nil : parts[0..-2] * self.class.joint
19
- end
20
-
21
- def right
22
- @right ||= simple? ? nil : parts[-1]
20
+ def trunk_name
21
+ simple? ? self : left_name
23
22
  end
24
23
 
25
- def left_name
26
- @left_name ||= left && self.class.new(left)
24
+ def trunk
25
+ trunk_name.s
27
26
  end
28
27
 
29
- def right_name
30
- @right_name ||= right && self.class.new(right)
28
+ def trunk_key
29
+ trunk_name.key
31
30
  end
32
31
 
33
- def left_key
34
- @left_key ||= simple? ? nil : part_keys[0..-2] * self.class.joint
32
+ def tag_name
33
+ simple? ? self : right_name
35
34
  end
36
35
 
37
- def right_key
38
- @right_key ||= simple? ? nil : part_keys.last
36
+ def tag
37
+ tag_name.s
39
38
  end
40
39
 
41
- def parents
42
- @parents ||= compound? ? [left, right] : []
40
+ def tag_key
41
+ tag_name.key
43
42
  end
44
43
 
45
44
  def parent_names
46
- @parent_names ||= compound? ? [left_name, right_name] : []
45
+ simple? ? [] : [left_name, right_name]
47
46
  end
48
47
 
49
- def parent_keys
50
- @parent_keys ||= compound? ? [left_key, right_key] : []
48
+ def parents
49
+ parent_names.map(&:s)
51
50
  end
52
51
 
53
- # Note that all names have a trunk and tag,
54
- # but only junctions have left and right
55
-
56
- def trunk
57
- @trunk ||= simple? ? s : left
52
+ def parent_keys
53
+ parent_names.map(&:key)
58
54
  end
59
55
 
60
- def tag
61
- @tag ||= simple? ? s : right
56
+ def left_name
57
+ simple? ? nil : self.class.new(part_names[0..-2])
62
58
  end
63
59
 
64
- def trunk_name
65
- @trunk_name ||= simple? ? self : left_name
60
+ def left
61
+ left_name&.s
66
62
  end
67
63
 
68
- def tag_name
69
- @tag_name ||= simple? ? self : right_name
64
+ def left_key
65
+ left_name&.key
70
66
  end
71
67
 
72
- def part_names
73
- @part_names ||= parts.map(&:to_name)
68
+ def right_name
69
+ simple? ? nil : part_names[-1]
74
70
  end
75
71
 
76
- def piece_names
77
- @piece_names ||= pieces.map(&:to_name)
72
+ def right
73
+ right_name&.s
78
74
  end
79
75
 
80
- def ancestors
81
- @ancestors ||= pieces.reject { |p| p == self }
76
+ def right_key
77
+ right_name&.key
82
78
  end
83
79
 
84
- # def + other
85
- # self.class.new(parts + other.to_name.parts)
86
- # end
80
+ private
87
81
 
88
- def [] *args
89
- self.class.new parts[*args]
82
+ def generate_part_names
83
+ return blank_part_names if blank?
84
+ parts = Cardname.split_parts s
85
+ @simple = parts.size <= 1
86
+ @simple ? [self] : parts.map(&:to_name)
90
87
  end
91
88
 
92
- # full support of array methods caused trouble with `flatten` calls
93
- # It splits the parts of names in arrays
94
- # # name parts can be accessed and manipulated like an array
95
- # def method_missing method, *args, &block
96
- # if ARRAY_METHODS.include? method # parts.respond_to?(method)
97
- # self.class.new parts.send(method, *args, &block)
98
- # else
99
- # super
100
- # end
101
- # end
102
- #
103
- # def respond_to? method, include_private=false
104
- # return true if ARRAY_METHODS.include? method
105
- # super || parts.respond_to?(method, include_private)
106
- # end
89
+ def blank_part_names
90
+ @simple = true
91
+ []
92
+ end
107
93
  end
108
94
  end
@@ -8,7 +8,15 @@ class Cardname
8
8
  # "A+B+C+D".to_name.pieces
9
9
  # # => ["A", "B", "C", "D", "A+B", "A+B+C", "A+B+C+D"]
10
10
  def pieces
11
- @pieces ||= simple? ? [self] : (parts + junction_pieces)
11
+ simple? ? [self] : (parts + junction_pieces)
12
+ end
13
+
14
+ def piece_names
15
+ pieces.map(&:to_name)
16
+ end
17
+
18
+ def ancestors
19
+ pieces.reject { |p| p == self }
12
20
  end
13
21
 
14
22
  private
@@ -1,15 +1,15 @@
1
1
  class Cardname
2
2
  module Predicates
3
+ # @return true if name has only one part
3
4
  def simple?
4
- @simple.nil? ? (@simple = parts.size <= 1) : @simple
5
+ part_names if @simple.nil?
6
+ @simple
5
7
  end
6
- alias_method :simple, :simple? # @deprecated
7
8
 
8
9
  # @return true if name has more than one part
9
10
  def compound?
10
11
  !simple?
11
12
  end
12
- alias_method :junction?, :compound? # @deprecated
13
13
 
14
14
  def valid?
15
15
  return true if self.class.nothing_banned?
@@ -15,7 +15,7 @@ class Cardname
15
15
  end
16
16
 
17
17
  def url_key
18
- @url_key ||= part_names.map do |part_name|
18
+ part_names.map do |part_name|
19
19
  stripped = part_name.decoded.gsub(/[^#{OK4KEY_RE}]+/, " ").strip
20
20
  stripped.gsub(/[\s_]+/, "_")
21
21
  end * self.class.joint
@@ -25,7 +25,7 @@ class Cardname
25
25
  # but the key is no longer unique.
26
26
  # For example "A-XB" and "A+*B" have the same safe_key
27
27
  def safe_key
28
- @safe_key ||= key.tr("*", "X").tr self.class.joint, "-"
28
+ key.tr("*", "X").tr self.class.joint, "-"
29
29
  end
30
30
 
31
31
  def decoded
data/lib/cardname.rb CHANGED
@@ -10,7 +10,6 @@ class Cardname < String
10
10
  require "cardname/contextual"
11
11
  require "cardname/predicates"
12
12
  require "cardname/manipulate"
13
- require "cardname/danger"
14
13
 
15
14
  include Parts
16
15
  include Pieces
@@ -18,7 +17,6 @@ class Cardname < String
18
17
  include Contextual
19
18
  include Predicates
20
19
  include Manipulate
21
- include Danger
22
20
 
23
21
  OK4KEY_RE = '\p{Word}\*'
24
22
 
@@ -33,31 +31,15 @@ class Cardname < String
33
31
  JOINT_RE = Regexp.escape joint
34
32
 
35
33
  class << self
36
- def cache
37
- @cache ||= {}
38
- end
39
-
40
34
  def new obj
41
35
  return obj if obj.is_a? self.class
42
36
 
43
37
  str = stringify(obj)
44
- cached_name(str) || super(str)
38
+ cache[str] ||= super(str)
45
39
  end
46
40
 
47
- def cached_name str
48
- cache[str]
49
- end
50
-
51
- def reset_cache str=nil
52
- str ? cache.delete(str) : @cache = {}
53
- end
54
-
55
- def stringify obj
56
- if obj.is_a?(Array)
57
- obj.map(&:to_s) * joint
58
- else
59
- obj.to_s
60
- end
41
+ def reset
42
+ @cache = {}
61
43
  end
62
44
 
63
45
  def nothing_banned?
@@ -73,6 +55,20 @@ class Cardname < String
73
55
  def split_parts str
74
56
  str.split(/\s*#{JOINT_RE}\s*/, -1)
75
57
  end
58
+
59
+ def cache
60
+ @cache ||= {}
61
+ end
62
+
63
+ private
64
+
65
+ def stringify obj
66
+ if obj.is_a?(Array)
67
+ obj.map(&:to_s) * joint
68
+ else
69
+ obj.to_s
70
+ end
71
+ end
76
72
  end
77
73
 
78
74
  # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -80,14 +76,21 @@ class Cardname < String
80
76
  attr_reader :key
81
77
 
82
78
  def initialize str
83
- self.class.cache[str] = super str.strip.encode("UTF-8")
79
+ super str
80
+ strip!
81
+ encode! "UTF-8"
82
+ part_names # populates @part_names and @simple
83
+ decoded # populates @decoded
84
+ key # populates and freezes @key
85
+ freeze
84
86
  end
85
87
 
86
88
  def s
87
- @s ||= String.new self
89
+ String.new self
88
90
  end
89
91
  alias_method :to_s, :s
90
92
  alias_method :to_str, :s
93
+ # alias_method :dup, :clone
91
94
 
92
95
  def to_name
93
96
  self
@@ -104,7 +107,7 @@ class Cardname < String
104
107
  end
105
108
 
106
109
  def key
107
- @key ||= part_keys.join(self.class.joint)
110
+ @key ||= generate_key.freeze
108
111
  end
109
112
 
110
113
  def == other
@@ -112,7 +115,13 @@ class Cardname < String
112
115
  case
113
116
  when other.respond_to?(:key) then other.key
114
117
  when other.respond_to?(:to_name) then other.to_name.key
115
- else other.to_s
118
+ else other.to_s.to_name.key
116
119
  end
117
120
  end
121
+
122
+ private
123
+
124
+ def generate_key
125
+ @simple ? simple_key : part_keys.join(self.class.joint)
126
+ end
118
127
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cardname
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.13.4
4
+ version: 0.14.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ethan McCutchen
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2021-09-24 00:00:00.000000000 Z
13
+ date: 2021-12-22 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activesupport
@@ -51,7 +51,6 @@ files:
51
51
  - Rakefile
52
52
  - lib/cardname.rb
53
53
  - lib/cardname/contextual.rb
54
- - lib/cardname/danger.rb
55
54
  - lib/cardname/manipulate.rb
56
55
  - lib/cardname/parts.rb
57
56
  - lib/cardname/pieces.rb
@@ -82,7 +81,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
82
81
  - !ruby/object:Gem::Version
83
82
  version: '0'
84
83
  requirements: []
85
- rubygems_version: 3.1.6
84
+ rubygems_version: 3.2.15
86
85
  signing_key:
87
86
  specification_version: 4
88
87
  summary: Card names without all the cards
@@ -1,23 +0,0 @@
1
- class Cardname
2
- # methods that have the potential to confuse cardname cache prompt cache resets
3
- module Danger
4
- def self.dangerous_methods
5
- bang_methods = String.instance_methods.select { |m| m.to_s.end_with?("!") }
6
- %i[replace concat clear].concat bang_methods
7
- end
8
-
9
- dangerous_methods.each do |m|
10
- define_method m do |*args, &block|
11
- reset
12
- super(*args, &block)
13
- end
14
- end
15
-
16
- private
17
-
18
- def reset
19
- self.class.reset_cache s
20
- instance_variables.each { |var| instance_variable_set var, nil }
21
- end
22
- end
23
- end