Module: Card::Set::Abstract::Attachment::Cloud

Extended by:
Card::Set
Defined in:
platypus/tmp/set/gem-defaults/mod016-carrierwave/abstract/attachment/cloud.rb

Overview

Set: Abstract (Attachment, Cloud)

Constant Summary

Constants included from Helpers

Helpers::SET_PATTERN_TEST_REGEXP

Constants included from Event::Api

Event::Api::OPTIONS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Card::Set

reset

Methods included from I18nScope

#mod_name, #scope

Methods included from Registrar

#extended, #finalize_load, #process_base_modules, #register_set

Methods included from Helpers

#format_module, #format_modules, #method_missing, #modules, #pattern_code, #respond_to_missing?, #set_name_parts, #shortname, #underscored_name

Methods included from Card::Set::AdvancedApi

#assign_type, #attachment, #define_set_from_error, #ensure_set, #setting_opts, #stage_method

Methods included from Format

#before, #format, layout_method_name, #view, view_method_name, view_setting_method_name, wrapper_method_name

Methods included from Inheritance

#include_set, #include_set_formats

Methods included from Trait

#card_accessor, #card_reader, #card_writer, #require_field

Methods included from Event::Api

#event

Instance Attribute Details

#bucketObject



15
16
17
# File 'platypus/tmp/set/gem-defaults/mod016-carrierwave/abstract/attachment/cloud.rb', line 15

def bucket
  @bucket ||= cloud? && (new_card_bucket || bucket_from_content || bucket_from_config)
end

Class Method Details

.source_locationObject



7
# File 'platypus/tmp/set/gem-defaults/mod016-carrierwave/abstract/attachment/cloud.rb', line 7

def self.source_location; "/Users/ethan/dev/decko/gem/mod/carrierwave/set/abstract/attachment/cloud.rb"; end

Instance Method Details

#bucket_configObject



30
31
32
# File 'platypus/tmp/set/gem-defaults/mod016-carrierwave/abstract/attachment/cloud.rb', line 30

def bucket_config
  @bucket_config ||= load_bucket_config
end

#bucket_from_configObject



120
121
122
123
# File 'platypus/tmp/set/gem-defaults/mod016-carrierwave/abstract/attachment/cloud.rb', line 120

def bucket_from_config
  cnf = Cardio.config
  cnf.file_default_bucket || cnf.file_buckets&.keys&.first
end

#bucket_from_contentObject



114
115
116
117
118
# File 'platypus/tmp/set/gem-defaults/mod016-carrierwave/abstract/attachment/cloud.rb', line 114

def bucket_from_content
  return unless content

  content.match(/^\((?<bucket>[^)]+)\)/) { |m| m[:bucket] }
end

#cant_find_in_bucket!(need) ⇒ Object

Raises:



62
63
64
# File 'platypus/tmp/set/gem-defaults/mod016-carrierwave/abstract/attachment/cloud.rb', line 62

def cant_find_in_bucket! need
  raise Card::Error, "couldn't find #{need} for bucket #{bucket}"
end

#change_bucket_if_read_only?Boolean

Returns:

  • (Boolean)


125
126
127
# File 'platypus/tmp/set/gem-defaults/mod016-carrierwave/abstract/attachment/cloud.rb', line 125

def change_bucket_if_read_only?
  cloud? && bucket_config[:read_only] && attachment_is_changing?
end

#credential_config(config) {|| ... } ⇒ Object

Yields:

  • ()


76
77
78
79
80
81
# File 'platypus/tmp/set/gem-defaults/mod016-carrierwave/abstract/attachment/cloud.rb', line 76

def credential_config config
  config[:credentials] ||= {}
  yield config[:credentials]
  config.delete :credentials if config[:credentials].blank?
  config
end

#credential_from_env_regexpObject



104
105
106
# File 'platypus/tmp/set/gem-defaults/mod016-carrierwave/abstract/attachment/cloud.rb', line 104

def credential_from_env_regexp
  Regexp.new "^(?:#{bucket.to_s.upcase}_)?CREDENTIALS_(?<option>.+)$"
end

#each_config_option_from_envObject



83
84
85
86
87
# File 'platypus/tmp/set/gem-defaults/mod016-carrierwave/abstract/attachment/cloud.rb', line 83

def each_config_option_from_env
  CarrierWave::FileCardUploader::CONFIG_OPTIONS.each do |key|
    yield key unless key.in? %i[attributes credentials]
  end
end

#each_credential_from_envObject



95
96
97
98
99
100
101
102
# File 'platypus/tmp/set/gem-defaults/mod016-carrierwave/abstract/attachment/cloud.rb', line 95

def each_credential_from_env
  regexp = credential_from_env_regexp
  ENV.each_key do |env_key|
    next unless (m = regexp.match env_key)

    yield m[:option].downcase.to_sym
  end
end

#ensure_bucket_configObject



47
48
49
50
51
52
# File 'platypus/tmp/set/gem-defaults/mod016-carrierwave/abstract/attachment/cloud.rb', line 47

def ensure_bucket_config
  yield.tap do |config|
    require_configuration! config
    require_credentials! config
  end
end

#event: change_bucket_if_read_onlyObject



10
11
12
13
# File 'platypus/tmp/set/gem-defaults/mod016-carrierwave/abstract/attachment/cloud.rb', line 10

event :change_bucket_if_read_only, :initialize,
      on: :update, when: :change_bucket_if_read_only? do
  @storage_type = storage_type_from_config
end

#load_bucket_configObject



34
35
36
37
38
39
40
41
42
43
44
45
# File 'platypus/tmp/set/gem-defaults/mod016-carrierwave/abstract/attachment/cloud.rb', line 34

def load_bucket_config
  return {} unless bucket

  bucket_config = Cardio.config.file_buckets&.dig(bucket.to_sym) || {}
  bucket_config.symbolize_keys!
  bucket_config[:credentials]&.symbolize_keys!
  # we don't want :attributes hash symbolized, so we can't use
  # deep_symbolize_keys
  ensure_bucket_config do
    load_bucket_config_from_env bucket_config
  end
end

#load_bucket_config_from_env(config) ⇒ Object



66
67
68
69
70
71
72
73
74
# File 'platypus/tmp/set/gem-defaults/mod016-carrierwave/abstract/attachment/cloud.rb', line 66

def load_bucket_config_from_env config
  config ||= {}
  each_config_option_from_env do |key|
    replace_with_env_variable config, key
  end
  credential_config config do |cred_hash|
    load_bucket_credentials_from_env cred_hash
  end
end

#load_bucket_credentials_from_env(cred_config) ⇒ Object



89
90
91
92
93
# File 'platypus/tmp/set/gem-defaults/mod016-carrierwave/abstract/attachment/cloud.rb', line 89

def load_bucket_credentials_from_env cred_config
  each_credential_from_env do |option|
    replace_with_env_variable cred_config, option, "credentials"
  end
end

#new_card_bucketObject



19
20
21
22
23
24
25
26
27
28
# File 'platypus/tmp/set/gem-defaults/mod016-carrierwave/abstract/attachment/cloud.rb', line 19

def new_card_bucket
  return unless new_card?

  # If the file is assigned before the bucket option we have to
  # check if there is a bucket options in set_specific.
  # That happens for exmaple when the file appears before the bucket in the
  # options hash:
  #   Card.create file: file_handle, bucket: "my_bucket"
  set_specific[:bucket] || set_specific["bucket"] || bucket_from_config
end

#replace_with_env_variable(config, option, prefix = nil) ⇒ Object



108
109
110
111
112
# File 'platypus/tmp/set/gem-defaults/mod016-carrierwave/abstract/attachment/cloud.rb', line 108

def replace_with_env_variable config, option, prefix=nil
  env_key = [prefix, option].compact.join("_").upcase
  new_value = ENV["#{bucket.to_s.upcase}_#{env_key}"] || ENV[env_key]
  config[option] = new_value if new_value
end

#require_configuration!(config) ⇒ Object



54
55
56
# File 'platypus/tmp/set/gem-defaults/mod016-carrierwave/abstract/attachment/cloud.rb', line 54

def require_configuration! config
  cant_find_in_bucket! "configuration" unless config.present?
end

#require_credentials!(config) ⇒ Object



58
59
60
# File 'platypus/tmp/set/gem-defaults/mod016-carrierwave/abstract/attachment/cloud.rb', line 58

def require_credentials! config
  cant_find_in_bucket! "credentials" unless config[:credentials]
end