Sha256: 45438d0a8317cb9faa5ee0a20a9c5f401d821fd513c93871dc187bedbeb3ef37

Contents?: true

Size: 1.33 KB

Versions: 113

Compression:

Stored size: 1.33 KB

Contents

# frozen_string_literal: true

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

module EacRubyUtils
  module Immutable
    class ArrayAccessor < ::EacRubyUtils::Immutable::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(self)
          when 1 then next accessor.immutable_value_set(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 || []
      end

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

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

Version data entries

113 entries across 113 versions & 2 rubygems

Version Path
eac_tools-0.66.0 sub/eac_ruby_utils/lib/eac_ruby_utils/immutable/array_accessor.rb
eac_tools-0.65.1 sub/eac_ruby_utils/lib/eac_ruby_utils/immutable/array_accessor.rb
eac_ruby_utils-0.116.0 lib/eac_ruby_utils/immutable/array_accessor.rb
eac_ruby_utils-0.115.0 lib/eac_ruby_utils/immutable/array_accessor.rb
eac_tools-0.65.0 sub/eac_ruby_utils/lib/eac_ruby_utils/immutable/array_accessor.rb
eac_ruby_utils-0.114.0 lib/eac_ruby_utils/immutable/array_accessor.rb
eac_tools-0.64.0 sub/eac_ruby_utils/lib/eac_ruby_utils/immutable/array_accessor.rb
eac_ruby_utils-0.113.0 lib/eac_ruby_utils/immutable/array_accessor.rb
eac_tools-0.63.0 sub/eac_ruby_utils/lib/eac_ruby_utils/immutable/array_accessor.rb
eac_tools-0.62.1 sub/eac_ruby_utils/lib/eac_ruby_utils/immutable/array_accessor.rb
eac_tools-0.62.0 sub/eac_ruby_utils/lib/eac_ruby_utils/immutable/array_accessor.rb
eac_tools-0.61.1 sub/eac_ruby_utils/lib/eac_ruby_utils/immutable/array_accessor.rb
eac_tools-0.61.0 sub/eac_ruby_utils/lib/eac_ruby_utils/immutable/array_accessor.rb
eac_tools-0.60.3 sub/eac_ruby_utils/lib/eac_ruby_utils/immutable/array_accessor.rb
eac_tools-0.60.2 sub/eac_ruby_utils/lib/eac_ruby_utils/immutable/array_accessor.rb
eac_tools-0.60.1 sub/eac_ruby_utils/lib/eac_ruby_utils/immutable/array_accessor.rb
eac_tools-0.60.0 sub/eac_ruby_utils/lib/eac_ruby_utils/immutable/array_accessor.rb
eac_tools-0.59.0 sub/eac_ruby_utils/lib/eac_ruby_utils/immutable/array_accessor.rb
eac_tools-0.58.0 sub/eac_ruby_utils/lib/eac_ruby_utils/immutable/array_accessor.rb
eac_tools-0.57.0 sub/eac_ruby_utils/lib/eac_ruby_utils/immutable/array_accessor.rb