Class: Lutaml::XMI::GeneralizationDrop

Inherits:
Liquid::Drop
  • Object
show all
Defined in:
lib/lutaml/xmi/liquid_drops/generalization_drop.rb

Instance Method Summary collapse

Constructor Details

#initialize(gen, guidance = nil) ⇒ GeneralizationDrop

rubocop:disable Lint/MissingSuper



6
7
8
9
10
11
12
# File 'lib/lutaml/xmi/liquid_drops/generalization_drop.rb', line 6

def initialize(gen, guidance = nil) # rubocop:disable Lint/MissingSuper
  @gen = gen
  @looped_general_item = false
  @inherited_props = []
  @inherited_assoc_props = []
  @guidance = guidance
end

Instance Method Details

#assoc_props(sort: false) ⇒ Object

get attributes with association



72
73
74
75
76
77
78
# File 'lib/lutaml/xmi/liquid_drops/generalization_drop.rb', line 72

def assoc_props(sort: false)
  return [] unless attributes

  props = attributes.select { |attr| attr[:association].nil? == false }
  props = sort_props(props) if sort
  props_to_liquid(props)
end

#attributesObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/lutaml/xmi/liquid_drops/generalization_drop.rb', line 34

def attributes
  attrs = @gen[:general_attributes]
  attrs.each do |i|
    name_ns = case i[:type_ns]
              when "core", "gml"
                upper_klass
              else
                i[:type_ns]
              end
    name_ns = upper_klass if name_ns.nil?

    i[:name_ns] = name_ns
    i[:name] = "" if i[:name].nil?
  end
end

#definitionObject



54
55
56
# File 'lib/lutaml/xmi/liquid_drops/generalization_drop.rb', line 54

def definition
  @gen[:definition]
end

#generalObject



26
27
28
# File 'lib/lutaml/xmi/liquid_drops/generalization_drop.rb', line 26

def general
  GeneralizationDrop.new(@gen[:general], @guidance) if @gen[:general]
end

#has_general?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/lutaml/xmi/liquid_drops/generalization_drop.rb', line 30

def has_general?
  !!@gen[:general]
end

#idObject



14
15
16
# File 'lib/lutaml/xmi/liquid_drops/generalization_drop.rb', line 14

def id
  @gen[:general_id]
end

#inherited_assoc_props(sort: false) ⇒ Object

get items with association by looping through the generation



96
97
98
99
100
101
102
# File 'lib/lutaml/xmi/liquid_drops/generalization_drop.rb', line 96

def inherited_assoc_props(sort: false)
  loop_general_item unless @looped_general_item

  props = @inherited_assoc_props.reverse
  props = sort_props_with_level(props) if sort
  props_hash_to_liquid(props)
end

#inherited_props(sort: false) ⇒ Object

get items without association by looping through the generation



87
88
89
90
91
92
93
# File 'lib/lutaml/xmi/liquid_drops/generalization_drop.rb', line 87

def inherited_props(sort: false)
  loop_general_item unless @looped_general_item

  props = @inherited_props.reverse
  props = sort_props_with_level(props) if sort
  props_hash_to_liquid(props)
end

#loop_general_itemObject

rubocop:disable Metrics/MethodLength,Metrics/AbcSize



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/lutaml/xmi/liquid_drops/generalization_drop.rb', line 144

def loop_general_item # rubocop:disable Metrics/MethodLength,Metrics/AbcSize
  general_item = general
  level = 0

  while general_item.has_general?
    gen_upper_klass = general_item.upper_klass
    gen_name = general_item.name
    # reverse the order to show super class first
    general_item.attributes.reverse_each do |attr|
      attr_hash = {
        attr: attr,
        gen_upper_klass: gen_upper_klass,
        gen_name: gen_name,
        guidance: @guidance,
      }
      attr_hash[:level] = level

      if attr[:association]
        @inherited_assoc_props << attr_hash
      else
        @inherited_props << attr_hash
      end
    end

    level += 1
    general_item = general_item.general
  end

  @looped_general_item = true
end

#nameObject



18
19
20
# File 'lib/lutaml/xmi/liquid_drops/generalization_drop.rb', line 18

def name
  @gen[:general_name]
end

#owned_props(sort: false) ⇒ Object

get attributes without association



63
64
65
66
67
68
69
# File 'lib/lutaml/xmi/liquid_drops/generalization_drop.rb', line 63

def owned_props(sort: false)
  return [] unless attributes

  props = attributes.select { |attr| attr[:association].nil? }
  props = sort_props(props) if sort
  props_to_liquid(props)
end

#props_hash_to_liquid(prop_hash_arr) ⇒ Object



111
112
113
114
115
116
117
118
119
120
# File 'lib/lutaml/xmi/liquid_drops/generalization_drop.rb', line 111

def props_hash_to_liquid(prop_hash_arr)
  prop_hash_arr.map do |prop_hash|
    GeneralizationAttributeDrop.new(
      prop_hash[:attr],
      prop_hash[:gen_upper_klass],
      prop_hash[:gen_name],
      prop_hash[:guidance],
    )
  end
end

#props_to_liquid(props) ⇒ Object



80
81
82
83
84
# File 'lib/lutaml/xmi/liquid_drops/generalization_drop.rb', line 80

def props_to_liquid(props)
  props.map do |attr|
    GeneralizationAttributeDrop.new(attr, upper_klass, name, @guidance)
  end
end

#sort_props(arr) ⇒ Object



138
139
140
141
142
# File 'lib/lutaml/xmi/liquid_drops/generalization_drop.rb', line 138

def sort_props(arr)
  return [] if arr.nil? || arr.empty?

  arr.sort_by { |i| [i[:name_ns], i[:name]] }
end

#sort_props_with_level(arr) ⇒ Object



104
105
106
107
108
109
# File 'lib/lutaml/xmi/liquid_drops/generalization_drop.rb', line 104

def sort_props_with_level(arr)
  return [] if arr.nil? || arr.empty?

  # level desc, name_ns asc, name asc
  arr.sort_by { |i| [-i[:level], i[:attr][:name_ns], i[:attr][:name]] }
end

#sorted_assoc_propsObject



126
127
128
# File 'lib/lutaml/xmi/liquid_drops/generalization_drop.rb', line 126

def sorted_assoc_props
  assoc_props(sort: true)
end

#sorted_inherited_assoc_propsObject



134
135
136
# File 'lib/lutaml/xmi/liquid_drops/generalization_drop.rb', line 134

def sorted_inherited_assoc_props
  inherited_assoc_props(sort: true)
end

#sorted_inherited_propsObject



130
131
132
# File 'lib/lutaml/xmi/liquid_drops/generalization_drop.rb', line 130

def sorted_inherited_props
  inherited_props(sort: true)
end

#sorted_owned_propsObject



122
123
124
# File 'lib/lutaml/xmi/liquid_drops/generalization_drop.rb', line 122

def sorted_owned_props
  owned_props(sort: true)
end

#stereotypeObject



58
59
60
# File 'lib/lutaml/xmi/liquid_drops/generalization_drop.rb', line 58

def stereotype
  @gen[:stereotype]
end

#typeObject



50
51
52
# File 'lib/lutaml/xmi/liquid_drops/generalization_drop.rb', line 50

def type
  @gen[:type]
end

#upper_klassObject



22
23
24
# File 'lib/lutaml/xmi/liquid_drops/generalization_drop.rb', line 22

def upper_klass
  @gen[:general_upper_klass]
end