Module: FileModel

Includes:
MiniMagic
Defined in:
lib/file_model.rb,
lib/file_model/file_model.rb

Defined Under Namespace

Modules: ClassMethods, Helper, MiniMagic Classes: Adapter, Version

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from MiniMagic

#mini_magic

Class Attribute Details

.box(name) ⇒ Object

Returns the value of attribute box.



109
110
111
# File 'lib/file_model/file_model.rb', line 109

def box name
  raise "Override this method to provide Your own custom box initialization."
end

Instance Attribute Details

#modelObject

Returns the value of attribute model.



3
4
5
# File 'lib/file_model/file_model.rb', line 3

def model
  @model
end

#nameObject

Returns the value of attribute name.



2
3
4
# File 'lib/file_model/file_model.rb', line 2

def name
  @name
end

#originalObject

Returns the value of attribute original.



9
10
11
# File 'lib/file_model/file_model.rb', line 9

def original
  @original
end

Instance Method Details

#build_name(name) ⇒ Object



92
93
94
# File 'lib/file_model/file_model.rb', line 92

def build_name name
  name
end

#build_standard_path(name, version = nil) ⇒ Object Also known as: build_path



80
81
82
83
# File 'lib/file_model/file_model.rb', line 80

def build_standard_path name, version = nil
  base, extension = name.rsplit('.', 2)
  %(/#{base}#{".#{version}" if version}#{".#{extension}" if extension})
end

#build_standard_url(name, version = nil) ⇒ Object Also known as: build_url



86
87
88
89
# File 'lib/file_model/file_model.rb', line 86

def build_standard_url name, version = nil
  base, extension = name.rsplit('.', 2)
  %(/#{base}#{".#{version}" if version}#{".#{extension}" if extension})
end

#delete(options = {}) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/file_model/file_model.rb', line 48

def delete options = {}
  return true unless name

  return false unless options[:validate] == false or valid?

  path = build_path name
  self.class.box[path].delete

  self.class.versions.each do |version_name, klass|
    path = build_path name, version_name
    self.class.box[path].delete
  end

  true
end

#delete!(options = {}) ⇒ Object



64
65
66
# File 'lib/file_model/file_model.rb', line 64

def delete! options = {}
  delete(options) || raise("can't delete #{self} (#{errors.inspect})!")
end

#errorsObject



96
97
98
# File 'lib/file_model/file_model.rb', line 96

def errors
  @errors ||= []
end

#fileObject



72
73
74
# File 'lib/file_model/file_model.rb', line 72

def file
  name && self.class.box[build_path(name)]
end

#process(&block) ⇒ Object



76
77
78
# File 'lib/file_model/file_model.rb', line 76

def process &block
  block.call original if original
end

#read(name) ⇒ Object



5
6
7
# File 'lib/file_model/file_model.rb', line 5

def read name
  self.name = name
end

#run_validationsObject



106
# File 'lib/file_model/file_model.rb', line 106

def run_validations; end

#save(options = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/file_model/file_model.rb', line 16

def save options = {}
  return true unless original

  # validating
  return false unless options[:validate] == false or valid?

  # deleting old files
  delete validate: false

  # saving
  self.name = build_name original.name

  process do |file|
    path = build_path name
    file.copy_to self.class.box[path]
  end

  self.class.versions.each do |version_name, klass|
    version = send version_name
    version.process do |file|
      path = build_path name, version_name
      file.copy_to self.class.box[path]
    end
  end

  true
end

#save!(options = {}) ⇒ Object



44
45
46
# File 'lib/file_model/file_model.rb', line 44

def save! options = {}
  save(options) || raise("can't save #{self} (#{errors.inspect})!")
end

#urlObject



68
69
70
# File 'lib/file_model/file_model.rb', line 68

def url
  name && build_url(name)
end

#valid?Boolean

Returns:

  • (Boolean)


100
101
102
103
104
# File 'lib/file_model/file_model.rb', line 100

def valid?
  errors.clear
  run_validations
  errors.empty?
end