blob: d653e32fe870cbabac0edda473965107fda71f17 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
# frozen_string_literal: true
module Axlsx
# This module defines some of the more common validating attribute
# accessors that we use in Axlsx
#
# When this module is included in your class you can simply call
#
# string_attr_access :foo
#
# To generate a new, validating set of accessors for foo.
module Accessors
def self.included(base)
base.send :extend, ClassMethods
end
# Defines the class level xxx_attr_accessor methods
module ClassMethods
# Creates one or more string validated attr_accessors
# @param [Array] symbols An array of symbols representing the
# names of the attributes you will add to your class.
def string_attr_accessor(*symbols)
validated_attr_accessor(symbols, :validate_string)
end
# Creates one or more usigned integer attr_accessors
# @param [Array] symbols An array of symbols representing the
# names of the attributes you will add to your class
def unsigned_int_attr_accessor(*symbols)
validated_attr_accessor(symbols, :validate_unsigned_int)
end
# Creates one or more float (double?) attr_accessors
# @param [Array] symbols An array of symbols representing the
# names of the attributes you will add to your class
def float_attr_accessor(*symbols)
validated_attr_accessor(symbols, :validate_float)
end
# Creates on or more boolean validated attr_accessors
# @param [Array] symbols An array of symbols representing the
# names of the attributes you will add to your class.
def boolean_attr_accessor(*symbols)
validated_attr_accessor(symbols, :validate_boolean)
end
# Template for defining validated write accessors
SETTER = "def %s=(value) Axlsx::%s(value); @%s = value; end"
# Creates the reader and writer access methods
# @param [Array] symbols The names of the attributes to create
# @param [String] validator The axlsx validation method to use when
# validating assignation.
# @see lib/axlsx/util/validators.rb
def validated_attr_accessor(symbols, validator)
symbols.each do |symbol|
attr_reader symbol
module_eval(SETTER % [symbol, validator, symbol], __FILE__, __LINE__)
end
end
end
end
end
|