File: default_attr_reader.rb

package info (click to toggle)
ruby-roo 3.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 8,216 kB
  • sloc: ruby: 6,529; xml: 88; makefile: 6
file content (20 lines) | stat: -rw-r--r-- 500 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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