cardname 0.3.13 → 0.4.0

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: a62968f99bcd0657cbd165f76d56823db7d6c862
4
- data.tar.gz: 4404675f606f45a36cc848bbab7c90d811eb97e5
3
+ metadata.gz: d8a43b73f7cdbeeb1711642bec6713472a29444e
4
+ data.tar.gz: c99ad7d12caa1d33f1c1c98b97230085a418946d
5
5
  SHA512:
6
- metadata.gz: 702b68602eea4b4e406aeae8704522021a74ef107634f3ec3975a9a622c39d3cf83149dbbbf6ed1fb74c1c43be7e096b24326b320ed030b04597b45636941f06
7
- data.tar.gz: 66fe9ed8593211ba788d459feeda9f5e07dca5db75e33bb89d6d44cc51a50789aedc31550dc02e6d4528649f86e0bf2ca26b1710efb746796988e4b871ca9c36
6
+ metadata.gz: 59ed2b78bf8d651941c8bc60fe4a6f229be00bc9bce6739d3fa4eaea7fe5d4160f4f1ffdf3f7f7220405288d146ba5aec15f475850ab8defa60b00ad2a1d5d48
7
+ data.tar.gz: 151d18229b037833ed54f22fe1f5e8715d0680b2a3b88a21d837aa5248e52c4344f86e07d65d8f4ac9f41ceb003fcaf98660bb293f5aaef87ca84673d0159c3f
@@ -120,7 +120,7 @@ class Cardname
120
120
  next if new_parts[i].present?
121
121
  # following avoids recontextualizing with relative contexts.
122
122
  # Eg, `+A+B+.absolute('+A')` should be +A+B, not +A+A+B.
123
- next if new_parts.to_name.send "#{[ :start, :end ][i]}s_with?", context
123
+ next if new_parts.to_name.send "#{[ :start, :end ][i]}s_with_parts?", context
124
124
  new_parts[i] = context
125
125
  end
126
126
  end
@@ -27,7 +27,7 @@ class Cardname
27
27
  newpiece = newpiece.to_name
28
28
 
29
29
  return swap_part oldpiece, newpiece if oldpiece.simple?
30
- return self unless self.starts_with?(oldpiece)
30
+ return self unless starts_with_parts?(oldpiece)
31
31
  return newpiece if oldpiece.num_parts == num_parts
32
32
  self.class.new [newpiece, self[oldpiece.num_parts..-1]].flatten
33
33
  end
@@ -8,7 +8,7 @@ class Cardname
8
8
  alias_method :to_a, :parts
9
9
 
10
10
  def parts
11
- @parts = s.split(/\s*#{JOINT_RE}\s*/, -1)
11
+ @parts = Cardname.split_parts s
12
12
  end
13
13
 
14
14
  def simple
@@ -12,18 +12,18 @@ class Cardname
12
12
  end
13
13
 
14
14
  # @return true if name starts with the same parts as `prefix`
15
- def starts_with? prefix
15
+ def starts_with_parts? *prefix
16
16
  start_name = prefix.to_name
17
17
  start_name == self[0, start_name.num_parts]
18
18
  end
19
- alias_method :start_with?, :starts_with?
19
+ alias_method :start_with_parts?, :starts_with_parts?
20
20
 
21
21
  # @return true if name ends with the same parts as `prefix`
22
- def ends_with? postfix
23
- end_name = postfix.to_name
22
+ def ends_with_parts? *suffix
23
+ end_name = suffix.to_name
24
24
  end_name == self[-end_name.num_parts..-1]
25
25
  end
26
- alias_method :end_with?, :ends_with?
26
+ alias_method :end_with_parts?, :ends_with_parts?
27
27
 
28
28
  # @return true if name has a chain of parts that equals `subname`
29
29
  def include? subname
data/lib/cardname.rb CHANGED
@@ -5,7 +5,6 @@ require 'active_support/inflector'
5
5
  require 'htmlentities'
6
6
 
7
7
  class Cardname < String
8
-
9
8
  require_relative 'cardname/parts'
10
9
  require_relative 'cardname/variants'
11
10
  require_relative 'cardname/contextual'
@@ -26,7 +25,7 @@ class Cardname < String
26
25
  :session, :stabilize
27
26
 
28
27
  Cardname.joint = '+'
29
- Cardname.banned_array = %w{ / }
28
+ Cardname.banned_array = []
30
29
  Cardname.var_re = /\{([^\}]*\})\}/
31
30
  Cardname.uninflect = :singularize
32
31
  Cardname.stabilize = false
@@ -38,7 +37,7 @@ class Cardname < String
38
37
  class << self
39
38
  def new obj
40
39
  return obj if obj.is_a? self.class
41
- str = stringify obj
40
+ str = stringify(obj)
42
41
  cached_name(str) || super(str)
43
42
  end
44
43
 
@@ -59,7 +58,8 @@ class Cardname < String
59
58
  end
60
59
 
61
60
  def banned_re
62
- %r{#{ (['['] + banned_array << joint) * '\\' + ']' }}
61
+ banned_chars = (banned_array << joint).join
62
+ /[#{Regexp.escape banned_chars}]/
63
63
  end
64
64
 
65
65
  # Sometimes the core rule "the key's key must be itself" (called "stable" below) is violated
@@ -74,6 +74,15 @@ class Cardname < String
74
74
  return key_one unless key_one != key_two
75
75
  stabilize ? stable_key(key_two) : name
76
76
  end
77
+
78
+ def dangerous_methods
79
+ bang_methods = String.instance_methods.select { |m| m.to_s.ends_with?("!") }
80
+ [:replace].concat bang_methods
81
+ end
82
+
83
+ def split_parts str
84
+ str.split(/\s*#{JOINT_RE}\s*/, -1)
85
+ end
77
86
  end
78
87
 
79
88
  # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -94,6 +103,13 @@ class Cardname < String
94
103
  self
95
104
  end
96
105
 
106
+ dangerous_methods.each do |m|
107
+ define_method m do |*args, &block|
108
+ reset
109
+ super(*args, &block)
110
+ end
111
+ end
112
+
97
113
  def key
98
114
  @key ||= part_keys.join(self.class.joint)
99
115
  end
@@ -107,4 +123,12 @@ class Cardname < String
107
123
  end
108
124
  other_key == key
109
125
  end
126
+
127
+ private
128
+
129
+ def reset
130
+ instance_variables.each do |var|
131
+ instance_variable_set var, nil
132
+ end
133
+ end
110
134
  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.3.13
4
+ version: 0.4.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: 2018-03-01 00:00:00.000000000 Z
13
+ date: 2018-03-20 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activesupport