class Gyro::Parser::XCDataModel::Entity

One Entity in the xcdatamodel

Constants

NUMBER_TYPES

Attributes

abstract[RW]
abstract?[RW]
attributes[RW]
comment[RW]
identity_attribute[RW]
name[RW]
parent[RW]
relationships[RW]

Public Class Methods

new(entity_xml) click to toggle source

rubocop:disable Metrics/AbcSize, Metrics/MethodLength

# 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.user_info(entity_xml, 'identityAttribute')
  @comment = Gyro::Parser::XCDataModel.user_info(entity_xml, 'comment')
  @attributes = {}
  @relationships = {}
  load_entity(entity_xml)
end

Public Instance Methods

bool_attributes?() click to toggle source
# File lib/gyro/parser/xcdatamodel/entity.rb, line 147
def bool_attributes?
  @attributes.any? { |_, attribute| attribute.type == :boolean }
end
custom_transformers?() click to toggle source
# File lib/gyro/parser/xcdatamodel/entity.rb, line 139
def custom_transformers?
  @attributes.any? { |_, attribute| !attribute.transformer.empty? }
end
date_attributes?() click to toggle source
# File lib/gyro/parser/xcdatamodel/entity.rb, line 158
def date_attributes?
  @attributes.any? { |_, attribute| attribute.type == :date }
end
default_value?(attribute) click to toggle source
# File lib/gyro/parser/xcdatamodel/entity.rb, line 111
def default_value?(attribute)
  attribute.name != @identity_attribute
end
enum_attributes?() click to toggle source
# File lib/gyro/parser/xcdatamodel/entity.rb, line 127
def enum_attributes?
  @attributes.any? { |_, attribute| !attribute.enum_type.empty? }
end
ignored_attributes?() click to toggle source
# File lib/gyro/parser/xcdatamodel/entity.rb, line 85
def ignored_attributes?
  @attributes.any? { |_, attribute| attribute.realm_ignored? }
end
ignored_attributes_relationships?() click to toggle source
# File lib/gyro/parser/xcdatamodel/entity.rb, line 93
def ignored_attributes_relationships?
  ignored_attributes? || ignored_relationships?
end
ignored_relationships?() click to toggle source
# File lib/gyro/parser/xcdatamodel/entity.rb, line 89
def ignored_relationships?
  @relationships.any? { |_, relationship| relationship.realm_ignored? }
end
indexed_attributes?() click to toggle source
# File lib/gyro/parser/xcdatamodel/entity.rb, line 115
def indexed_attributes?
  @attributes.any? { |_, attribute| attribute.indexed? }
end
json_key_paths?() click to toggle source
# 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) click to toggle source
# 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?() click to toggle source
# File lib/gyro/parser/xcdatamodel/entity.rb, line 162
def list_relationships?
  @relationships.any? { |_, relationship| !relationship.destination.empty? }
end
need_transformer?() click to toggle source
# File lib/gyro/parser/xcdatamodel/entity.rb, line 143
def need_transformer?
  enum_attributes? || bool_attributes? || custom_transformers? || date_attributes?
end
no_inverse_relationship?() click to toggle source
# File lib/gyro/parser/xcdatamodel/entity.rb, line 81
def no_inverse_relationship?
  @relationships.none? { |_, relationship| relationship.inverse? }
end
number_attributes?() click to toggle source
# 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?() click to toggle source
# File lib/gyro/parser/xcdatamodel/entity.rb, line 166
def only_inverse_relationships?
  @relationships.all? { |_, relationship| relationship.inverse? }
end
primary_key?() click to toggle source
# File lib/gyro/parser/xcdatamodel/entity.rb, line 97
def primary_key?
  !@identity_attribute.empty?
end
required?(attribute) click to toggle source
# 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?() click to toggle source
# File lib/gyro/parser/xcdatamodel/entity.rb, line 101
def required_attributes?
  @attributes.any? { |_, attribute| required?(attribute) }
end
to_h() click to toggle source
# 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_s() click to toggle source

rubocop:enable Metrics/AbcSize, Metrics/MethodLength

# 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
transformers() click to toggle source
# 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) click to toggle source
# 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