Sha256: b6e10fc33c4c65344ee1bdc6aca0c36392951fa7fdee28b796686441448cd465

Contents?: true

Size: 935 Bytes

Versions: 8

Compression:

Stored size: 935 Bytes

Contents

module ESA
  module Associations
    module FlagsExtension
      def most_recent(nature, time=Time.zone.now, exclude=nil)
        query = where(nature: nature).
              where('esa_flags.time <= ?', time)

        if exclude.present?
          query = query.where('esa_flags.id not in (?)', exclude)
        end

        query.order('esa_flags.time DESC, esa_flags.created_at DESC').first
      end

      def is_set?(nature, time=Time.zone.now, exclude=nil)
        most_recent = most_recent(nature, time, exclude)
        if most_recent.present?
          most_recent.is_set? # return the set bit of this flag
        else
          false
        end
      end

      def transition_for(flag)
        if flag.state and not is_set?(flag.nature, flag.time, flag.id)
          1
        elsif not flag.state and is_set?(flag.nature, flag.time, flag.id)
          -1
        else
          0
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
event_sourced_accounting-0.2.6 app/models/esa/associations/flags_extension.rb
event_sourced_accounting-0.2.4 app/models/esa/associations/flags_extension.rb
event_sourced_accounting-0.2.3 app/models/esa/associations/flags_extension.rb
event_sourced_accounting-0.2.2 app/models/esa/associations/flags_extension.rb
event_sourced_accounting-0.1.6 app/models/esa/associations/flags_extension.rb
event_sourced_accounting-0.1.4 app/models/esa/associations/flags_extension.rb
event_sourced_accounting-0.1.3 app/models/esa/associations/flags_extension.rb
event_sourced_accounting-0.1.1 app/models/esa/associations/flags_extension.rb