Module: SmartName::Parts

Included in:
SmartName
Defined in:
lib/smart_name/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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

name parts can be accessed and manipulated like an array



112
113
114
115
116
117
118
# File 'lib/smart_name/parts.rb', line 112

def method_missing method, *args, &block
  if parts.respond_to?(method)
    self.class.new parts.send(method, *args, &block)
  else
    super
  end
end

Instance Attribute Details

#part_keysObject (readonly)

Returns the value of attribute part_keys.



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

def part_keys
  @part_keys
end

#partsObject (readonly) Also known as: to_a, to_ary

Returns the value of attribute parts.



6
7
8
# File 'lib/smart_name/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/smart_name/parts.rb', line 6

def simple
  @simple
end

Instance Method Details

#initialize_partsObject



20
21
22
23
24
25
26
27
# File 'lib/smart_name/parts.rb', line 20

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



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

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

#left_keyObject



45
46
47
# File 'lib/smart_name/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/smart_name/parts.rb', line 37

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

#parent_keysObject



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

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

#parent_namesObject



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

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

#parentsObject



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

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

#part_namesObject



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

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

#piece_namesObject



88
89
90
# File 'lib/smart_name/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/smart_name/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

#respond_to?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


120
121
122
# File 'lib/smart_name/parts.rb', line 120

def respond_to? method, include_private=false
  super || parts.respond_to?(method, include_private)
end

#rightObject



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

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

#right_keyObject



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

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

#right_nameObject



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

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

#tagObject



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

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

#tag_nameObject



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

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



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

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

#trunk_nameObject



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

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