cardname 0.1 → 0.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: 15f37bd2c96222cfff562f06782c52e27a803dc0
4
- data.tar.gz: 86752b10ed33282d3cafa93eaca1320aea1e8e94
3
+ metadata.gz: 5cc3696416bb4628e7059042f1e6761cf61f49a9
4
+ data.tar.gz: ade3ca4f9d5d2a312723442ebc6c16d6ffdd569b
5
5
  SHA512:
6
- metadata.gz: d66817487594c22c30d7f1374c53711312c6d314609ec0d34ac8724def65bb7e853f1a5ce5e29a03aec902b4c28d136d535b2cbae2e87d7fa069575139ed8fdf
7
- data.tar.gz: a3867d0234a9f27360e3bcf47328c5ff1380066b1af3ffad1487578796c0f6a47f14b5008d970d87b3ae3697fb12c86ebeafc8753d19cf13f9178437b416e498
6
+ metadata.gz: 3abca29801123d048d89189fbf9079ee70d6f14a24c63732a77fdcaa8c833c9481732f50a40248c8aad72b65438806eae7a23f930432f36a1575af7d6216b992
7
+ data.tar.gz: e44752dd9ba9670e5082d8cbe45b169d7d54d7e8b8497e18b61c7309e096884898fc78bbb0d4eba2ad152d46c1ba2fa1f13d5504c97ce8a2b1e629487408188f
@@ -2,14 +2,6 @@ class Cardname
2
2
  module Contextual
3
3
  RELATIVE_REGEXP = /\b_(left|right|whole|self|user|main|\d+|L*R?)\b/
4
4
 
5
- def relative_name context_name
6
- to_show(*context_name.to_name.parts).to_name
7
- end
8
-
9
- def absolute_name context_name
10
- to_absolute_name(context_name)
11
- end
12
-
13
5
  # @return true if name is left or right of context
14
6
  def child_of? context
15
7
  return false unless junction?
@@ -18,11 +10,11 @@ class Cardname
18
10
  end
19
11
 
20
12
  def relative?
21
- s =~ RELATIVE_REGEXP || starts_with_joint?
13
+ starts_with_joint? || (s =~ RELATIVE_REGEXP).present?
22
14
  end
23
15
 
24
16
  def simple_relative?
25
- relative? && stripped.to_name.starts_with_joint?
17
+ starts_with_joint? && (s =~ RELATIVE_REGEXP).nil?
26
18
  end
27
19
 
28
20
  def absolute?
@@ -34,43 +26,52 @@ class Cardname
34
26
  end
35
27
 
36
28
  def starts_with_joint?
37
- length >= 2 && parts.first.empty?
29
+ junction? && parts.first.empty?
38
30
  end
39
31
 
40
- def to_show *ignore
41
- ignore.map!(&:to_name)
32
+ def from *from
33
+ name_from(*from).s
34
+ end
42
35
 
43
- show_parts = parts.map do |part|
44
- reject = (part.empty? || (part =~ /^_/) || ignore.member?(part.to_name))
45
- reject ? nil : part
46
- end
36
+ # if possible, relativize name into one beginning with a "+". To do so, it must absolutize back to the correct
37
+ # original name in the context of "from"
38
+ def name_from *from
39
+ remaining = parts_excluding *from
40
+ return self unless context_relevant?(remaining)
41
+ compressed = remaining.compact.unshift(nil).to_name # exactly one nil at beginning
42
+ key == compressed.absolute_name(from).key ? compressed : self
43
+ end
47
44
 
48
- show_name = show_parts.compact.to_name.s
45
+ def context_relevant? remaining
46
+ remaining.compact.any? && # not all name parts in context
47
+ remaining != parts # some name parts in context (could be faster test!)
48
+ end
49
49
 
50
- case
51
- when show_parts.compact.empty? then self
52
- when show_parts[0].nil? then self.class.joint + show_name
53
- else show_name
54
- end
50
+ def parts_excluding *string
51
+ exclude_name = string.to_name
52
+ exclude_keys = exclude_name ? exclude_name.part_names.map(&:key) : []
53
+ parts_minus exclude_keys
55
54
  end
56
55
 
57
- def to_absolute context, args={}
58
- context = context.to_name
56
+ def parts_minus keys_to_ignore
57
+ parts.map do |part|
58
+ next if part.empty?
59
+ next if part =~ /^_/ # this removes relative parts. why?
60
+ next if keys_to_ignore.member? part.to_name.key
61
+ part
62
+ end
63
+ end
59
64
 
60
- new_parts = replace_contextual_parts context
65
+ def absolute context, args={}
66
+ context = (context || "").to_name
67
+ new_parts = absolutize_contextual_parts context
61
68
  return "" if new_parts.empty?
62
-
63
- if new_parts.first.empty? && !new_parts.to_name.starts_with?(context)
64
- new_parts[0] = context.to_s
65
- end
66
- if new_parts.last.empty? && !new_parts.to_name.ends_with?(context)
67
- new_parts[-1] = context.to_s
68
- end
69
+ absolutize_extremes new_parts, context.s
69
70
  new_parts.join self.class.joint
70
71
  end
71
72
 
72
- def to_absolute_name *args
73
- self.class.new to_absolute(*args)
73
+ def absolute_name *args
74
+ absolute(*args).to_name
74
75
  end
75
76
 
76
77
  def nth_left n
@@ -80,35 +81,47 @@ class Cardname
80
81
 
81
82
  private
82
83
 
83
- def replace_contextual_parts context
84
+ def absolutize_contextual_parts context
84
85
  parts.map do |part|
85
- new_part =
86
- case part
87
- when /^_user$/i
88
- name_proc = self.class.session
89
- name_proc ? name_proc.call : part
90
- when /^_main$/i then self.class.params[:main_name]
91
- when /^(_self|_whole|_)$/i then context.s
92
- when /^_left$/i then context.trunk
93
- # note - inconsistent use of left v. trunk
94
- when /^_right$/i then context.tag
95
- when /^_(\d+)$/i
96
- pos = $~[1].to_i
97
- pos = context.length if pos > context.length
98
- context.parts[pos - 1]
99
- when /^_(L*)(R?)$/i
100
- l_s, r_s = $~[1].size, !$~[2].empty?
101
- l_part = context.nth_left l_s
102
- r_s ? l_part.tag : l_part.s
103
- # when /^_/
104
- # custom = args[:params] ? args[:params][part] : nil
105
- # custom ? CGI.escapeHTML(custom) : part
106
- # why are we escaping HTML here?
107
- else
108
- part
109
- end.to_s.strip
110
- new_part
86
+ case part
87
+ when /^_user$/i then user_part part
88
+ when /^_main$/i then self.class.params[:main_name]
89
+ when /^(_self|_whole|_)$/i then context.s
90
+ when /^_left$/i then context.trunk
91
+ # note - inconsistent use of left v. trunk
92
+ when /^_right$/i then context.tag
93
+ when /^_(\d+)$/i then ordinal_part $~[1].to_i, context
94
+ when /^_(L*)(R?)$/i then partmap_part $~, context
95
+ else part
96
+ end.to_s.strip
97
+ end
98
+ end
99
+
100
+ def user_part part
101
+ name_proc = self.class.session
102
+ name_proc ? name_proc.call : part
103
+ end
104
+
105
+ def ordinal_part pos, context
106
+ pos = context.length if pos > context.length
107
+ context.parts[pos - 1]
108
+ end
109
+
110
+ def partmap_part match, context
111
+ l_s, r_s = match[1].size, !match[2].empty?
112
+ l_part = context.nth_left l_s
113
+ r_s ? l_part.tag : l_part.s
114
+ end
115
+
116
+ def absolutize_extremes new_parts, context
117
+ [0, -1].each do |i|
118
+ next if new_parts[i].present?
119
+ # following avoids recontextualizing with relative contexts.
120
+ # Eg, `+A+B+.absolute('+A')` should be +A+B, not +A+A+B.
121
+ next if new_parts.to_name.send "#{[ :start, :end ][i]}s_with?", context
122
+ new_parts[i] = context
111
123
  end
112
124
  end
125
+
113
126
  end
114
127
  end
@@ -7,7 +7,7 @@ class Cardname
7
7
  .gsub(/[^#{OK4KEY_RE}]+/, '_')
8
8
  .split(/_+/)
9
9
  .reject(&:empty?)
10
- .map { |key| Cardname.stable_uninflect(key) }
10
+ .map { |key| self.class.stable_key(key) }
11
11
  .join('_')
12
12
  end
13
13
 
data/lib/cardname.rb CHANGED
@@ -26,24 +26,28 @@ class Cardname < Object
26
26
  :session, :stabilize
27
27
 
28
28
  Cardname.joint = '+'
29
- Cardname.banned_array = ['/', '~', '|']
29
+ Cardname.banned_array = %w{ / }
30
30
  Cardname.var_re = /\{([^\}]*\})\}/
31
31
  Cardname.uninflect = :singularize
32
32
  Cardname.stabilize = false
33
33
 
34
34
  JOINT_RE = Regexp.escape joint
35
35
 
36
- @@name2nameobject = {} # name cache
36
+ @@cache = {}
37
37
 
38
38
  class << self
39
39
  def new obj
40
40
  return obj if obj.is_a? self.class
41
41
  str = stringify obj
42
- known_name(str) || super(str.strip)
42
+ cached_name(str) || super(str)
43
43
  end
44
44
 
45
- def known_name str
46
- @@name2nameobject[str]
45
+ def cached_name str
46
+ @@cache[str]
47
+ end
48
+
49
+ def reset_cache str=nil
50
+ str ? @@cache.delete(str) : @@cache = {}
47
51
  end
48
52
 
49
53
  def stringify obj
@@ -63,13 +67,12 @@ class Cardname < Object
63
67
  # Usually that means the name is a proper noun and not a plural.
64
68
  # You can choose between two solutions:
65
69
  # 1. don't uninflect if the uninflected key is not stable (stabilize = false)
66
- # (probably the best choice because you want Matthias not to be the same as Matthium)
67
70
  # 2. uninflect until the key is stable (stabilize = true)
68
- def stable_uninflect name
69
- key_one = name.send(Cardname.uninflect)
70
- key_two = key_one.send(Cardname.uninflect)
71
+ def stable_key name
72
+ key_one = name.send(uninflect)
73
+ key_two = key_one.send(uninflect)
71
74
  return key_one unless key_one != key_two
72
- Cardname.stabilize ? stable_uninflect(key_two) : name
75
+ stabilize ? stable_key(key_two) : name
73
76
  end
74
77
  end
75
78
 
@@ -77,13 +80,14 @@ class Cardname < Object
77
80
  # ~~~~~~~~~~~~~~~~~~~~~~ INSTANCE ~~~~~~~~~~~~~~~~~~~~~~~~~
78
81
  attr_reader :key, :s
79
82
  alias to_s s
83
+ alias to_str s
80
84
 
81
85
  def initialize str
82
86
  @s = str.to_s.strip
83
87
  @s = @s.encode('UTF-8') if RUBYENCODING
84
88
  initialize_parts
85
89
  @key = @part_keys.join(self.class.joint)
86
- @@name2nameobject[str] = self
90
+ @@cache[str] = self
87
91
  end
88
92
 
89
93
  def to_name
metadata CHANGED
@@ -1,16 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cardname
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.1'
4
+ version: '0.2'
5
5
  platform: ruby
6
6
  authors:
7
- - Gerry Gleason
8
7
  - Ethan McCutchen
9
8
  - Philipp Kühl
9
+ - Gerry Gleason
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2017-08-02 00:00:00.000000000 Z
13
+ date: 2017-09-27 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activesupport
@@ -83,7 +83,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
83
83
  version: '0'
84
84
  requirements: []
85
85
  rubyforge_project:
86
- rubygems_version: 2.6.6
86
+ rubygems_version: 2.6.13
87
87
  signing_key:
88
88
  specification_version: 4
89
89
  summary: Card names without all the cards