Module: Card::Set::AdvancedApi

Included in:
Card::Set
Defined in:
lib/card/set/advanced_api.rb

Overview

advanced set module API

Instance Method Summary collapse

Instance Method Details

#attachment(name, args) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/card/set/advanced_api.rb', line 22

def attachment name, args
  include_set Abstract::Attachment
  add_attributes name, "remote_#{name}_url".to_sym,
                 :action_id_of_cached_upload, :empty_ok,
                 :storage_type, :bucket, :mod
  uploader_class = args[:uploader] || ::CarrierWave::FileCardUploader
  mount_uploader name, uploader_class
end

#ensure_set(&block) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/card/set/advanced_api.rb', line 5

def ensure_set &block
  set_module = yield
rescue NameError => e
  if e.message =~ /uninitialized constant (?:Card::Set::)?(.+)$/
    constant_pieces = Regexp.last_match(1).split("::")
    constant_pieces.inject(Card::Set) do |set_mod, module_name|
      set_mod.const_get_or_set module_name do
        Module.new
      end
    end
  end
  # try again - there might be another submodule that doesn't exist
  ensure_set(&block)
else
  set_module.extend Card::Set
end

#stage_method(method, opts = {}, &block) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/card/set/advanced_api.rb', line 31

def stage_method method, opts={}, &block
  class_eval do
    define_method "_#{method}", &block
    define_method method do |*args|
      if (error = wrong_stage(opts) || wrong_action(opts[:on]))
        raise Card::Error, error
      else
        send "_#{method}", *args
      end
    end
  end
end