Sha256: 8e513f845436f9e7ea8f8cec7fd2bb1d8afd1d65b061eca9ca7354fdd02af488

Contents?: true

Size: 960 Bytes

Versions: 2

Compression:

Stored size: 960 Bytes

Contents

module Troo
  module Behaviours
    class NullEntity
      # @return [FalseClass]
      def default?
        false
      end
    end

    class SetDefault
      class << self
        # @param  []
        # @return []
        def for(entity)
          new(entity).set_default!
        end
      end

      # @param  []
      # @return [Troo::Behaviours::SetDefault]
      def initialize(entity)
        @entity = entity
      end

      # @return [TrueClass, ?]
      def set_default!
        return true if already_default?
        unset_default
        set_new_default
        entity
      end

      private

      attr_reader :entity

      def set_new_default
        entity.update(default: true)
      end

      def unset_default
        entity.class.update(default: false)
      end

      def already_default?
        entity.default?
      end

      def entity
        return NullEntity.new if @entity.nil?
        @entity
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
troo-0.0.15 lib/troo/models/behaviours/set_default.rb
troo-0.0.14 lib/troo/models/behaviours/set_default.rb