Class: Cattri::AttributeOptions

Inherits:
Object
  • Object
show all
Defined in:
lib/cattri/attribute_options.rb

Overview

AttributeOptions encapsulates normalized metadata for a single Cattri-defined attribute.

It validates, transforms, and freezes all input during initialization, ensuring attribute safety and immutability at runtime.

Examples:

options = AttributeOptions.new(:enabled, default: true, expose: :read_write)
options.name         # => :enabled
options.default.call # => true
options.expose       # => :read_write

Constant Summary collapse

VISIBILITIES =

Valid method visibility levels.

%i[public protected private].freeze
EXPOSE_OPTIONS =

Valid expose options for method generation.

%i[read write read_write none].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, ivar: nil, final: false, scope: :instance, predicate: false, default: nil, transformer: nil, expose: :read_write, visibility: :public) ⇒ AttributeOptions

Initializes a frozen attribute configuration.

Parameters:

  • name (Symbol, String)

    the attribute name

  • ivar (Symbol, String, nil) (defaults to: nil)

    optional custom instance variable name

  • final (Boolean) (defaults to: false)

    marks the attribute as write-once

  • scope (Symbol) (defaults to: :instance)

    indicates if the attribute is class-level (:class) or instance-level (:instance)

  • predicate (Boolean) (defaults to: false)

    whether to define a ‘?` predicate method

  • default (Object, Proc, nil) (defaults to: nil)

    default value or callable

  • transformer (Proc, nil) (defaults to: nil)

    optional coercion block

  • expose (Symbol) (defaults to: :read_write)

    access level to define (:read, :write, :read_write, :none)

  • visibility (Symbol) (defaults to: :public)

    method visibility (:public, :protected, :private)



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/cattri/attribute_options.rb', line 73

def initialize(
  name,
  ivar: nil,
  final: false,
  scope: :instance,
  predicate: false,
  default: nil,
  transformer: nil,
  expose: :read_write,
  visibility: :public
)
  @name = name.to_sym
  @ivar = normalize_ivar(ivar)
  @final = final
  @scope = validate_scope!(scope)
  @predicate = predicate
  @default = normalize_default(default)
  @transformer = normalize_transformer(transformer)
  @expose = self.class.validate_expose!(expose)
  @visibility = self.class.validate_visibility!(visibility)

  freeze
end

Instance Attribute Details

#defaultObject (readonly)

Returns the value of attribute default.



59
60
61
# File 'lib/cattri/attribute_options.rb', line 59

def default
  @default
end

#exposeObject (readonly)

Returns the value of attribute expose.



59
60
61
# File 'lib/cattri/attribute_options.rb', line 59

def expose
  @expose
end

#finalObject (readonly)

Returns the value of attribute final.



59
60
61
# File 'lib/cattri/attribute_options.rb', line 59

def final
  @final
end

#ivarObject (readonly)

Returns the value of attribute ivar.



59
60
61
# File 'lib/cattri/attribute_options.rb', line 59

def ivar
  @ivar
end

#nameObject (readonly)

Returns the value of attribute name.



59
60
61
# File 'lib/cattri/attribute_options.rb', line 59

def name
  @name
end

#predicateObject (readonly)

Returns the value of attribute predicate.



59
60
61
# File 'lib/cattri/attribute_options.rb', line 59

def predicate
  @predicate
end

#scopeObject (readonly)

Returns the value of attribute scope.



59
60
61
# File 'lib/cattri/attribute_options.rb', line 59

def scope
  @scope
end

#transformerObject (readonly)

Returns the value of attribute transformer.



59
60
61
# File 'lib/cattri/attribute_options.rb', line 59

def transformer
  @transformer
end

#visibilityObject (readonly)

Returns the value of attribute visibility.



59
60
61
# File 'lib/cattri/attribute_options.rb', line 59

def visibility
  @visibility
end

Class Method Details

.validate_expose!(expose) ⇒ Symbol

Validates and normalizes the ‘expose` configuration.

Parameters:

  • expose (Symbol, String)

    one of: :read, :write, :read_write, :none

Returns:

  • (Symbol)

Raises:



25
26
27
28
29
30
# File 'lib/cattri/attribute_options.rb', line 25

def validate_expose!(expose)
  expose = expose.to_sym
  return expose if EXPOSE_OPTIONS.include?(expose) # steep:ignore

  raise Cattri::AttributeError, "Invalid expose option `#{expose.inspect}` for :#{name}"
end

.validate_visibility!(visibility) ⇒ Symbol

Validates and normalizes method visibility.

Parameters:

  • visibility (Symbol, String)

    one of: :public, :protected, :private

Returns:

  • (Symbol)

Raises:



37
38
39
40
41
42
# File 'lib/cattri/attribute_options.rb', line 37

def validate_visibility!(visibility)
  visibility = visibility.to_sym
  return visibility if VISIBILITIES.include?(visibility) # steep:ignore

  raise Cattri::AttributeError, "Invalid visibility `#{visibility.inspect}` for :#{name}"
end

Instance Method Details

#[](key) ⇒ Object

Allows hash-style access to the option set.

Parameters:

  • key (Symbol, String)

Returns:

  • (Object)


120
121
122
# File 'lib/cattri/attribute_options.rb', line 120

def [](key)
  to_h[key.to_sym]
end

#to_hHash<Symbol, Object>

Returns a frozen hash representation of this option set.

Returns:

  • (Hash<Symbol, Object>)


100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/cattri/attribute_options.rb', line 100

def to_h
  hash = {
    name: @name,
    ivar: @ivar,
    final: @final,
    scope: @scope,
    predicate: @predicate,
    default: @default,
    transformer: @transformer,
    expose: @expose,
    visibility: @visibility
  }
  hash.freeze
  hash
end