Class: Bronze::Entities::Attributes::Metadata

Inherits:
Object
  • Object
show all
Defined in:
lib/bronze/entities/attributes/metadata.rb

Overview

Data class that characterizes an entity attribute and allows for reflection on its properties and options.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, type, options) ⇒ Metadata

Returns a new instance of Metadata.

Parameters:

  • name (String, Symbol)

    The name of the attribute.

  • type (Class)

    The type of the attribute.

  • options (Hash)

    Additional options for the attribute.



12
13
14
15
16
17
18
# File 'lib/bronze/entities/attributes/metadata.rb', line 12

def initialize(name, type, options)
  @name        = name.intern
  @type        = type
  @options     = options
  @reader_name = name.intern
  @writer_name = "#{name}=".intern
end

Instance Attribute Details

#nameString, Symbol (readonly)

Returns the name of the attribute.

Returns:

  • (String, Symbol)

    the name of the attribute.



21
22
23
# File 'lib/bronze/entities/attributes/metadata.rb', line 21

def name
  @name
end

#optionsHash (readonly)

Returns additional options for the attribute.

Returns:

  • (Hash)

    additional options for the attribute.



24
25
26
# File 'lib/bronze/entities/attributes/metadata.rb', line 24

def options
  @options
end

#reader_nameString, Symbol (readonly)

Returns the name of the attribute’s reader method.

Returns:

  • (String, Symbol)

    the name of the attribute’s reader method.



27
28
29
# File 'lib/bronze/entities/attributes/metadata.rb', line 27

def reader_name
  @reader_name
end

#typeClass (readonly)

Returns the type of the attribute.

Returns:

  • (Class)

    the type of the attribute.



30
31
32
# File 'lib/bronze/entities/attributes/metadata.rb', line 30

def type
  @type
end

#writer_nameString, Symbol (readonly)

Returns the name of the attribute’s writer method.

Returns:

  • (String, Symbol)

    the name of the attribute’s writer method.



33
34
35
# File 'lib/bronze/entities/attributes/metadata.rb', line 33

def writer_name
  @writer_name
end

Instance Method Details

#allow_nil?Boolean

Returns true if the attribute allows nil values, otherwise false.

Returns:

  • (Boolean)

    true if the attribute allows nil values, otherwise false.



37
38
39
# File 'lib/bronze/entities/attributes/metadata.rb', line 37

def allow_nil?
  !!@options[:allow_nil]
end

#defaultObject Also known as: default_value

Returns the default value for the attribute.

Returns:

  • (Object)

    the default value for the attribute.



42
43
44
45
46
# File 'lib/bronze/entities/attributes/metadata.rb', line 42

def default
  val = @options[:default]

  val.is_a?(Proc) ? val.call : val
end

#default?Boolean

Returns true if the default value is set, otherwise false.

Returns:

  • (Boolean)

    true if the default value is set, otherwise false.



50
51
52
# File 'lib/bronze/entities/attributes/metadata.rb', line 50

def default?
  !@options[:default].nil?
end

#default_transform?Boolean

Returns true if the attribute does not have a custom transform, or if the transform is flagged as a default transform; otherwise false.

Returns:

  • (Boolean)

    true if the attribute does not have a custom transform, or if the transform is flagged as a default transform; otherwise false.



56
57
58
# File 'lib/bronze/entities/attributes/metadata.rb', line 56

def default_transform?
  !!@options[:default_transform] || !transform?
end

#foreign_key?Boolean

Returns true if the attribute is a foreign key, otherwise false.

Returns:

  • (Boolean)

    true if the attribute is a foreign key, otherwise false.



61
62
63
# File 'lib/bronze/entities/attributes/metadata.rb', line 61

def foreign_key?
  !!@options[:foreign_key]
end

#primary_key?Boolean

Returns true if the attribute is a primary key, otherwise false.

Returns:

  • (Boolean)

    true if the attribute is a primary key, otherwise false.



66
67
68
# File 'lib/bronze/entities/attributes/metadata.rb', line 66

def primary_key?
  !!@options[:primary_key]
end

#read_only?Boolean

Returns true if the attribute is read-only, otherwise false.

Returns:

  • (Boolean)

    true if the attribute is read-only, otherwise false.



71
72
73
# File 'lib/bronze/entities/attributes/metadata.rb', line 71

def read_only?
  !!@options[:read_only]
end

#transformBronze::Transform

Returns the transform used to normalize and denormalize the attribute.

Returns:

  • (Bronze::Transform)

    the transform used to normalize and denormalize the attribute.



77
78
79
# File 'lib/bronze/entities/attributes/metadata.rb', line 77

def transform
  @options[:transform]
end

#transform?Boolean

Returns true if the attribute has a custom transform, otherwise false.

Returns:

  • (Boolean)

    true if the attribute has a custom transform, otherwise false.



83
84
85
# File 'lib/bronze/entities/attributes/metadata.rb', line 83

def transform?
  !!@options[:transform]
end