Sha256: 16ebc2639d036328409bc1bc147ea695cf24978a06380f3c30495dd7e2cc4d7d

Contents?: true

Size: 693 Bytes

Versions: 4

Compression:

Stored size: 693 Bytes

Contents

module Troo
  class NullEntity
    def default?
      true
    end
  end

  class SetDefault
    class << self
      def for(entity)
        new(entity).set_default!
      end
    end

    def initialize(entity)
      @entity = entity
    end

    def set_default!
      return false 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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
troo-0.0.7 lib/troo/actions/set_default.rb
troo-0.0.6 lib/troo/actions/set_default.rb
troo-0.0.5 lib/troo/actions/set_default.rb
troo-0.0.4 lib/troo/actions/set_default.rb