Sha256: 02ca3d8723d0b1ad1d487fd424218ff34f607613ef79bf377cd7b83c6ca9a15b

Contents?: true

Size: 1.54 KB

Versions: 28

Compression:

Stored size: 1.54 KB

Contents

# frozen_string_literal: true

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

module EacRubyUtils
  module ActsAsImmutable
    # Should implement in concrect classes:
    # * immutable_value_set_assert(value)
    # * initial_value()
    class EnumerableAccessor < ::EacRubyUtils::ActsAsImmutable::BaseAccessor
      def apply(klass)
        apply_singular(klass)
        apply_plural(klass)
      end

      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.first)
          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 |value|
          accessor.immutable_value_push(self, value)
        end
      end

      def immutable_value_get(object)
        super || initial_value.dup
      end

      def immutable_value_push(object, value)
        duplicate_object(object) do |old_value|
          (old_value || initial_value.dup) + [value]
        end
      end

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

Version data entries

28 entries across 28 versions & 2 rubygems

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