cardname 0.2.2 → 0.3.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: 99ec2a308a3378e69b7dcf8a88cd249c70b2e37b
4
- data.tar.gz: 51ab12c658f6639b0479731323bd43734f787060
3
+ metadata.gz: bf908ff88412470eef1eb037b0a90b398e419ad4
4
+ data.tar.gz: 791c5cf2d06a2b7d572262fb9f16d56cb647c0b3
5
5
  SHA512:
6
- metadata.gz: 3c49c639c5ce9f90a08a03bdba4ea4a4ff53681c931a1dda58409eadf81d71b5821ca64e511d2bebecda21c509ffca082dcaf913f13daffc64854bc1b7b9f4b1
7
- data.tar.gz: ed8a1afaa7932b2444742d42de55700298e9ade05eed4be6a6dabcf6a5bd53f4555a585228cd6b3cac36c6ffb775c6e242ba5a36e13043f1609b9ba05d8f3e5b
6
+ metadata.gz: 4298e82e31e1b987bd42a589f52bb13f3ee0e9e611277f64307de182db345ad34436ec4f53310b2901d447f0063338d2e53081132c288b9fa1d0e4b65c9ab985
7
+ data.tar.gz: ed7fe969ab7f71ff59ff998640b0d5489d02147501e92aedf1f6e9f30028ed178ef72f38765a5c144ffbeda2fb2f827808d4b47d2d7296e7a8296cd08e30600c
@@ -1,18 +1,18 @@
1
1
  class Cardname
2
2
  module Manipulate
3
- # replace a subname
3
+ # swap a subname
4
4
  # keys are used for comparison
5
- def replace old, new
5
+ def swap old, new
6
6
  old_name = old.to_name
7
7
  new_name = new.to_name
8
- return self if old_name.length > length
9
- return replace_part(old_name, new_name) if old_name.simple?
8
+ return self if old_name.num_parts > num_parts
9
+ return swap_part(old_name, new_name) if old_name.simple?
10
10
  return self unless include? old_name
11
- replace_all_subsequences(old_name, new_name).to_name
11
+ swap_all_subsequences(old_name, new_name).to_name
12
12
  end
13
13
 
14
- def replace_part oldpart, newpart
15
- ensure_simpliness oldpart, "Use 'replace' to replace junctions"
14
+ def swap_part oldpart, newpart
15
+ ensure_simpleness oldpart, "Use 'swap' to swap junctions"
16
16
 
17
17
  oldpart = oldpart.to_name
18
18
  newpart = newpart.to_name
@@ -22,37 +22,46 @@ class Cardname
22
22
  end.to_name
23
23
  end
24
24
 
25
- def replace_piece oldpiece, newpiece
25
+ def swap_piece oldpiece, newpiece
26
26
  oldpiece = oldpiece.to_name
27
27
  newpiece = newpiece.to_name
28
28
 
29
- return replace_part oldpiece, newpiece if oldpiece.simple?
29
+ return swap_part oldpiece, newpiece if oldpiece.simple?
30
30
  return self unless self.starts_with?(oldpiece)
31
- return newpiece if oldpiece.length == length
32
- newpiece + self[oldpiece.length..-1]
31
+ return newpiece if oldpiece.num_parts == num_parts
32
+ self.class.new [newpiece, self[oldpiece.num_parts..-1]].flatten
33
+ end
34
+
35
+ def num_parts
36
+ parts.length
37
+ end
38
+
39
+ def prepend_joint
40
+ joint = self.class.joint
41
+ self =~ /^#{Regexp.escape joint}/ ? self : (joint + self)
33
42
  end
34
43
 
35
44
  private
36
45
 
37
- def replace_all_subsequences oldseq, newseq
46
+ def swap_all_subsequences oldseq, newseq
38
47
  res = []
39
48
  i = 0
40
- while i <= length - oldseq.length
49
+ while i <= num_parts - oldseq.num_parts
41
50
  # for performance reasons: check first character first then the rest
42
51
  if oldseq.part_keys.first == part_keys[i] &&
43
- oldseq.part_keys == part_keys[i, oldseq.length]
52
+ oldseq.part_keys == part_keys[i, oldseq.num_parts]
44
53
  res += newseq.parts
45
- i += oldseq.length
54
+ i += oldseq.num_parts
46
55
  else
47
56
  res << parts[i]
48
57
  i += 1
49
58
  end
50
59
  end
51
- res += parts[i..-1] if i < length
60
+ res += parts[i..-1] if i < num_parts
52
61
  res
53
62
  end
54
63
 
55
- def ensure_simpliness part, msg=nil
64
+ def ensure_simpleness part, msg=nil
56
65
  return if part.to_name.simple?
57
66
  raise StandardError, "'#{part}' has to be simple. #{msg}"
58
67
  end
@@ -5,16 +5,19 @@ class Cardname
5
5
  module Parts
6
6
  attr_reader :parts, :part_keys, :simple
7
7
 
8
- alias simple? simple
9
8
  alias_method :to_a, :parts
10
9
 
11
- def initialize_parts
12
- # -1 = don't suppress trailing null fields
13
- @parts = @s.split(/\s*#{JOINT_RE}\s*/, -1)
14
- @simple = @parts.size <= 1
15
- # simple check needed to avoid inifinite recursion
16
- @part_keys =
17
- @simple ? [simple_key] : @parts.map { |p| p.to_name.simple_key }
10
+ def parts
11
+ @parts = s.split(/\s*#{JOINT_RE}\s*/, -1)
12
+ end
13
+
14
+ def simple
15
+ @simple = parts.size <= 1
16
+ end
17
+ alias simple? simple
18
+
19
+ def part_keys
20
+ @part_keys ||= simple ? [simple_key] : parts.map { |p| p.to_name.simple_key }
18
21
  end
19
22
 
20
23
  def left
@@ -99,16 +102,16 @@ class Cardname
99
102
  end
100
103
  end
101
104
 
102
- def + other
103
- self.class.new(parts + other.to_name.parts)
104
- end
105
+ # def + other
106
+ # self.class.new(parts + other.to_name.parts)
107
+ # end
105
108
 
106
109
  def [] *args
107
110
  self.class.new parts[*args]
108
111
  end
109
112
 
110
113
  # full support of array methods caused trouble with `flatten` calls
111
- # It splits the parts of cardnames in arrays
114
+ # It splits the parts of names in arrays
112
115
  # # name parts can be accessed and manipulated like an array
113
116
  # def method_missing method, *args, &block
114
117
  # if ARRAY_METHODS.include? method # parts.respond_to?(method)
@@ -5,11 +5,6 @@ class Cardname
5
5
  !simple?
6
6
  end
7
7
 
8
- def blank?
9
- s.blank?
10
- end
11
- alias empty? blank?
12
-
13
8
  def valid?
14
9
  !parts.find do |pt|
15
10
  pt.match self.class.banned_re
@@ -19,14 +14,14 @@ class Cardname
19
14
  # @return true if name starts with the same parts as `prefix`
20
15
  def starts_with? prefix
21
16
  start_name = prefix.to_name
22
- start_name == self[0, start_name.length]
17
+ start_name == self[0, start_name.num_parts]
23
18
  end
24
19
  alias_method :start_with?, :starts_with?
25
20
 
26
21
  # @return true if name ends with the same parts as `prefix`
27
22
  def ends_with? postfix
28
23
  end_name = postfix.to_name
29
- end_name == self[-end_name.length..-1]
24
+ end_name == self[-end_name.num_parts..-1]
30
25
  end
31
26
  alias_method :end_with?, :ends_with?
32
27
 
@@ -1,7 +1,7 @@
1
1
  class Cardname
2
2
  module Variants
3
3
  def simple_key
4
- return "" if @s.empty?
4
+ return "" if empty?
5
5
  decoded
6
6
  .underscore
7
7
  .gsub(/[^#{OK4KEY_RE}]+/, '_')
data/lib/cardname.rb CHANGED
@@ -3,13 +3,14 @@
3
3
  require 'active_support/configurable'
4
4
  require 'active_support/inflector'
5
5
  require 'htmlentities'
6
- require_relative 'cardname/parts'
7
- require_relative 'cardname/variants'
8
- require_relative 'cardname/contextual'
9
- require_relative 'cardname/predicates'
10
- require_relative 'cardname/manipulate'
11
6
 
12
- class Cardname < Object
7
+ class Cardname < String
8
+
9
+ require_relative 'cardname/parts'
10
+ require_relative 'cardname/variants'
11
+ require_relative 'cardname/contextual'
12
+ require_relative 'cardname/predicates'
13
+ require_relative 'cardname/manipulate'
13
14
 
14
15
  include Parts
15
16
  include Variants
@@ -17,8 +18,7 @@ class Cardname < Object
17
18
  include Predicates
18
19
  include Manipulate
19
20
 
20
- RUBYENCODING = RUBY_VERSION !~ /^1\.8/
21
- OK4KEY_RE = RUBYENCODING ? '\p{Word}\*' : '\w\*'
21
+ OK4KEY_RE = '\p{Word}\*'
22
22
 
23
23
  include ActiveSupport::Configurable
24
24
 
@@ -78,32 +78,24 @@ class Cardname < Object
78
78
 
79
79
  # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
80
80
  # ~~~~~~~~~~~~~~~~~~~~~~ INSTANCE ~~~~~~~~~~~~~~~~~~~~~~~~~
81
- attr_reader :key, :s
82
- alias to_s s
83
- alias to_str s
81
+ attr_reader :key
84
82
 
85
83
  def initialize str
86
- @s = str.to_s.strip
87
- @s = @s.encode('UTF-8') if RUBYENCODING
88
- initialize_parts
89
- @key = @part_keys.join(self.class.joint)
90
- @@cache[str] = self
84
+ @@cache[str] = super str.strip.encode('UTF-8')
91
85
  end
92
86
 
93
- def to_name
94
- self
95
- end
96
-
97
- def length
98
- parts.length
87
+ def s
88
+ @s ||= String.new self
99
89
  end
90
+ alias to_s s
91
+ alias to_str s
100
92
 
101
- def size
102
- to_s.size
93
+ def to_name
94
+ self
103
95
  end
104
96
 
105
- def inspect
106
- "<#{self.class.name} key=#{key}[#{self}]>"
97
+ def key
98
+ @key ||= part_keys.join(self.class.joint)
107
99
  end
108
100
 
109
101
  def == other
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.2.2
4
+ version: 0.3.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: 2017-10-09 00:00:00.000000000 Z
13
+ date: 2017-10-31 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activesupport
@@ -75,7 +75,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
75
75
  requirements:
76
76
  - - ">="
77
77
  - !ruby/object:Gem::Version
78
- version: '0'
78
+ version: '2.3'
79
79
  required_rubygems_version: !ruby/object:Gem::Requirement
80
80
  requirements:
81
81
  - - ">="