Class: Class

Inherits:
Object
  • Object
show all
Defined in:
lib/typed_accessors/class.rb

Overview

Typed accessors works as a Mixin on Class. It creates a set of functions that are used in the class definition which eval and create new accessor methods at class eval time (so little performance penalty). For now, this is limitted to built in types, float, int, boolean, and date, though this approach could clearly be expanded.

Example of a class definition

class MyClass
  float_accessor :distance
  int_accessor :count
  bool_yn_accessor :onfire
  date_accessor :day
end

Instance Method Summary collapse

Instance Method Details

#bool_yn_accessor(*symbols) ⇒ Object

Creates a boolean accessor. It will convert and incoming string to a true / false value. If the string is “y” or “yes” it will be true, otherwise false.



22
23
24
25
# File 'lib/typed_accessors/class.rb', line 22

def bool_yn_accessor( *symbols )
    attr_reader( *symbols )
    bool_yn_writer( *symbols )
end

#date_accessor(*symbols) ⇒ Object

Creates a data accessor using the Date parse function on strings. Not defined for other input types.



44
45
46
47
# File 'lib/typed_accessors/class.rb', line 44

def date_accessor( *symbols )
    attr_reader( *symbols )
    date_writer( *symbols )
end

#float_accessor(*symbols) ⇒ Object

Creates a float accessor, using built in .to_f functions on objects. Any object that has a .to_f is supported.



30
31
32
33
# File 'lib/typed_accessors/class.rb', line 30

def float_accessor( *symbols )
    attr_reader( *symbols )
    float_writer( *symbols )
end

#int_accessor(*symbols) ⇒ Object

Creates an int accessor, using built in .to_i functions on objects. Any object that has a .to_i is supported.



37
38
39
40
# File 'lib/typed_accessors/class.rb', line 37

def int_accessor( *symbols )
    attr_reader( *symbols )
    int_writer( *symbols )
end