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)

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

#[](*args) ⇒ Object

def + other

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

end



119
120
121
# File 'lib/cardname/parts.rb', line 119

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

#ancestorsObject



111
112
113
# File 'lib/cardname/parts.rb', line 111

def ancestors
  @ancestors ||= pieces.reject { |p| p == self}
end

#compound?Boolean Also known as: junction?

Returns true if name has more than one part.

Returns:

  • (Boolean)

    true if name has more than one part



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

def compound?
  !simple?
end

#leftObject



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

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

#left_keyObject



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

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

#left_nameObject



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

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

#parent_keysObject



61
62
63
# File 'lib/cardname/parts.rb', line 61

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

#parent_namesObject



57
58
59
# File 'lib/cardname/parts.rb', line 57

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

#parentsObject



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

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

#part_namesObject



84
85
86
# File 'lib/cardname/parts.rb', line 84

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

#piece_namesObject



88
89
90
# File 'lib/cardname/parts.rb', line 88

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"]


96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/cardname/parts.rb', line 96

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



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

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

#right_keyObject



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

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

#right_nameObject



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

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

#tagObject



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

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

#tag_nameObject



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

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

#to_aObject

Returns the value of attribute parts.



8
9
10
# File 'lib/cardname/parts.rb', line 8

def parts
  @parts
end

#trunkObject

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



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

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

#trunk_nameObject



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

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