Sha256: cec94495192f7e20f6ba2ea9d5af81eedf095af18b34f54d98a3a96f785f3cc1

Contents?: true

Size: 775 Bytes

Versions: 13

Compression:

Stored size: 775 Bytes

Contents

# frozen_string_literal: true

module LunaPark
  module Extensions
    module TypedAttrAccessor
      def typed_attr_accessor(*names, callable, is_array: false)
        attr_reader(*names)
        typed_attr_writer(*names, callable, is_array: is_array)
      end

      def typed_attr_writer(*names, callable, is_array: false)
        return attr_writer(*names) if callable.nil?

        names.each do |name|
          setter = :"#{name}="
          ivar   = :"@#{name}"
          if is_array
            define_method(setter) { |input| instance_variable_set(ivar, input&.map { |elem| callable.call(elem) }) }
          else
            define_method(setter) { |input| instance_variable_set(ivar, callable.call(input)) }
          end
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
luna_park-0.13.2 lib/luna_park/extensions/typed_attr_accessor.rb
luna_park-0.13.1 lib/luna_park/extensions/typed_attr_accessor.rb
luna_park-0.13.0 lib/luna_park/extensions/typed_attr_accessor.rb
luna_park-0.12.1 lib/luna_park/extensions/typed_attr_accessor.rb
luna_park-0.12.0 lib/luna_park/extensions/typed_attr_accessor.rb
luna_park-0.11.7 lib/luna_park/extensions/typed_attr_accessor.rb
luna_park-0.11.6 lib/luna_park/extensions/typed_attr_accessor.rb
luna_park-0.11.5 lib/luna_park/extensions/typed_attr_accessor.rb
luna_park-0.11.4 lib/luna_park/extensions/typed_attr_accessor.rb
luna_park-0.11.3 lib/luna_park/extensions/typed_attr_accessor.rb
luna_park-0.11.2 lib/luna_park/extensions/typed_attr_accessor.rb
luna_park-0.11.1 lib/luna_park/extensions/typed_attr_accessor.rb
luna_park-0.11.0 lib/luna_park/extensions/typed_attr_accessor.rb