class Stannum::Attribute

Data object representing an attribute on a struct.

Attributes

name[R]

@return [String] the name of the attribute.

options[R]

@return [Hash] the attribute options.

type[R]

@return [String] the name of the attribute type Class or Module.

Public Class Methods

new(name:, options:, type:) click to toggle source

@param name [String, Symbol] The name of the attribute. Converted to a

String.

@param options [Hash, nil] Options for the attribute. Converted to a Hash

with Symbol keys. Defaults to an empty Hash.

@param type [Class, Module, String] The type of the attribute. Can be a

Class, a Module, or the name of a class or module.
# File lib/stannum/attribute.rb, line 17
def initialize(name:, options:, type:)
  validate_name(name)
  validate_options(options)
  validate_type(type)

  @name    = name.to_s
  @options = tools.hash_tools.convert_keys_to_symbols(options || {})
  @options = resolve_required_option(**@options)

  @type, @resolved_type = resolve_type(type)
end

Public Instance Methods

default() click to toggle source

@return [Object] the default value for the attribute, if any.

# File lib/stannum/attribute.rb, line 39
def default
  @options[:default]
end
default?() click to toggle source

@return [Boolean] true if the attribute has a default value; otherwise

false.
# File lib/stannum/attribute.rb, line 45
def default?
  !@options[:default].nil?
end
reader_name() click to toggle source

@return [Symbol] the name of the reader method for the attribute.

# File lib/stannum/attribute.rb, line 50
def reader_name
  @reader_name ||= name.intern
end
resolved_type() click to toggle source

@return [Module] the type of the attribute.

# File lib/stannum/attribute.rb, line 55
def resolved_type
  return @resolved_type if @resolved_type

  @resolved_type = Object.const_get(type)

  unless @resolved_type.is_a?(Module)
    raise NameError, "constant #{type} is not a Class or Module"
  end

  @resolved_type
end
writer_name() click to toggle source

@return [Symbol] the name of the writer method for the attribute.

# File lib/stannum/attribute.rb, line 68
def writer_name
  @writer_name ||= :"#{name}="
end

Private Instance Methods

resolve_type(type) click to toggle source
# File lib/stannum/attribute.rb, line 74
def resolve_type(type)
  return [type, nil] if type.is_a?(String)

  [type.to_s, type]
end
tools() click to toggle source
# File lib/stannum/attribute.rb, line 80
def tools
  SleepingKingStudios::Tools::Toolbelt.instance
end
validate_name(name) click to toggle source
# File lib/stannum/attribute.rb, line 84
def validate_name(name)
  raise ArgumentError, "name can't be blank" if name.nil?

  unless name.is_a?(String) || name.is_a?(Symbol)
    raise ArgumentError, 'name must be a String or Symbol'
  end

  raise ArgumentError, "name can't be blank" if name.empty?
end
validate_options(options) click to toggle source
# File lib/stannum/attribute.rb, line 94
def validate_options(options)
  return if options.nil? || options.is_a?(Hash)

  raise ArgumentError, 'options must be a Hash or nil'
end
validate_type(type) click to toggle source
# File lib/stannum/attribute.rb, line 100
def validate_type(type)
  raise ArgumentError, "type can't be blank" if type.nil?

  return if type.is_a?(Module)

  if type.is_a?(String)
    return unless type.empty?

    raise ArgumentError, "type can't be blank"
  end

  raise ArgumentError,
    'type must be a Class, a Module, or the name of a class or module'
end