class NoSE::Entity

Allow entities to create their own indices

A representation of an object in the conceptual data model

Attributes

count[RW]
fields[R]
foreign_keys[R]
name[R]

Public Class Methods

new(name, &block) click to toggle source
# File lib/nose/model/entity.rb, line 10
def initialize(name, &block)
  @name = name
  @fields = {}
  @foreign_keys = {}
  @count = 1

  # Precompute the hash
  hash

  # Apply the DSL
  EntityDSL.new(self).instance_eval(&block) if block_given?
end

Public Instance Methods

*(other) click to toggle source

Shortcut for {#count=} @return [Entity]

# File lib/nose/model/entity.rb, line 67
def *(other)
  fail TypeError, 'count must be an integer' unless other.is_a? Integer
  @count = other

  self
end
<<(field, freeze: true) click to toggle source

Adds a {Fields::Field} to the entity @return [self] the current entity to allow chaining

# File lib/nose/model/entity.rb, line 51
def <<(field, freeze: true)
  if field.is_a? Fields::ForeignKeyField
    @foreign_keys[field.name] = field
  else
    @fields[field.name] = field
  end

  field.instance_variable_set(:@parent, self)
  field.hash
  field.freeze if freeze

  self
end
==(other) click to toggle source

Compare by name @return [Boolean]

# File lib/nose/model/entity.rb, line 32
def ==(other)
  @name == other.instance_variable_get(:@name)
end
Also aliased as: eql?
[](field_name) click to toggle source

Get the field on the entity with the given name @return [Field]

# File lib/nose/model/entity.rb, line 76
def [](field_name)
  field = @fields[field_name] || @foreign_keys[field_name]
  fail FieldNotFound if field.nil?
  field
end
eql?(other)
Alias for: ==
field?(field) click to toggle source

Return true if the entity contains a field with the given name

# File lib/nose/model/entity.rb, line 83
def field?(field)
  @fields.key? field
end
hash() click to toggle source

The hash is based on the name of the entity and its fields @return [Integer]

# File lib/nose/model/entity.rb, line 39
def hash
  @hash ||= @name.hash
end
id_field() click to toggle source

Get the key fields for the entity @return [Fields::IDField>]

# File lib/nose/model/entity.rb, line 45
def id_field
  fields.each_value.find(&:primary_key?)
end
random_entity(prefix_entity = true) click to toggle source

Generate a hash with random values for fields in the entity @return [Hash]

# File lib/nose/model/entity.rb, line 89
def random_entity(prefix_entity = true)
  Hash[@fields.map do |name, field|
    key = name
    key = "#{@name}_#{name}" if prefix_entity
    [key, field.random_value]
  end]
end
simple_index() click to toggle source

Create a simple index which maps entity keys to other fields @return [Index]

# File lib/nose/indexes.rb, line 191
def simple_index
  Index.new [id_field], [], fields.values - [id_field],
            QueryGraph::Graph.from_path([id_field]), saved_key: name
end
to_color() click to toggle source

:nocov: @return [String]

# File lib/nose/model/entity.rb, line 25
def to_color
  "[light_blue]#{@name}[/] [#{fields.each_key.map(&:to_color).join ', '}]"
end