Module: Cardname::Parts

Included in:
Cardname
Defined in:
lib/cardname/parts.rb

Overview

naming conventions: methods that end with _name return name objects the same methods without _name return strings

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#part_keysObject (readonly)

Returns the value of attribute part_keys.



6
7
8
# File 'lib/cardname/parts.rb', line 6

def part_keys
  @part_keys
end

#partsObject (readonly) Also known as: to_a

Returns the value of attribute parts.



6
7
8
# File 'lib/cardname/parts.rb', line 6

def parts
  @parts
end

#simpleObject (readonly) Also known as: simple?

Returns the value of attribute simple.



6
7
8
# File 'lib/cardname/parts.rb', line 6

def simple
  @simple
end

Instance Method Details

#+(other) ⇒ Object



102
103
104
# File 'lib/cardname/parts.rb', line 102

def + other
  self.class.new(parts + other.to_name.parts)
end

#[](*args) ⇒ Object



106
107
108
# File 'lib/cardname/parts.rb', line 106

def [] *args
  self.class.new parts[*args]
end

#initialize_partsObject



11
12
13
14
15
16
17
18
# File 'lib/cardname/parts.rb', line 11

def initialize_parts
  # -1 = don't suppress trailing null fields
  @parts = @s.split(/\s*#{JOINT_RE}\s*/, -1)
  @simple = @parts.size <= 1
  # simple check needed to avoid inifinite recursion
  @part_keys =
    @simple ? [simple_key] : @parts.map { |p| p.to_name.simple_key }
end

#leftObject



20
21
22
# File 'lib/cardname/parts.rb', line 20

def left
  @left ||= simple? ? nil : parts[0..-2] * self.class.joint
end

#left_keyObject



36
37
38
# File 'lib/cardname/parts.rb', line 36

def left_key
  @left_key ||=  simple? ? nil : part_keys[0..-2] * self.class.joint
end

#left_nameObject



28
29
30
# File 'lib/cardname/parts.rb', line 28

def left_name
  @left_name ||= left && self.class.new(left)
end

#parent_keysObject



52
53
54
# File 'lib/cardname/parts.rb', line 52

def parent_keys
  @parent_keys ||= junction? ? [left_key, right_key] : []
end

#parent_namesObject



48
49
50
# File 'lib/cardname/parts.rb', line 48

def parent_names
  @parent_names ||= junction? ? [left_name, right_name] : []
end

#parentsObject



44
45
46
# File 'lib/cardname/parts.rb', line 44

def parents
  @parents ||= junction? ? [left, right] : []
end

#part_namesObject



75
76
77
# File 'lib/cardname/parts.rb', line 75

def part_names
  @part_names ||= parts.map(&:to_name)
end

#piece_namesObject



79
80
81
# File 'lib/cardname/parts.rb', line 79

def piece_names
  @piece_names ||= pieces.map(&:to_name)
end

#piecesObject

self and all ancestors (= parts and recursive lefts)

Examples:

"A+B+C+D".to_name.pieces
# => ["A", "B", "C", "D", "A+B", "A+B+C", "A+B+C+D"]


87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/cardname/parts.rb', line 87

def pieces
  @pieces ||=
    if simple?
      [self]
    else
      junction_pieces = []
      parts[1..-1].inject parts[0] do |left, right|
        piece = [left, right] * self.class.joint
        junction_pieces << piece
        piece
      end
      parts + junction_pieces
    end
end

#rightObject



24
25
26
# File 'lib/cardname/parts.rb', line 24

def right
  @right ||= simple? ? nil : parts[-1]
end

#right_keyObject



40
41
42
# File 'lib/cardname/parts.rb', line 40

def right_key
  @right_key ||= simple? ? nil : part_keys.last
end

#right_nameObject



32
33
34
# File 'lib/cardname/parts.rb', line 32

def right_name
  @right_name ||= right && self.class.new(right)
end

#tagObject



63
64
65
# File 'lib/cardname/parts.rb', line 63

def tag
  @tag ||= simple? ? s : right
end

#tag_nameObject



71
72
73
# File 'lib/cardname/parts.rb', line 71

def tag_name
  @tag_name ||= simple? ? self : right_name
end

#trunkObject

Note that all names have a trunk and tag, but only junctions have left and right



59
60
61
# File 'lib/cardname/parts.rb', line 59

def trunk
  @trunk ||= simple? ? s : left
end

#trunk_nameObject



67
68
69
# File 'lib/cardname/parts.rb', line 67

def trunk_name
  @trunk_name ||= simple? ? self : left_name
end