Module: BindUrl

Defined in:
lib/bind_url.rb,
lib/bind_url/binder.rb,
lib/bind_url/version.rb

Defined Under Namespace

Classes: BindUrlConfig, Binder, BucketConfig, Error

Constant Summary collapse

VERSION =
"0.1.2.5".freeze

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.default_binder_classObject



88
89
90
91
# File 'lib/bind_url.rb', line 88

def default_binder_class
  raise "you need overwrite BindUrl.default_binder_class" if @default_binder_class.nil?
  @default_binder_class
end

.storage_configsObject

Returns the value of attribute storage_configs.



79
80
81
# File 'lib/bind_url.rb', line 79

def storage_configs
  @storage_configs
end

Class Method Details

.configure(storage = :default) {|config| ... } ⇒ Object

Yields:

  • (config)


82
83
84
85
86
# File 'lib/bind_url.rb', line 82

def configure(storage = :default)
  config = BindUrl::BucketConfig.new
  yield config
  storage_configs[storage] = config
end

Instance Method Details

#bind_url(attr, binder_class: BindUrl.default_binder_class, private: false) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/bind_url.rb', line 9

def bind_url(attr, binder_class: BindUrl.default_binder_class, private: false)
  bind_url_configs[attr.to_sym] = BindUrlConfig.new(binder_class: binder_class, private: private)

  self.class_eval <<-RUBY, __FILE__, __LINE__ + 1
    def #{attr}_url(params = {})
      v = self.send(:#{attr})
      return nil unless v
      #{attr}_binder.gen_url(v, params)
    end

    def #{attr}_url=(url)
      self.send("#{attr}=", #{attr}_binder.upload_via_url(url))
    end

    def #{attr}_file=(file)
      self.send("#{attr}=", #{attr}_binder.upload_via_file(file))
    end

    def #{attr}_binder
      config = self.class.bind_url_configs[:#{attr}]
      config.binder_class.new(model: self, attr: :#{attr}, private: config.private)
    end
  RUBY
end

#bind_url_configsObject



74
75
76
# File 'lib/bind_url.rb', line 74

def bind_url_configs
  @bind_url_configs ||= {}
end

#bind_urls(attr, binder_class: BindUrl.default_binder_class, private: false) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/bind_url.rb', line 34

def bind_urls(attr, binder_class: BindUrl.default_binder_class, private: false)
  bind_url_configs[attr.to_sym] = BindUrlConfig.new(binder_class: binder_class, private: private)

  signle_attr = attr.to_s.singularize.to_sym
  self.class_eval <<-RUBY, __FILE__, __LINE__ + 1
    def #{signle_attr}_urls(params = {})
      vs = self.send(:#{attr})
      return [] if vs.nil?
      vs.map { |v| #{attr}_binder.gen_url(v, params) }.freeze
    end

    def #{signle_attr}_urls=(urls)
      vs = urls.map { |url| #{attr}_binder.upload_via_url(url) }
      self.send("#{attr}=", vs)
    end

    def #{signle_attr}_files=(files)
      vs = files.map { |f| #{attr}_binder.upload_via_file(f) }
      self.send("#{attr}=", vs)
    end

    def upload_#{signle_attr}_url(index, url)
      vs = self.send("#{attr}") || []
      vs[index] = #{attr}_binder.upload_via_url(url)
      self.send("#{attr}=", vs)
    end

    def upload_#{signle_attr}_file(index, file)
      vs = self.send("#{attr}") || []
      vs[index] = #{attr}_binder.upload_via_file(file)
      self.send("#{attr}=", vs)
    end

    def #{attr}_binder
      config = self.class.bind_url_configs[:#{attr}]
      config.binder_class.new(model: self, attr: :#{attr}, private: config.private)
    end
  RUBY
end