summaryrefslogtreecommitdiffstats
path: root/lib/roo/helpers/default_attr_reader.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/roo/helpers/default_attr_reader.rb')
-rw-r--r--lib/roo/helpers/default_attr_reader.rb20
1 files changed, 20 insertions, 0 deletions
diff --git a/lib/roo/helpers/default_attr_reader.rb b/lib/roo/helpers/default_attr_reader.rb
new file mode 100644
index 0000000..a02ba84
--- /dev/null
+++ b/lib/roo/helpers/default_attr_reader.rb
@@ -0,0 +1,20 @@
+# frozen_string_literal: true
+
+module Roo
+ module Helpers
+ module DefaultAttrReader
+ def attr_reader_with_default(attr_hash)
+ attr_hash.each do |attr_name, default_value|
+ instance_variable = :"@#{attr_name}"
+ define_method attr_name do
+ if instance_variable_defined? instance_variable
+ instance_variable_get instance_variable
+ else
+ default_value
+ end
+ end
+ end
+ end
+ end
+ end
+end