Module: Familia::DataType::ClassMethods

Included in:
Familia::DataType
Defined in:
lib/familia/data_type/class_methods.rb

Overview

ClassMethods - Class-level DSL methods for defining DataType behavior

This module is extended into classes that inherit from Familia::DataType, providing class methods for type registration, configuration, and inheritance.

Key features:

  • Type registration system for creating DataType subclasses
  • Database and connection configuration
  • Inheritance hooks for propagating settings
  • Option validation and filtering

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#logical_database(val = nil) ⇒ Object



38
39
40
41
# File 'lib/familia/data_type/class_methods.rb', line 38

def logical_database(val = nil)
  @logical_database = val unless val.nil?
  @logical_database || parent&.logical_database
end

#parentObject

Returns the value of attribute parent.



19
20
21
# File 'lib/familia/data_type/class_methods.rb', line 19

def parent
  @parent
end

#prefixObject

Returns the value of attribute prefix.



19
20
21
# File 'lib/familia/data_type/class_methods.rb', line 19

def prefix
  @prefix
end

#suffixObject

Returns the value of attribute suffix.



19
20
21
# File 'lib/familia/data_type/class_methods.rb', line 19

def suffix
  @suffix
end

#uri(val = nil) ⇒ Object

Returns the value of attribute uri.



19
20
21
# File 'lib/familia/data_type/class_methods.rb', line 19

def uri
  @uri
end

Instance Method Details

#inherited(obj) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/familia/data_type/class_methods.rb', line 48

def inherited(obj)
  Familia.trace :DATATYPE, nil, "#{obj} is my kinda type" if Familia.debug?
  obj.logical_database = logical_database
  obj.default_expiration = default_expiration # method added via Features::Expiration
  obj.uri = uri
  super
end

#register(klass, methname) ⇒ Object

To be called inside every class that inherits DataType +methname+ is the term used for the class and instance methods that are created for the given +klass+ (e.g. set, list, etc)



25
26
27
28
29
# File 'lib/familia/data_type/class_methods.rb', line 25

def register(klass, methname)
  Familia.trace :REGISTER, nil, "[#{self}] Registering #{klass} as #{methname.inspect}" if Familia.debug?

  @registered_types[methname] = klass
end

#registered_type(methname) ⇒ Object

Get the registered type class from a given method name +methname+ is the method name used to register the class (e.g. :set, :list, etc) Returns the registered class or nil if not found



34
35
36
# File 'lib/familia/data_type/class_methods.rb', line 34

def registered_type(methname)
  @registered_types[methname]
end

#relations?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/familia/data_type/class_methods.rb', line 93

def relations?
  @has_related_fields ||= false
end

#valid_keys_only(opts) ⇒ Object



56
57
58
# File 'lib/familia/data_type/class_methods.rb', line 56

def valid_keys_only(opts)
  opts.slice(*DataType.valid_options)
end

#validate_dirty_write_warnings!(mode) ⇒ Object

Parameters:

  • mode (Symbol, nil)

    a +dirty_write_warnings:+ option value

Raises:

  • (ArgumentError)

    if mode is present and not a recognized mode



86
87
88
89
90
91
# File 'lib/familia/data_type/class_methods.rb', line 86

def validate_dirty_write_warnings!(mode)
  return if mode.nil? || DIRTY_WRITE_MODES.include?(mode)

  raise ArgumentError,
        "dirty_write_warnings must be one of #{DIRTY_WRITE_MODES.inspect}, got #{mode.inspect}"
end

#validate_max_length!(value, klass) ⇒ Object

Validates a +max_length:+ option against the DataType class that would receive it. Class-level so a caller holding only a definition (Horreum.configure_related_field) can fail at configuration time with exactly the error #initialize would raise at first use.

Parameters:

  • value (Object, nil)

    the proposed cap

  • klass (Class)

    the DataType subclass the cap is for

Raises:

  • (ArgumentError)

    if value is present and not a positive Integer, or if klass does not implement max_length trimming



69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/familia/data_type/class_methods.rb', line 69

def validate_max_length!(value, klass)
  return if value.nil?

  unless value.is_a?(Integer) && value.positive?
    raise ArgumentError,
          "max_length must be a positive Integer, got #{value.inspect}"
  end

  return if klass.supports_max_length?

  raise ArgumentError,
        "max_length is not supported by #{klass.name} " \
        '(only SortedSet and ListKey trim on write)'
end