Module: Card::Set::Abstract::Attachment::StorageType

Extended by:
Card::Set
Defined in:
card/tmpsets/set/mod014-card-mod-carrierwave/abstract/attachment/storage_type.rb,
card/tmpsets/set/mod013-card-mod-carrierwave/abstract/attachment/storage_type.rb

Overview

Set: Abstract (Attachment, StorageType)

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_modules

Methods included from I18nScope

#mod_name, #scope

Methods included from Loader

#clean_empty_module_from_hash, #clean_empty_modules, #extended, #process_base_modules, #register_set

Methods included from Helpers

#method_missing, #num_set_parts, #pattern_code, #respond_to_missing?, #set_name_parts, #shortname, #underscore

Methods included from Card::Set::AdvancedApi

#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 Basket

#abstract_basket, #add_to_basket, #basket, #unshift_basket

Methods included from Trait

#card_accessor, #card_reader, #card_writer, #require_field

Methods included from Event::Api

#event

Instance Attribute Details

#bucket=(value) ⇒ Object (writeonly)

Sets the attribute bucket

Parameters:

  • value

    the value to set the attribute bucket to.



8
9
10
# File 'card/tmpsets/set/mod014-card-mod-carrierwave/abstract/attachment/storage_type.rb', line 8

def bucket=(value)
  @bucket = value
end

#storage_typeObject



60
61
62
63
# File 'card/tmpsets/set/mod014-card-mod-carrierwave/abstract/attachment/storage_type.rb', line 60

def storage_type
  @storage_type ||=
    new_card? ? storage_type_from_config : storage_type_from_content
end

Class Method Details

.source_locationObject



7
# File 'card/tmpsets/set/mod014-card-mod-carrierwave/abstract/attachment/storage_type.rb', line 7

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

Instance Method Details

#cloud?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'card/tmpsets/set/mod014-card-mod-carrierwave/abstract/attachment/storage_type.rb', line 40

def cloud?
  storage_type == :cloud
end

#coded?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'card/tmpsets/set/mod014-card-mod-carrierwave/abstract/attachment/storage_type.rb', line 52

def coded?
  storage_type == :coded
end

#deprecated_mod_file?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'card/tmpsets/set/mod014-card-mod-carrierwave/abstract/attachment/storage_type.rb', line 65

def deprecated_mod_file?
  content && (lines = content.split("\n")) && lines.size == 4
end

#event: storage_type_changeObject



10
11
12
13
14
15
16
17
18
19
20
21
# File 'card/tmpsets/set/mod014-card-mod-carrierwave/abstract/attachment/storage_type.rb', line 10

event :storage_type_change, :store, on: :update, when: :storage_type_changed? do
  # carrierwave stores file if @cache_id is not nil
  attachment.cache_stored_file!
  # attachment.retrieve_from_cache!(attachment.cache_name)
  update_storage_attributes
  # next line might be necessary to move files to cloud

  # make sure that we get the new identifier
  # otherwise action_id will return wrong id for new identifier
  db_content_will_change!
  write_identifier
end

#event: validate_storage_typeObject



23
24
25
26
27
28
29
30
# File 'card/tmpsets/set/mod014-card-mod-carrierwave/abstract/attachment/storage_type.rb', line 23

event :validate_storage_type, :validate, on: :save do
  unless known_storage_type? will_be_stored_as
    errors.add :storage_type, tr(
      :unknown_storage_type,
      new_storage_type: @new_storage_type
    )
  end
end

#invalid_storage_type!(type) ⇒ Object

Raises:



102
103
104
# File 'card/tmpsets/set/mod014-card-mod-carrierwave/abstract/attachment/storage_type.rb', line 102

def invalid_storage_type! type
  raise Card::Error, tr(:error_invalid_storage_type, type: type)
end

#known_storage_type?(type = storage_type) ⇒ Boolean

Returns:

  • (Boolean)


175
176
177
# File 'card/tmpsets/set/mod014-card-mod-carrierwave/abstract/attachment/storage_type.rb', line 175

def known_storage_type? type=storage_type
  type.in? CarrierWave::FileCardUploader::STORAGE_TYPES
end

#local?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'card/tmpsets/set/mod014-card-mod-carrierwave/abstract/attachment/storage_type.rb', line 48

def local?
  storage_type == :local
end

#modObject



69
70
71
# File 'card/tmpsets/set/mod014-card-mod-carrierwave/abstract/attachment/storage_type.rb', line 69

def mod
  @mod ||= coded? && mod_from_content
end

#mod_from_contentObject



73
74
75
76
77
78
79
# File 'card/tmpsets/set/mod014-card-mod-carrierwave/abstract/attachment/storage_type.rb', line 73

def mod_from_content
  if content =~ %r{^:[^/]+/([^.]+)}
    Regexp.last_match(1) # current mod_file format
  else
    mod_from_deprecated_content
  end
end

#mod_from_deprecated_contentObject

old format is still used in card_changes



82
83
84
85
86
# File 'card/tmpsets/set/mod014-card-mod-carrierwave/abstract/attachment/storage_type.rb', line 82

def mod_from_deprecated_content
  return if content =~ /^\~/
  return unless (lines = content.split("\n")) && lines.size == 4
  lines.last
end

#read_only?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'card/tmpsets/set/mod014-card-mod-carrierwave/abstract/attachment/storage_type.rb', line 36

def read_only?
  web? || (cloud? && bucket_config[:read_only])
end

#remote_storage?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'card/tmpsets/set/mod014-card-mod-carrierwave/abstract/attachment/storage_type.rb', line 56

def remote_storage?
  cloud? || web?
end

#stash_and_set_storage_options(opts) ⇒ Object



150
151
152
153
154
155
156
157
158
# File 'card/tmpsets/set/mod013-card-mod-carrierwave/abstract/attachment/storage_type.rb', line 150

def stash_and_set_storage_options opts
  %i[storage_type mod bucket].each_with_object({}) do |opt_name, old_values|
    next unless opts[opt_name]

    old_values[opt_name] = instance_variable_get "@#{opt_name}"
    instance_variable_set "@#{opt_name}", opts[opt_name]
    old_values
  end
end

#storage_type_changed?Boolean

Returns:

  • (Boolean)


127
128
129
# File 'card/tmpsets/set/mod014-card-mod-carrierwave/abstract/attachment/storage_type.rb', line 127

def storage_type_changed?
  @new_bucket || (@new_storage_type && @new_storage_type != storage_type) || @new_mod
end

#storage_type_from_configObject



88
89
90
# File 'card/tmpsets/set/mod014-card-mod-carrierwave/abstract/attachment/storage_type.rb', line 88

def storage_type_from_config
  valid_storage_type ENV["FILE_STORAGE"] || Cardio.config.file_storage
end

#storage_type_from_contentObject



106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'card/tmpsets/set/mod014-card-mod-carrierwave/abstract/attachment/storage_type.rb', line 106

def storage_type_from_content
  case content
  when /^\(/           then :cloud
  when %r{/^https?\:/} then :web
  when /^~/            then :local
  when /^\:/           then :coded
  else
    if deprecated_mod_file?
      :coded
    else
      storage_type_from_config
    end
  end
end

#temporary_storage_type_change?Boolean

Returns:

  • (Boolean)


159
160
161
# File 'card/tmpsets/set/mod014-card-mod-carrierwave/abstract/attachment/storage_type.rb', line 159

def temporary_storage_type_change?
  @temp_storage_type
end

#unknown_storage_type(type) ⇒ Object



176
177
178
# File 'card/tmpsets/set/mod013-card-mod-carrierwave/abstract/attachment/storage_type.rb', line 176

def unknown_storage_type type
  t :carrierwave_unknown_storage_type, new_storage_type: type
end

#update_storage_attributesObject



121
122
123
124
125
# File 'card/tmpsets/set/mod014-card-mod-carrierwave/abstract/attachment/storage_type.rb', line 121

def update_storage_attributes
  @mod = @new_mod if @new_mod
  @bucket = @new_bucket if @new_bucket
  @storage_type = @new_storage_type
end

#valid_storage_type(storage_type) ⇒ Object



92
93
94
95
96
# File 'card/tmpsets/set/mod014-card-mod-carrierwave/abstract/attachment/storage_type.rb', line 92

def valid_storage_type storage_type
  storage_type.to_sym.tap do |type|
    invalid_storage_type! type unless type.in? valid_storage_type_list
  end
end

#valid_storage_type_listObject



98
99
100
# File 'card/tmpsets/set/mod014-card-mod-carrierwave/abstract/attachment/storage_type.rb', line 98

def valid_storage_type_list
  CarrierWave::FileCardUploader::STORAGE_TYPES
end

#validate_temporary_storage_type_change(type = nil) ⇒ Object

Raises:



163
164
165
166
167
168
169
170
171
172
173
# File 'card/tmpsets/set/mod014-card-mod-carrierwave/abstract/attachment/storage_type.rb', line 163

def validate_temporary_storage_type_change new_storage_type=nil
  new_storage_type ||= @new_storage_type
  return unless new_storage_type
  unless known_storage_type? new_storage_type
    raise Error, tr(:unknown_storage_type, new_storage_type: new_storage_type)
  end

  if new_storage_type == :coded && codename.blank?
    raise Error, "codename needed for storage type :coded"
  end
end

#web?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'card/tmpsets/set/mod014-card-mod-carrierwave/abstract/attachment/storage_type.rb', line 44

def web?
  storage_type == :web
end

#will_be_stored_asObject



32
33
34
# File 'card/tmpsets/set/mod014-card-mod-carrierwave/abstract/attachment/storage_type.rb', line 32

def will_be_stored_as
  @new_storage_type || storage_type
end

#with_storage_options(opts = {}) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'card/tmpsets/set/mod014-card-mod-carrierwave/abstract/attachment/storage_type.rb', line 142

def with_storage_options opts={}
  old_values = {}
  validate_temporary_storage_type_change opts[:storage_type]
  %i[storage_type mod bucket].each do |opt_name|
    next unless opts[opt_name]
    old_values[opt_name] = instance_variable_get "@#{opt_name}"
    instance_variable_set "@#{opt_name}", opts[opt_name]
    @temp_storage_type = true
  end
  yield
ensure
  @temp_storage_type = false
  old_values.each do |key, val|
    instance_variable_set "@#{key}", val
  end
end