Class: Fable::Path::Component

Inherits:
Object
  • Object
show all
Defined in:
lib/fable/path.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Component

Returns a new instance of Component.



149
150
151
152
153
154
155
156
157
# File 'lib/fable/path.rb', line 149

def initialize(options)
  if options[:index]
    self.index = options[:index]
    self.name = nil
  elsif options[:name]
    self.name = options[:name]
    self.index = -1
  end
end

Instance Attribute Details

#indexObject

Returns the value of attribute index.



131
132
133
# File 'lib/fable/path.rb', line 131

def index
  @index
end

#nameObject

Returns the value of attribute name.



131
132
133
# File 'lib/fable/path.rb', line 131

def name
  @name
end

Class Method Details

.component_type(value) ⇒ Object



141
142
143
144
145
146
147
# File 'lib/fable/path.rb', line 141

def self.component_type(value)
  if value.is_a?(Numeric) || value.match?(/^\d+$/)
    return {index: Integer(value)}
  else
    return {name: value}
  end
end

.parent_componentObject



181
182
183
# File 'lib/fable/path.rb', line 181

def self.parent_component
  self.new(name: Path::PARENT_ID)
end

Instance Method Details

#==(other_component) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/fable/path.rb', line 167

def ==(other_component)
  return false if other_component.nil?

  if self.is_index? == other_component.is_index?
    if is_index?
      return self.index == other_component.index
    else
      return self.name == other_component.name
    end
  end

  return false
end

#is_index?Boolean

Returns:

  • (Boolean)


133
134
135
# File 'lib/fable/path.rb', line 133

def is_index?
  index >= 0
end

#is_parent?Boolean

Returns:

  • (Boolean)


137
138
139
# File 'lib/fable/path.rb', line 137

def is_parent?
  name == Path::PARENT_ID
end

#to_sObject



159
160
161
162
163
164
165
# File 'lib/fable/path.rb', line 159

def to_s
  if is_index?
    index.to_s
  else
    name
  end
end