Module: Cattri::Visibility

Defined in:
lib/cattri/visibility.rb

Overview

Cattri::Visibility tracks the current method visibility context (‘public`, `protected`, `private`) when defining methods dynamically. It mimics Ruby’s native visibility behavior so that ‘cattri` definitions can automatically infer the intended access level based on the current context in the source file.

This module is intended to be extended by classes that include or extend Cattri.

Examples:

class MyClass
  include Cattri

  private
  cattri :sensitive_data
end

# => :sensitive_data will be defined as a private method

Instance Method Summary collapse

Instance Method Details

#__cattri_visibilitySymbol

Returns the currently active visibility scope on the class or module.

Defaults to ‘:public` unless changed explicitly via `public`, `protected`, or `private`.



26
27
28
# File 'lib/cattri/visibility.rb', line 26

def __cattri_visibility
  @__cattri_visibility ||= :public
end

#private(*args) ⇒ void

This method returns an undefined value.

Intercepts calls to ‘private` to update the visibility tracker.

If no method names are passed, this sets the current visibility scope for future methods. Otherwise, delegates to Ruby’s native ‘Module#private`.



61
62
63
64
# File 'lib/cattri/visibility.rb', line 61

def private(*args)
  @__cattri_visibility = :private if args.empty?
  Module.instance_method(:private).bind(self).call(*args)
end

#protected(*args) ⇒ void

This method returns an undefined value.

Intercepts calls to ‘protected` to update the visibility tracker.

If no method names are passed, this sets the current visibility scope for future methods. Otherwise, delegates to Ruby’s native ‘Module#protected`.



49
50
51
52
# File 'lib/cattri/visibility.rb', line 49

def protected(*args)
  @__cattri_visibility = :protected if args.empty?
  Module.instance_method(:protected).bind(self).call(*args)
end

#public(*args) ⇒ void

This method returns an undefined value.

Intercepts calls to ‘public` to update the visibility tracker.

If no method names are passed, this sets the current visibility scope for future methods. Otherwise, delegates to Ruby’s native ‘Module#public`.



37
38
39
40
# File 'lib/cattri/visibility.rb', line 37

def public(*args)
  @__cattri_visibility = :public if args.empty?
  Module.instance_method(:public).bind(self).call(*args)
end