Sha256: 120a10f7fb6090fc66a02c790309ff2b33b44c137ea33832f4ec0f8b7506fecc

Contents?: true

Size: 903 Bytes

Versions: 2

Compression:

Stored size: 903 Bytes

Contents

# frozen_string_literal: true

module Beaconable
  class BaseBeacon
    attr_reader :object, :object_was

    def initialize(object, object_was)
      @object = object
      @object_was = object_was
    end

    def field_changed(field)
      @field = field
      @result = field_changed? field
      self
    end

    def from(*values)
      return self unless @result

      @result = values.include? object_was.send(@field)
      self
    end

    def to(*values)
      @result && values.include?(object.send(@field))
    end

    private

    def destroyed_entry?
      object.destroyed?
    end

    def field_changed?(field)
      object.send(field) != object_was.send(field)
    end

    def any_field_changed?(*fields)
      fields.each do |field|
        return true if field_changed?(field)
      end
      false
    end

    def new_entry?
      object_was.created_at.nil?
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
beaconable-0.3.3 lib/beaconable/base_beacon.rb
beaconable-0.3.2 lib/beaconable/base_beacon.rb