class NoSE::Fields::Field

A single field on an {Entity}

Constants

TYPE

The Ruby type of values stored in this field

Attributes

name[R]
parent[R]
primary_key[RW]
primary_key?[RW]
size[R]

Public Class Methods

new(name, size, count: nil) click to toggle source
# File lib/nose/model/fields.rb, line 22
def initialize(name, size, count: nil)
  @name = name
  @size = size
  @cardinality = count
  @primary_key = false
end
value_from_string(_string) click to toggle source

@abstract Subclasses should produce a typed value from a string :nocov:

# File lib/nose/model/fields.rb, line 74
def self.value_from_string(_string)
  fail NotImplementedError
end

Private Class Methods

add_field_method(child_class) click to toggle source

Add convenience methods for all field types for an entity DSL

# File lib/nose/model/fields.rb, line 102
def self.add_field_method(child_class)
  method_regex = /^NoSE::Fields::(.*?)(Field)?$/
  method_name = child_class.name.sub(method_regex, '\1')
  EntityDSL.send :define_method, method_name,
                 (proc do |*args|
                   entity = send :instance_variable_get, :@entity

                   # XXX This is necessary since passing a hash of
                   #     keyword arguments as the last argument is
                   #     now deprecated
                   if args.last.is_a? Hash
                     hash_args = args.last
                     args.pop
                   else
                     hash_args = {}
                   end

                   field = child_class.new(*args, **hash_args)
                   entity.send :<<, field
                 end)
end
inherited(child_class) click to toggle source

Populate a helper DSL object with all subclasses of Field

# File lib/nose/model/fields.rb, line 87
def self.inherited(child_class)
  # We use separate methods for foreign keys
  begin
    fk_class = Fields.const_get('ForeignKeyField')
  rescue NameError
    fk_class = nil
  end
  return if !fk_class.nil? && child_class <= fk_class

  add_field_method(child_class)
  child_class.send(:include, Subtype)
end

Public Instance Methods

*(other) click to toggle source

Set the estimated cardinality of the field @return [Field]

# File lib/nose/model/fields.rb, line 61
def *(other)
  @cardinality = other
  self
end
==(other) click to toggle source

Compare by parent entity and name

# File lib/nose/model/fields.rb, line 30
def ==(other)
  other.is_a?(Field) && @parent == other.parent &&
    @name == other.name
end
Also aliased as: eql?
cardinality() click to toggle source

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

# File lib/nose/model/fields.rb, line 68
def cardinality
  @cardinality || @parent.count || 1
end
eql?(other)
Alias for: ==
hash() click to toggle source

Hash by entity and name @return [Integer]

# File lib/nose/model/fields.rb, line 38
def hash
  @hash ||= id.hash
end
id() click to toggle source

A simple string representing the field

# File lib/nose/model/fields.rb, line 55
def id
  @id ||= "#{@parent.name}_#{@name}"
end
random_value() click to toggle source

@abstract Subclasses should produce a random value of the correct type :nocov:

# File lib/nose/model/fields.rb, line 81
def random_value
  fail NotImplementedError
end
to_color() click to toggle source

:nocov:

# File lib/nose/model/fields.rb, line 43
def to_color
  "[blue]#{@parent.name}[/].[blue]#{@name}[/]"
end
to_s() click to toggle source

:nocov:

# File lib/nose/model/fields.rb, line 49
def to_s
  "#{@parent.name}.#{@name}"
end