Sha256: f7f7d24ccad28046486dbf06b4c59bd8ba697f7cf1b7fe997bcbc4492bcf4f73

Contents?: true

Size: 1.62 KB

Versions: 19

Compression:

Stored size: 1.62 KB

Contents

# frozen_string_literal: true

require 'eac_ruby_utils/core_ext'
require 'ostruct'

module Ehbrs
  module Observers
    class Base
      include ::EacRubyUtils::Listable

      lists.add_string :blank_value, :add, :ignore, :raise

      attr_reader :records, :blank_value, :last_check_time

      common_constructor :options, default: [{}] do
        @records = options[:records] || []
        @blank_value = options[:blank_value].if_present(BLANK_VALUE_ADD) do |v|
          v = v.to_s
          ::Ehbrs::Observers::Base.lists.blank_value.value_validate!(v)
          v
        end
      end

      def check(value, date = ::Time.zone.now)
        @last_check_time = date
        send("check_with_blank_value_#{blank_value}", value, date)
      end

      def changing_value?(value)
        records.if_present(true) do
          last_value.if_present(value.present?) { |v| v != value }
        end
      end

      def last_change_time
        records.last.if_present(&:time)
      end

      def last_value
        records.last.if_present(&:value)
      end

      private

      def check_with_blank_value_add(value, time)
        return false unless changing_value?(value)

        records << ::OpenStruct.new(value: value, time: time)
        true
      end

      def check_with_blank_value_ignore(value, date)
        return false if value.blank? ? false : check_with_blank_value_add(value, date)
      end

      def check_with_blank_value_raise(value, date)
        raise(::ArgumentError, "Blank value checked (Value: #{value}, Class: #{value.class})") if
        value.blank?

        check_with_blank_value_add(value, date)
      end
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
ehbrs-tools-0.19.0 lib/ehbrs/observers/base.rb
ehbrs-tools-0.18.0 lib/ehbrs/observers/base.rb
ehbrs-tools-0.17.0 lib/ehbrs/observers/base.rb
ehbrs-tools-0.16.5 lib/ehbrs/observers/base.rb
ehbrs-tools-0.16.4 lib/ehbrs/observers/base.rb
ehbrs-tools-0.16.3 lib/ehbrs/observers/base.rb
ehbrs-tools-0.16.2 lib/ehbrs/observers/base.rb
ehbrs-tools-0.16.1 lib/ehbrs/observers/base.rb
ehbrs-tools-0.16.0 lib/ehbrs/observers/base.rb
ehbrs-tools-0.15.0 lib/ehbrs/observers/base.rb
ehbrs-tools-0.14.1 lib/ehbrs/observers/base.rb
ehbrs-tools-0.14.0 lib/ehbrs/observers/base.rb
ehbrs-tools-0.13.1 lib/ehbrs/observers/base.rb
ehbrs-tools-0.13.0 lib/ehbrs/observers/base.rb
ehbrs-tools-0.12.0 lib/ehbrs/observers/base.rb
ehbrs-tools-0.11.0 lib/ehbrs/observers/base.rb
ehbrs-tools-0.10.0 lib/ehbrs/observers/base.rb
ehbrs-tools-0.9.0 lib/ehbrs/observers/base.rb
ehbrs-tools-0.7.0 lib/ehbrs/observers/base.rb