Class: NoSE::Entity
- 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
-
#count ⇒ Object
Returns the value of attribute count.
-
#fields ⇒ Object
readonly
Returns the value of attribute fields.
-
#foreign_keys ⇒ Object
readonly
Returns the value of attribute foreign_keys.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
-
#*(other) ⇒ Entity
Shortcut for #count=.
-
#<<(field, freeze: true) ⇒ self
Adds a Fields::Field to the entity.
-
#==(other) ⇒ Boolean
(also: #eql?)
Compare by name.
-
#[](field_name) ⇒ Field
Get the field on the entity with the given name.
-
#field?(field) ⇒ Boolean
Return true if the entity contains a field with the given name.
-
#hash ⇒ Fixnum
The hash is based on the name of the entity and its fields.
-
#id_field ⇒ Fields::IDField
Get the key fields for the entity.
-
#initialize(name, &block) ⇒ Entity
constructor
A new instance of Entity.
-
#random_entity(prefix_entity = true) ⇒ Hash
Generate a hash with random values for fields in the entity.
-
#simple_index ⇒ Index
Create a simple index which maps entity keys to other fields.
-
#to_color ⇒ String
:nocov:.
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
#count ⇒ Object
Returns the value of attribute count.
8 9 10 |
# File 'lib/nose/model/entity.rb', line 8 def count @count end |
#fields ⇒ Object (readonly)
Returns the value of attribute fields.
6 7 8 |
# File 'lib/nose/model/entity.rb', line 6 def fields @fields end |
#foreign_keys ⇒ Object (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 |
#name ⇒ Object (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=
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
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
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
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
83 84 85 |
# File 'lib/nose/model/entity.rb', line 83 def field?(field) @fields.key? field end |
#hash ⇒ Fixnum
The hash is based on the name of the entity and its fields
39 40 41 |
# File 'lib/nose/model/entity.rb', line 39 def hash @hash ||= @name.hash end |
#id_field ⇒ Fields::IDField
Get the key fields for the entity
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
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_index ⇒ Index
Create a simple index which maps entity keys to other fields
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_color ⇒ String
:nocov:
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 |