Sha256: 46ad1111fd132a79f3b12772e70f3a3e0b6a54347796bc2d00407adf7edbeeb0

Contents?: true

Size: 1.49 KB

Versions: 32

Compression:

Stored size: 1.49 KB

Contents

module Kithe
  # Just a handy class for handling logic with our promotion directives
  # for "timing" promotion directives whose values are "false", "background",
  # or "inline", where the default is "background"
  #
  # These directives are :promote, :create_derivatives, and :delete
  #
  # You might use like:
  #
  #     Kithe::LifecyclePromotionDirective.new(key: :promotion, directives: asset.file_attacher.promotion_directives) do |directive|
  #       if directive.inline?
  #         run_something
  #       elsif directive.background?
  #         SomeJob.perform_later
  #       end
  #     end
  class TimingPromotionDirective
    DEFAULT_VALUE = "background"
    ALLOWED_VALUES = ["background", "inline", "false"]

    attr_reader :directive_key, :directives

    def initialize(key:, directives: )
      @directive_key = key.to_s
      @directives = directives

      unless ALLOWED_VALUES.include?(directive_value)
        raise ArgumentError.new("Unrecognized value `#{directive_value}` for `#{key}`; must be #{ALLOWED_VALUES}")
      end

      yield self
    end

    def directive_value
      @directive_value ||= begin
        value = (directives || {})[directive_key]
        # not blank?, cause false we want to recognize.
        (value.nil? || value == "") ? DEFAULT_VALUE : value.to_s
      end
    end

    def inline?
      directive_value == "inline"
    end

    def background?
      directive_value == "background"
    end

    def disabled?
      directive_value == "false"
    end
  end
end

Version data entries

32 entries across 32 versions & 1 rubygems

Version Path
kithe-2.16.0 app/models/kithe/timing_promotion_directive.rb
kithe-2.15.1 app/models/kithe/timing_promotion_directive.rb
kithe-2.15.0 app/models/kithe/timing_promotion_directive.rb
kithe-2.14.0 app/models/kithe/timing_promotion_directive.rb
kithe-2.13.0 app/models/kithe/timing_promotion_directive.rb
kithe-2.12.0 app/models/kithe/timing_promotion_directive.rb
kithe-2.11.0 app/models/kithe/timing_promotion_directive.rb
kithe-2.10.0 app/models/kithe/timing_promotion_directive.rb
kithe-2.9.1 app/models/kithe/timing_promotion_directive.rb
kithe-2.9.0 app/models/kithe/timing_promotion_directive.rb
kithe-2.8.0 app/models/kithe/timing_promotion_directive.rb
kithe-2.7.1 app/models/kithe/timing_promotion_directive.rb
kithe-2.7.0 app/models/kithe/timing_promotion_directive.rb
kithe-2.6.1 app/models/kithe/timing_promotion_directive.rb
kithe-2.6.0 app/models/kithe/timing_promotion_directive.rb
kithe-2.5.0 app/models/kithe/timing_promotion_directive.rb
kithe-2.4.0 app/models/kithe/timing_promotion_directive.rb
kithe-2.3.0 app/models/kithe/timing_promotion_directive.rb
kithe-2.2.0 app/models/kithe/timing_promotion_directive.rb
kithe-2.1.0 app/models/kithe/timing_promotion_directive.rb