Class: Kithe::TimingPromotionDirective

Inherits:
Object
  • Object
show all
Defined in:
app/models/kithe/timing_promotion_directive.rb

Overview

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

Constant Summary collapse

DEFAULT_VALUE =
"background"
ALLOWED_VALUES =
["background", "inline", "false"]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key:, directives:) {|_self| ... } ⇒ TimingPromotionDirective

Returns a new instance of TimingPromotionDirective.

Yields:

  • (_self)

Yield Parameters:



23
24
25
26
27
28
29
30
31
32
# File 'app/models/kithe/timing_promotion_directive.rb', line 23

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

Instance Attribute Details

#directive_keyObject (readonly)

Returns the value of attribute directive_key.



21
22
23
# File 'app/models/kithe/timing_promotion_directive.rb', line 21

def directive_key
  @directive_key
end

#directivesObject (readonly)

Returns the value of attribute directives.



21
22
23
# File 'app/models/kithe/timing_promotion_directive.rb', line 21

def directives
  @directives
end

Instance Method Details

#background?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'app/models/kithe/timing_promotion_directive.rb', line 46

def background?
  directive_value == "background"
end

#directive_valueObject



34
35
36
37
38
39
40
# File 'app/models/kithe/timing_promotion_directive.rb', line 34

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

#disabled?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'app/models/kithe/timing_promotion_directive.rb', line 50

def disabled?
  directive_value == "false"
end

#inline?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'app/models/kithe/timing_promotion_directive.rb', line 42

def inline?
  directive_value == "inline"
end