Class: NoSE::Fields::Field

Inherits:
Object show all
Includes:
Supertype
Defined in:
lib/nose/model/fields.rb

Overview

A single field on an Entity

Constant Summary collapse

TYPE =

The Ruby type of values stored in this field

nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Supertype

included

Constructor Details

#initialize(name, size, count: nil) ⇒ Field

Returns a new instance of Field.



22
23
24
25
26
27
# File 'lib/nose/model/fields.rb', line 22

def initialize(name, size, count: nil)
  @name = name
  @size = size
  @cardinality = count
  @primary_key = false
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



15
16
17
# File 'lib/nose/model/fields.rb', line 15

def name
  @name
end

#parentObject (readonly)

Returns the value of attribute parent.



15
16
17
# File 'lib/nose/model/fields.rb', line 15

def parent
  @parent
end

#primary_keyObject Also known as: primary_key?

Returns the value of attribute primary_key.



16
17
18
# File 'lib/nose/model/fields.rb', line 16

def primary_key
  @primary_key
end

#sizeObject (readonly)

Returns the value of attribute size.



15
16
17
# File 'lib/nose/model/fields.rb', line 15

def size
  @size
end

Class Method Details

.value_from_string(_string) ⇒ Object

This method is abstract.

Subclasses should produce a typed value from a string

:nocov:



74
75
76
# File 'lib/nose/model/fields.rb', line 74

def self.value_from_string(_string)
  fail NotImplementedError
end

Instance Method Details

#*(other) ⇒ Field

Set the estimated cardinality of the field

Returns:



61
62
63
64
# File 'lib/nose/model/fields.rb', line 61

def *(other)
  @cardinality = other
  self
end

#==(other) ⇒ Object Also known as: eql?

Compare by parent entity and name



30
31
32
33
# File 'lib/nose/model/fields.rb', line 30

def ==(other)
  other.is_a?(Field) && @parent == other.parent &&
    @name == other.name
end

#cardinalityObject

Return the previously set cardinality, falling back to the number of entities for the field if set, or just 1



68
69
70
# File 'lib/nose/model/fields.rb', line 68

def cardinality
  @cardinality || @parent.count || 1
end

#hashFixnum

Hash by entity and name

Returns:

  • (Fixnum)


38
39
40
# File 'lib/nose/model/fields.rb', line 38

def hash
  @hash ||= id.hash
end

#idObject

A simple string representing the field



55
56
57
# File 'lib/nose/model/fields.rb', line 55

def id
  @id ||= "#{@parent.name}_#{@name}"
end

#random_valueObject

This method is abstract.

Subclasses should produce a random value of the correct type

:nocov:



81
82
83
# File 'lib/nose/model/fields.rb', line 81

def random_value
  fail NotImplementedError
end

#to_colorObject

:nocov:



43
44
45
# File 'lib/nose/model/fields.rb', line 43

def to_color
  "[blue]#{@parent.name}[/].[blue]#{@name}[/]"
end

#to_sObject

:nocov:



49
50
51
# File 'lib/nose/model/fields.rb', line 49

def to_s
  "#{@parent.name}.#{@name}"
end