Class: Gyro::Parser::XCDataModel::Entity

Inherits:
Object
  • Object
show all
Defined in:
lib/gyro/parser/xcdatamodel/entity.rb

Overview

One Entity in the xcdatamodel

Constant Summary collapse

NUMBER_TYPES =
%i[integer_16 integer_32 integer_64 decimal double float].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(entity_xml) ⇒ Entity

rubocop:disable Metrics/AbcSize, Metrics/MethodLength



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/gyro/parser/xcdatamodel/entity.rb', line 25

def initialize(entity_xml)
  @name = entity_xml.attributes['name'].to_s
  @parent = entity_xml.attributes['parentEntity'].to_s
  @abstract = entity_xml.attributes['isAbstract'].to_s == 'YES'
  @clean = false
  @identity_attribute = Gyro::Parser::XCDataModel.(entity_xml, 'identityAttribute')
  @comment = Gyro::Parser::XCDataModel.(entity_xml, 'comment')
  @attributes = {}
  @relationships = {}
  load_entity(entity_xml)
end

Instance Attribute Details

#abstractObject Also known as: abstract?

Returns the value of attribute abstract.



21
22
23
# File 'lib/gyro/parser/xcdatamodel/entity.rb', line 21

def abstract
  @abstract
end

#attributesObject

Returns the value of attribute attributes.



21
22
23
# File 'lib/gyro/parser/xcdatamodel/entity.rb', line 21

def attributes
  @attributes
end

#commentObject

Returns the value of attribute comment.



21
22
23
# File 'lib/gyro/parser/xcdatamodel/entity.rb', line 21

def comment
  @comment
end

#identity_attributeObject

Returns the value of attribute identity_attribute.



21
22
23
# File 'lib/gyro/parser/xcdatamodel/entity.rb', line 21

def identity_attribute
  @identity_attribute
end

#nameObject

Returns the value of attribute name.



21
22
23
# File 'lib/gyro/parser/xcdatamodel/entity.rb', line 21

def name
  @name
end

#parentObject

Returns the value of attribute parent.



21
22
23
# File 'lib/gyro/parser/xcdatamodel/entity.rb', line 21

def parent
  @parent
end

#relationshipsObject

Returns the value of attribute relationships.



21
22
23
# File 'lib/gyro/parser/xcdatamodel/entity.rb', line 21

def relationships
  @relationships
end

Instance Method Details

#bool_attributes?Boolean

Returns:

  • (Boolean)


147
148
149
# File 'lib/gyro/parser/xcdatamodel/entity.rb', line 147

def bool_attributes?
  @attributes.any? { |_, attribute| attribute.type == :boolean }
end

#custom_transformers?Boolean

Returns:

  • (Boolean)


139
140
141
# File 'lib/gyro/parser/xcdatamodel/entity.rb', line 139

def custom_transformers?
  @attributes.any? { |_, attribute| !attribute.transformer.empty? }
end

#date_attributes?Boolean

Returns:

  • (Boolean)


158
159
160
# File 'lib/gyro/parser/xcdatamodel/entity.rb', line 158

def date_attributes?
  @attributes.any? { |_, attribute| attribute.type == :date }
end

#default_value?(attribute) ⇒ Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/gyro/parser/xcdatamodel/entity.rb', line 111

def default_value?(attribute)
  attribute.name != @identity_attribute
end

#enum_attributes?Boolean

Returns:

  • (Boolean)


127
128
129
# File 'lib/gyro/parser/xcdatamodel/entity.rb', line 127

def enum_attributes?
  @attributes.any? { |_, attribute| !attribute.enum_type.empty? }
end

#ignored_attributes?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/gyro/parser/xcdatamodel/entity.rb', line 85

def ignored_attributes?
  @attributes.any? { |_, attribute| attribute.realm_ignored? }
end

#ignored_attributes_relationships?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/gyro/parser/xcdatamodel/entity.rb', line 93

def ignored_attributes_relationships?
  ignored_attributes? || ignored_relationships?
end

#ignored_relationships?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/gyro/parser/xcdatamodel/entity.rb', line 89

def ignored_relationships?
  @relationships.any? { |_, relationship| relationship.realm_ignored? }
end

#indexed_attributes?Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/gyro/parser/xcdatamodel/entity.rb', line 115

def indexed_attributes?
  @attributes.any? { |_, attribute| attribute.indexed? }
end

#json_key_paths?Boolean

Returns:

  • (Boolean)


119
120
121
122
123
124
125
# File 'lib/gyro/parser/xcdatamodel/entity.rb', line 119

def json_key_paths?
  @attributes.any? do |_, attribute|
    !attribute.json_key_path.empty?
  end || @relationships.any? do |_, relationship|
    !relationship.inverse? && !relationship.json_key_path.empty?
  end
end

#list_attributes?(include_inverse = false) ⇒ Boolean

Returns:

  • (Boolean)


75
76
77
78
79
# File 'lib/gyro/parser/xcdatamodel/entity.rb', line 75

def list_attributes?(include_inverse = false)
  @relationships.any? do |_, relationship|
    (relationship.type == :to_many) && (include_inverse ? true : !relationship.inverse?)
  end
end

#list_relationships?Boolean

Returns:

  • (Boolean)


162
163
164
# File 'lib/gyro/parser/xcdatamodel/entity.rb', line 162

def list_relationships?
  @relationships.any? { |_, relationship| !relationship.destination.empty? }
end

#need_transformer?Boolean

Returns:

  • (Boolean)


143
144
145
# File 'lib/gyro/parser/xcdatamodel/entity.rb', line 143

def need_transformer?
  enum_attributes? || bool_attributes? || custom_transformers? || date_attributes?
end

#no_inverse_relationship?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/gyro/parser/xcdatamodel/entity.rb', line 81

def no_inverse_relationship?
  @relationships.none? { |_, relationship| relationship.inverse? }
end

#number_attributes?Boolean

Returns:

  • (Boolean)


152
153
154
155
156
# File 'lib/gyro/parser/xcdatamodel/entity.rb', line 152

def number_attributes?
  @attributes.any? do |_, attribute|
    attribute.enum_type.empty? && NUMBER_TYPES.include?(attribute.type)
  end
end

#only_inverse_relationships?Boolean

Returns:

  • (Boolean)


166
167
168
# File 'lib/gyro/parser/xcdatamodel/entity.rb', line 166

def only_inverse_relationships?
  @relationships.all? { |_, relationship| relationship.inverse? }
end

#primary_key?Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/gyro/parser/xcdatamodel/entity.rb', line 97

def primary_key?
  !@identity_attribute.empty?
end

#required?(attribute) ⇒ Boolean

Returns:

  • (Boolean)


105
106
107
108
109
# File 'lib/gyro/parser/xcdatamodel/entity.rb', line 105

def required?(attribute)
  return false if attribute.optional?
  return true unless primary_key?
  return true if primary_key? && !attribute.name.eql?(identity_attribute)
end

#required_attributes?Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/gyro/parser/xcdatamodel/entity.rb', line 101

def required_attributes?
  @attributes.any? { |_, attribute| required?(attribute) }
end

#to_hObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/gyro/parser/xcdatamodel/entity.rb', line 37

def to_h
  {
    'attributes' => attributes.values.sort_by { |a| a.name == identity_attribute ? '__' : a.name }.map(&:to_h),
    'relationships' => relationships.values.map(&:to_h),
    'name' => name,
    'parent' => parent,
    'abstract' => abstract,
    'identity_attribute' => identity_attribute,
    'comment' => comment,
    'has_no_inverse_relationship' => no_inverse_relationship?,
    'has_ignored' => ignored_attributes_relationships?, 'has_required' => required_attributes?,
    'has_primary_key' => primary_key?, 'has_indexed_attributes' => indexed_attributes?,
    'has_json_key_path' => json_key_paths?, 'has_enum_attributes' => enum_attributes?,
    'has_custom_transformers' => custom_transformers?, 'need_transformer' => need_transformer?,
    'has_bool_attributes' => bool_attributes?,
    'has_number_attributes' => number_attributes?,
    'has_date_attribute' => date_attributes?,
    'has_list_relationship' => list_relationships?,
    'has_list_attributes' => list_attributes?,
    'has_only_inverse' => only_inverse_relationships?
  }
end

#to_sObject

rubocop:enable Metrics/AbcSize, Metrics/MethodLength



61
62
63
64
65
# File 'lib/gyro/parser/xcdatamodel/entity.rb', line 61

def to_s
  "\nEntity => #{@name}\n" +
    @attributes.map { |_, attr| attr.to_s } +
    @relationships.map { |_, rel| rel.to_s }
end

#transformersObject



131
132
133
134
135
136
137
# File 'lib/gyro/parser/xcdatamodel/entity.rb', line 131

def transformers
  transformers = Set.new
  @attributes.each_value do |_, attribute|
    transformers.add attribute.transformer unless attribute.transformer.empty?
  end
  transformers
end

#used_as_list_by_other?(entities) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
70
71
72
73
# File 'lib/gyro/parser/xcdatamodel/entity.rb', line 67

def used_as_list_by_other?(entities)
  entities.any? do |_, entity|
    entity.relationships.any? do |_, relationship|
      (relationship.inverse_type == @name) && (relationship.type == :to_many)
    end
  end
end