Class: Opencdd::ClassTree

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/opencdd/class_tree.rb

Constant Summary collapse

DEFAULT_FIELDS =
%i[code name].freeze
AVAILABLE_FIELDS =
%i[code name irdi definition class_type].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(database, fields: DEFAULT_FIELDS) ⇒ ClassTree

Returns a new instance of ClassTree.



14
15
16
17
# File 'lib/opencdd/class_tree.rb', line 14

def initialize(database, fields: DEFAULT_FIELDS)
  @database = database
  @fields = fields
end

Instance Attribute Details

#databaseObject (readonly)

Returns the value of attribute database.



12
13
14
# File 'lib/opencdd/class_tree.rb', line 12

def database
  @database
end

#fieldsObject (readonly)

Returns the value of attribute fields.



12
13
14
# File 'lib/opencdd/class_tree.rb', line 12

def fields
  @fields
end

Instance Method Details

#eachObject



23
24
25
26
# File 'lib/opencdd/class_tree.rb', line 23

def each
  return enum_for(:each) unless block_given?
  walk(roots, 0) { |k, _d| yield k }
end

#each_nodeObject



28
29
30
31
# File 'lib/opencdd/class_tree.rb', line 28

def each_node
  return enum_for(:each_node) unless block_given?
  walk(roots, 0) { |k, d| yield k, d }
end

#rootsObject



19
20
21
# File 'lib/opencdd/class_tree.rb', line 19

def roots
  @database.root_classes
end

#subtree(klass, max_depth: nil, fields: @fields) ⇒ Object



41
42
43
# File 'lib/opencdd/class_tree.rb', line 41

def subtree(klass, max_depth: nil, fields: @fields)
  node_h(klass, depth: 0, max_depth: max_depth, fields: fields)
end

#subtree_yaml(klass, **opts) ⇒ Object



45
46
47
# File 'lib/opencdd/class_tree.rb', line 45

def subtree_yaml(klass, **opts)
  subtree(klass, **opts).to_yaml
end

#to_h(max_depth: nil, fields: @fields) ⇒ Object



33
34
35
# File 'lib/opencdd/class_tree.rb', line 33

def to_h(max_depth: nil, fields: @fields)
  roots.map { |k| node_h(k, depth: 0, max_depth: max_depth, fields: fields) }
end

#to_yaml(max_depth: nil, fields: @fields) ⇒ Object



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

def to_yaml(max_depth: nil, fields: @fields)
  to_h(max_depth: max_depth, fields: fields).to_yaml
end