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
|
# File 'lib/creatable.rb', line 10
def attribute(name: nil, type: nil, kind_of: nil)
type ||= 'accessor'
raise ArgumentError, 'name is a required parameter' unless name
raise ArgumentError, "type must be of type: 'accessor', 'reader', or 'writer'" unless ['accessor', 'reader', 'writer'].include? type
if ['accessor', 'reader'].include?(type)
define_method name.to_s do
instance_variable_get "@#{name}"
end
end
if ['accessor', 'writer'].include?(type)
if kind_of.nil?
define_method "#{name}=" do |value|
instance_variable_set "@#{name}", value
end
else
define_method "#{name}=" do |value|
raise ArgumentError, "parameter #{name} (#{value.class}) is not a kind of (#{kind_of})" unless value.is_a?(kind_of) || value.nil?
instance_variable_set "@#{name}", value
end
end
end
attributes.push name.to_sym
end
|