Module: ClassAttribute::ClassMethods

Defined in:
lib/class_attribute.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#class_attribute(*attrs) ⇒ void

This method returns an undefined value.

Sets a class level attr_accessor(s) for the given attribute(s).

Examples:

require 'class_attribute'

class Animal
  include ClassAttribute

  class_attribute :legs, :tail

  self.legs = 0
  self.tail = false
end

class Dog < Animal
  self.legs = 4
  self.tail = true
end

class Chicken < Animal
  self.legs = 2
end

class Snake < Animal
end

Dog.legs     # => 4
Chicken.legs # => 2
Snake.legs   # => 0

Dog.tail      # => true
Chicken.tails # => false
Snake.tail    # => false

Parameters:

  • attr (Symbol)

    Name of the attribute to be set.

  • default (mixed)

    Attribute default value (optional).



56
57
58
59
60
61
62
# File 'lib/class_attribute.rb', line 56

def class_attribute(*attrs)
  singleton_class.class_eval do
    attr_accessor *attrs
  end

  class_attributes.merge(attrs)
end