Module: Accessor

Included in:
Object, Object
Defined in:
lib/accessor.rb,
lib/accessor/version.rb

Overview

The Accessor library is for giving your setters, getters, and accessors a more suggary syntax:

class Person
  accessor :name, :email
  writer :password
  reader :encrypted_password

  def encrypt_password!
    @encrypted_password = Base64::strict_encode64(@password)
  end

end

contact = Person.new
contact.name "Kurtis Rainbolt-Greene"
contact.email "[email protected]"
contact.email
  # => "[email protected]"
contact.password "ilovehorsesthateatglue"
contact.password
  # => 0 for 1 arguments
contact.encrypted_password "Test!"
  # => 1 for 0 arguments
contact.encrypt_password!
contact.encrypted_password
  # => "MTIzNDEyMzQ="
contact.name("James Earl Jones").email("[email protected]")
  # => #<Person:0x3E034 @name="James Earl Jones" @email="[email protected]">

Constant Summary collapse

VERSION =
"1.0.2"

Instance Method Summary collapse

Instance Method Details

#accessor(*names) ⇒ Object

The accessor method is a metamethod, and it takes an Array of Symbols. It goes over each Symbol and calls the attribute method with the Symbol as an argument, creating both a getter and setter method.



72
73
74
# File 'lib/accessor.rb', line 72

def accessor(*names)
  names.each { |name| attribute name }
end

#accessor!(*names) ⇒ Object

The accessor! method is a metamethod, and it takes the same arguments as accessor. The difference between the two are that accessor! is used to define setter/getter methods for the class of an instance object.

Raises:

  • (ArgumentError)


79
80
81
82
83
# File 'lib/accessor.rb', line 79

def accessor!(*names)
  raise ArgumentError unless names.any?
  self.class.accessor(*names)
  self
end

#reader(*names) ⇒ Object

The reader method is a metamethod, and it takes an Array of Symbols. It goes over each Symbol and calls the attribute method with the Symbol as an argument, creating a getter method.



38
39
40
# File 'lib/accessor.rb', line 38

def reader(*names)
  names.each { |name| attribute name, :reader }
end

#reader!(*names) ⇒ Object

The reader! method is a metamethod, and it takes the same arguments as reader. The difference between the two are that reader! is used to define getter methods for the class of an instance object.

Raises:

  • (ArgumentError)


45
46
47
48
49
# File 'lib/accessor.rb', line 45

def reader!(*names)
  raise ArgumentError unless names.any?
  self.class.reader(*names)
  self
end

#writer(*names) ⇒ Object

The writer method is a metamethod, and it takes an Array of Symbols. It goes over each Symbol and calls the attribute method with the Symbol as an argument, creating a setter method.



55
56
57
# File 'lib/accessor.rb', line 55

def writer(*names)
  names.each { |name| attribute name, :writer }
end

#writer!(*names) ⇒ Object

The writer! method is a metamethod, and it takes the same arguments as writer. The difference between the two are that writer! is used to define setter methods for the class of an instance object.

Raises:

  • (ArgumentError)


62
63
64
65
66
# File 'lib/accessor.rb', line 62

def writer!(*names)
  raise ArgumentError unless names.any?
  self.class.writer(*names)
  self
end