Module: Tins::ClassMethod

Includes:
Eigenclass
Included in:
Module
Defined in:
lib/tins/dslkit.rb

Overview

This module provides convenient helpers for defining class methods and attributes on classes themselves.

Examples:

Using ClassMethod module to define class methods dynamically

class MyClass
  include Tins::ClassMethod
end

# Define class methods using the ClassMethod helpers
MyClass.class_attr_accessor :foo
MyClass.foo = "bar"
MyClass.foo  # => "bar"

MyClass.class_define_method(:baz) { "qux" }
MyClass.baz  # => "qux"

Instance Method Summary collapse

Methods included from Eigenclass

#eigenclass_class_eval, #eigenclass_eval

Instance Method Details

#class_attr_accessor(*ids) ⇒ void Also known as: class_attr

This method returns an undefined value.

Define reader and writer attribute methods for all *ids.

Parameters:

  • ids (Array<Symbol>)

    The names of the attributes to define



100
101
102
# File 'lib/tins/dslkit.rb', line 100

def class_attr_accessor(*ids)
  eigenclass_eval { attr_accessor(*ids) }
end

#class_attr_reader(*ids) ⇒ void

This method returns an undefined value.

Define reader attribute methods for all *ids.

Parameters:

  • ids (Array<Symbol>)

    The names of the attributes to define



113
114
115
# File 'lib/tins/dslkit.rb', line 113

def class_attr_reader(*ids)
  eigenclass_eval { attr_reader(*ids) }
end

#class_attr_writer(*ids) ⇒ void

This method returns an undefined value.

Define writer attribute methods for all *ids.

Parameters:

  • ids (Array<Symbol>)

    The names of the attributes to define



121
122
123
# File 'lib/tins/dslkit.rb', line 121

def class_attr_writer(*ids)
  eigenclass_eval { attr_writer(*ids) }
end

#class_define_method(name) {|Object| ... } ⇒ void

This method returns an undefined value.

Define a class method named name using block.

Parameters:

  • name (Symbol)

    The name of the method to define

Yields:

  • (Object)

    The block that defines the method’s behavior



92
93
94
# File 'lib/tins/dslkit.rb', line 92

def class_define_method(name, &block)
  eigenclass_eval { define_method(name, &block) }
end