Sha256: 84d91289b5ea8e55a24cdc5eed2725c1ba04a62025f12df03b21f0484839e1e4

Contents?: true

Size: 1.71 KB

Versions: 26

Compression:

Stored size: 1.71 KB

Contents

# frozen_string_literal: true

require 'eac_ruby_utils/acts_as_immutable/base_accessor'
require 'eac_ruby_utils/patches/class/common_constructor'

module EacRubyUtils
  module ActsAsImmutable
    class HashAccessor < ::EacRubyUtils::ActsAsImmutable::BaseAccessor
      def apply(klass)
        apply_plural(klass)
        apply_singular(klass)
      end

      def immutable_value_get(object)
        super || {}
      end

      def immutable_value_get_single(object, key)
        immutable_value_get(object)[key]
      end

      def immutable_value_set(object, new_hash)
        duplicate_object(object) { |_old_value| new_hash }
      end

      def immutable_value_set_single(object, key, value)
        immutable_value_set(object, immutable_value_get(object).merge(key => value))
      end

      private

      def apply_plural(klass)
        accessor = self
        klass.send(:define_method, ::ActiveSupport::Inflector.pluralize(name)) do |*args|
          case args.count
          when 0 then next accessor.immutable_value_get_filtered(self)
          when 1 then next accessor.immutable_value_set_filtered(self, args[0])
          else
            raise ::ArgumentError, "wrong number of arguments (given #{args.count}, expected 0..1)"
          end
        end
      end

      def apply_singular(klass)
        accessor = self
        klass.send(:define_method, name) do |*args|
          case args.count
          when 1 then next accessor.immutable_value_get_single(self, args[0])
          when 2 then next accessor.immutable_value_set_single(self, *args[0..1])
          else
            raise ::ArgumentError, "wrong number of arguments (given #{args.count}, expected 1..2)"
          end
        end
      end
    end
  end
end

Version data entries

26 entries across 26 versions & 2 rubygems

Version Path
eac_ruby_utils-0.123.0 lib/eac_ruby_utils/acts_as_immutable/hash_accessor.rb
eac_ruby_utils-0.121.0 lib/eac_ruby_utils/acts_as_immutable/hash_accessor.rb
eac_ruby_utils-0.120.0 lib/eac_ruby_utils/acts_as_immutable/hash_accessor.rb
eac_tools-0.76.1 sub/eac_ruby_utils/lib/eac_ruby_utils/acts_as_immutable/hash_accessor.rb
eac_tools-0.76.0 sub/eac_ruby_utils/lib/eac_ruby_utils/acts_as_immutable/hash_accessor.rb
eac_tools-0.75.2 sub/eac_ruby_utils/lib/eac_ruby_utils/acts_as_immutable/hash_accessor.rb
eac_ruby_utils-0.119.2 lib/eac_ruby_utils/acts_as_immutable/hash_accessor.rb
eac_tools-0.75.1 sub/eac_ruby_utils/lib/eac_ruby_utils/acts_as_immutable/hash_accessor.rb
eac_tools-0.75.0 sub/eac_ruby_utils/lib/eac_ruby_utils/acts_as_immutable/hash_accessor.rb
eac_ruby_utils-0.119.1 lib/eac_ruby_utils/acts_as_immutable/hash_accessor.rb
eac_tools-0.74.1 sub/eac_ruby_utils/lib/eac_ruby_utils/acts_as_immutable/hash_accessor.rb
eac_tools-0.74.0 sub/eac_ruby_utils/lib/eac_ruby_utils/acts_as_immutable/hash_accessor.rb
eac_tools-0.73.0 sub/eac_ruby_utils/lib/eac_ruby_utils/acts_as_immutable/hash_accessor.rb
eac_ruby_utils-0.119.0 lib/eac_ruby_utils/acts_as_immutable/hash_accessor.rb
eac_tools-0.72.0 sub/eac_ruby_utils/lib/eac_ruby_utils/acts_as_immutable/hash_accessor.rb
eac_ruby_utils-0.118.1 lib/eac_ruby_utils/acts_as_immutable/hash_accessor.rb
eac_ruby_utils-0.118.0 lib/eac_ruby_utils/acts_as_immutable/hash_accessor.rb
eac_tools-0.70.1 sub/eac_ruby_utils/lib/eac_ruby_utils/acts_as_immutable/hash_accessor.rb
eac_tools-0.70.0 sub/eac_ruby_utils/lib/eac_ruby_utils/acts_as_immutable/hash_accessor.rb
eac_tools-0.69.1 sub/eac_ruby_utils/lib/eac_ruby_utils/acts_as_immutable/hash_accessor.rb