Class: NoSE::Entity

Inherits:
Object show all
Defined in:
lib/nose/indexes.rb,
lib/nose/model/entity.rb

Overview

A representation of an object in the conceptual data model

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, &block) ⇒ Entity

Returns a new instance of Entity.



10
11
12
13
14
15
16
17
18
19
20
21
# 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

Instance Attribute Details

#countObject

Returns the value of attribute count.



8
9
10
# File 'lib/nose/model/entity.rb', line 8

def count
  @count
end

#fieldsObject (readonly)

Returns the value of attribute fields.



6
7
8
# File 'lib/nose/model/entity.rb', line 6

def fields
  @fields
end

#foreign_keysObject (readonly)

Returns the value of attribute foreign_keys.



7
8
9
# File 'lib/nose/model/entity.rb', line 7

def foreign_keys
  @foreign_keys
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/nose/model/entity.rb', line 7

def name
  @name
end

Instance Method Details

#*(other) ⇒ Entity

Shortcut for #count=

Returns:



67
68
69
70
71
72
# 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) ⇒ self

Adds a Fields::Field to the entity

Returns:

  • (self)

    the current entity to allow chaining



51
52
53
54
55
56
57
58
59
60
61
62
63
# 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) ⇒ Boolean Also known as: eql?

Compare by name

Returns:

  • (Boolean)


32
33
34
# File 'lib/nose/model/entity.rb', line 32

def ==(other)
  @name == other.instance_variable_get(:@name)
end

#[](field_name) ⇒ Field

Get the field on the entity with the given name

Returns:

  • (Field)


76
77
78
79
80
# 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

#field?(field) ⇒ Boolean

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

Returns:

  • (Boolean)


83
84
85
# File 'lib/nose/model/entity.rb', line 83

def field?(field)
  @fields.key? field
end

#hashFixnum

The hash is based on the name of the entity and its fields

Returns:

  • (Fixnum)


39
40
41
# File 'lib/nose/model/entity.rb', line 39

def hash
  @hash ||= @name.hash
end

#id_fieldFields::IDField

Get the key fields for the entity

Returns:



45
46
47
# File 'lib/nose/model/entity.rb', line 45

def id_field
  fields.each_value.find(&:primary_key?)
end

#random_entity(prefix_entity = true) ⇒ Hash

Generate a hash with random values for fields in the entity

Returns:

  • (Hash)


89
90
91
92
93
94
95
# 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_indexIndex

Create a simple index which maps entity keys to other fields

Returns:



191
192
193
194
# 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_colorString

:nocov:

Returns:

  • (String)


25
26
27
# File 'lib/nose/model/entity.rb', line 25

def to_color
  "[light_blue]#{@name}[/] [#{fields.each_key.map(&:to_color).join ', '}]"
end