Class: Attached::Attatcher
- Inherits:
-
Object
- Object
- Attached::Attatcher
- Defined in:
- lib/attached/attatcher.rb
Instance Attribute Summary collapse
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Class Method Summary collapse
Instance Method Summary collapse
-
#define ⇒ Object
Define the model methods required to run.
-
#destroying ⇒ Object
Define the destroying callbacks.
-
#flusher ⇒ Object
Flush validations.
-
#getters ⇒ Object
Define the getter.
-
#initialize(klass, name, options = {}) ⇒ Attatcher
constructor
A new instance of Attatcher.
-
#query ⇒ Object
Define the query.
-
#saving ⇒ Object
Define the saving callbacks.
-
#setters ⇒ Object
Define the setter.
-
#url ⇒ Object
Define the URL.
-
#validations ⇒ Object
Forward validations.
Constructor Details
#initialize(klass, name, options = {}) ⇒ Attatcher
Returns a new instance of Attatcher.
9 10 11 12 13 |
# File 'lib/attached/attatcher.rb', line 9 def initialize(klass, name, = {}) @klass = klass @name = name @options = end |
Instance Attribute Details
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
5 6 7 |
# File 'lib/attached/attatcher.rb', line 5 def klass @klass end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/attached/attatcher.rb', line 6 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
7 8 9 |
# File 'lib/attached/attatcher.rb', line 7 def @options end |
Class Method Details
.define(klass, name, options = {}) ⇒ Object
15 16 17 |
# File 'lib/attached/attatcher.rb', line 15 def self.define(klass, name, = {}) new(klass, name, ).define end |
Instance Method Details
#define ⇒ Object
Define the model methods required to run.
Usage:
attacher.define
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/attached/attatcher.rb', line 25 def define saving getters setters query url validations flusher end |
#destroying ⇒ Object
Define the destroying callbacks.
Usage:
attacher.
56 57 58 59 60 61 62 |
# File 'lib/attached/attatcher.rb', line 56 def name = @name @klass.send(:before_destroy) do logger.info "[attached] destroying #{name}" send(name).destroy end end |
#flusher ⇒ Object
Flush validations.
Usage:
attacher.flusher
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/attached/attatcher.rb', line 146 def flusher name = @name [:before_validation,:after_validation].each do |operation| @klass.send(operation) do %w(size extension identifier).each do |attribute| if errors.include?(:"#{name}_#{attribute}") errors[:"#{name}_#{attribute}"].each do || errors.add(name, ) end errors[:"#{name}_#{attribute}"].clear end end end end end |
#getters ⇒ Object
Define the getter.
Usage:
attacher.getters
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/attached/attatcher.rb', line 70 def getters name = @name = @options @klass.send(:define_method, name) do ivar = :"@_attachment_#{name}" = instance_variable_get(ivar) if .nil? = Attachment.new(name, self, ) instance_variable_set(ivar, ) end return end end |
#query ⇒ Object
Define the query.
Usage:
attacher.getters
105 106 107 108 109 110 |
# File 'lib/attached/attatcher.rb', line 105 def query name = @name @klass.send(:define_method, "#{name}?") do send(name).attached? end end |
#saving ⇒ Object
Define the saving callbacks.
Usage:
attacher.saving
42 43 44 45 46 47 48 |
# File 'lib/attached/attatcher.rb', line 42 def saving name = @name @klass.send(:before_save) do logger.info "[attached] saving #{name}" send(name).save end end |
#setters ⇒ Object
Define the setter.
Usage:
attacher.setters
92 93 94 95 96 97 |
# File 'lib/attached/attatcher.rb', line 92 def setters name = @name @klass.send(:define_method, "#{name}=") do |file| send(name).assign(file) end end |
#url ⇒ Object
Define the URL.
Usage:
attacher.getters
118 119 120 121 122 123 |
# File 'lib/attached/attatcher.rb', line 118 def url name = @name @klass.send(:define_method, "#{name}_url=") do |url| send(name).url = url end end |
#validations ⇒ Object
Forward validations.
Usage:
attacher.validations
131 132 133 134 135 136 137 138 |
# File 'lib/attached/attatcher.rb', line 131 def validations name = @name @klass.send(:validates_each, name) do |record, attr, value| record.send(name).errors.each do |error| record.errors.add(name, error) end end end |