Class: LCClasses::LCClass

Inherits:
Array
  • Object
show all
Defined in:
lib/lcclasses.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.flatten(hash) ⇒ Object

Convert nested LCClasses::CLASS_HASH to flat array.



19
20
21
22
23
24
25
26
# File 'lib/lcclasses.rb', line 19

def self.flatten(hash)
  return self[] if hash.nil?

  hash.sort { |a, b| a[0] <=> b[0] }.inject(self[]) do |result, klass|
    result << self[klass[0], klass[1][:name]]
    result + self.flatten(klass[1][:subclasses])
  end
end

.nest(hash) ⇒ Object

Convert nested LCClasses::CLASS_HASH to nested array.



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/lcclasses.rb', line 6

def self.nest(hash)
  return self[] if hash.nil?

  hash.sort { |a, b| a[0] <=> b[0] }.inject(self[]) do |result, klass|
    result << if klass[1][:subclasses]
                self[klass[0], klass[1][:name], self.nest(klass[1][:subclasses])]
              else
                self[klass[0], klass[1][:name]]
              end
  end
end

Instance Method Details

#codeObject

The class code.



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

def code
  self[0]
end

#nameObject

THe class name.



42
43
44
# File 'lib/lcclasses.rb', line 42

def name
  self[1]
end

#subclassesObject

Return the subclasses of this class, if any.



29
30
31
32
33
34
# File 'lib/lcclasses.rb', line 29

def subclasses
  class_data = LCClasses::CLASS_HASH[self[0]]
  return if class_data.nil? || class_data.empty?

  LCClasses::LCClass.nest(class_data[:subclasses])
end