Class: Lutaml::XMI::GeneralizationDrop

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

Instance Method Summary collapse

Methods included from Parsers::XMIBase

included, #set_xmi_model

Constructor Details

#initialize(gen, guidance = nil, options = {}) ⇒ GeneralizationDrop

rubocop:disable Lint/MissingSuper



8
9
10
11
12
13
14
15
16
17
# File 'lib/lutaml/xmi/liquid_drops/generalization_drop.rb', line 8

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

Instance Method Details

#assoc_props(sort: false) ⇒ Object

get attributes with association



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

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

rubocop:disable Metrics/MethodLength



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/lutaml/xmi/liquid_drops/generalization_drop.rb', line 41

def attributes # rubocop:disable Metrics/MethodLength
  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



61
62
63
# File 'lib/lutaml/xmi/liquid_drops/generalization_drop.rb', line 61

def definition
  @gen[:definition]
end

#generalObject



31
32
33
34
35
# File 'lib/lutaml/xmi/liquid_drops/generalization_drop.rb', line 31

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

#has_general?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/lutaml/xmi/liquid_drops/generalization_drop.rb', line 37

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

#idObject



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

def id
  @gen[:general_id]
end

#inherited_assoc_props(sort: false) ⇒ Object

get items with association by looping through the generation



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

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



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

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



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/lutaml/xmi/liquid_drops/generalization_drop.rb', line 151

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



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

def name
  @gen[:general_name]
end

#owned_props(sort: false) ⇒ Object

get attributes without association



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

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



118
119
120
121
122
123
124
125
126
127
# File 'lib/lutaml/xmi/liquid_drops/generalization_drop.rb', line 118

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



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

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

#sort_props(arr) ⇒ Object



145
146
147
148
149
# File 'lib/lutaml/xmi/liquid_drops/generalization_drop.rb', line 145

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



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

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



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

def sorted_assoc_props
  assoc_props(sort: true)
end

#sorted_inherited_assoc_propsObject



141
142
143
# File 'lib/lutaml/xmi/liquid_drops/generalization_drop.rb', line 141

def sorted_inherited_assoc_props
  inherited_assoc_props(sort: true)
end

#sorted_inherited_propsObject



137
138
139
# File 'lib/lutaml/xmi/liquid_drops/generalization_drop.rb', line 137

def sorted_inherited_props
  inherited_props(sort: true)
end

#sorted_owned_propsObject



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

def sorted_owned_props
  owned_props(sort: true)
end

#stereotypeObject



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

def stereotype
  @gen[:stereotype]
end

#typeObject



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

def type
  @gen[:type]
end

#upper_klassObject



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

def upper_klass
  @gen[:general_upper_klass]
end