Class: Bulldog::Attachment::Maybe

Inherits:
Object
  • Object
show all
Defined in:
lib/bulldog/attachment/maybe.rb

Overview

Abstract base class of None and Base.

Direct Known Subclasses

Base, None

Defined Under Namespace

Classes: StorableAttribute

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(record, name, stream) ⇒ Maybe

Returns a new instance of Maybe.



7
8
9
10
11
12
13
# File 'lib/bulldog/attachment/maybe.rb', line 7

def initialize(record, name, stream)
  @record = record
  @name = name
  @stream = stream
  @value = stream && stream.target
  @saved = @value.is_a?(SavedFile) || @value.is_a?(MissingFile)
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



22
23
24
# File 'lib/bulldog/attachment/maybe.rb', line 22

def name
  @name
end

#recordObject (readonly)

Returns the value of attribute record.



22
23
24
# File 'lib/bulldog/attachment/maybe.rb', line 22

def record
  @record
end

#saved=(value) ⇒ Object (writeonly)

Set the saved flag.



35
36
37
# File 'lib/bulldog/attachment/maybe.rb', line 35

def saved=(value)
  @saved = value
end

#streamObject (readonly)

Returns the value of attribute stream.



22
23
24
# File 'lib/bulldog/attachment/maybe.rb', line 22

def stream
  @stream
end

#valueObject (readonly)

Returns the value of attribute value.



22
23
24
# File 'lib/bulldog/attachment/maybe.rb', line 22

def value
  @value
end

Class Method Details

.handle(type) ⇒ Object

Set the attachment type for this class.

This will register it as the type of attachment to use for the given attachment type.



62
63
64
65
# File 'lib/bulldog/attachment/maybe.rb', line 62

def self.handle(type)
  self.attachment_type = type
  Attachment.types_to_classes[type] = self
end

Instance Method Details

#==(other) ⇒ Object

Return true if this object wraps the same IO, and is in the same state as the given Attachment.



48
49
50
51
52
53
54
# File 'lib/bulldog/attachment/maybe.rb', line 48

def ==(other)
  other.is_a?(Bulldog::Attachment::Maybe) &&
    record == other.record &&
    name == other.name &&
    value == other.value &&
    saved? == other.saved?
end

#copy_for(record) ⇒ Object

Return a copy of this attachment for the given record.



18
19
20
# File 'lib/bulldog/attachment/maybe.rb', line 18

def copy_for(record)
  self.class.new(record, name, stream)
end

#interpolate_path(style_name, params = {}) ⇒ Object

Return the path that the given style would be stored at.

Unlike #path, this is not affected by whether or not the record is saved. It may depend on the attachment value, however, as some interpolations may be derived from the value assigned to the attachment (e.g., :extension).



159
160
161
162
163
164
165
166
167
# File 'lib/bulldog/attachment/maybe.rb', line 159

def interpolate_path(style_name, params={})
  template = reflection.path_template
  style = reflection.styles[style_name]
  if template.is_a?(Symbol)
    record.send(template, name, style)
  else
    Interpolation.interpolate(template, record, name, style, params)
  end
end

#interpolate_url(style_name, params = {}) ⇒ Object

Return the URL that the given style would be found at.

Unlike #url, this is not affected by whether or not the record is saved. It may be depend on the attachment value, however, as some interpolations may be derived from the value assigned to the attachment (e.g., :extension).



177
178
179
180
181
182
183
184
185
# File 'lib/bulldog/attachment/maybe.rb', line 177

def interpolate_url(style_name, params={})
  template = reflection.url_template
  style = reflection.styles[style_name]
  if template.is_a?(Symbol)
    record.send(template, name, style)
  else
    Interpolation.interpolate(template, record, name, style, params)
  end
end

#loadObject

Load the attachment.

Called just after initializing the attachment, or after a reload on the new attachment.



82
83
84
85
86
87
88
89
# File 'lib/bulldog/attachment/maybe.rb', line 82

def load
  storable_attributes.each do |name, storable_attribute|
    if (column_name = reflection.column_name_for_stored_attribute(name))
      value = storable_attribute.value_for(self, :original)
      record.send("#{column_name}=", value)
    end
  end
end

#read_storable_attributesObject

Set the stored attributes in the attachment from the values in the record.



136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/bulldog/attachment/maybe.rb', line 136

def read_storable_attributes
  storable_attributes.each do |name, storable_attribute|
    if (column_name = reflection.column_name_for_stored_attribute(name))
      value = record.send(column_name)
      value = send("deserialize_#{name}", value) if storable_attribute.cast
      if storable_attribute.per_style?
        ivar = :"@original_#{name}"
      else
        ivar = :"@#{name}"
      end
      instance_variable_set(ivar, value)
    end
  end
end

#reflectionObject

Return the reflection for the attachment.



40
41
42
# File 'lib/bulldog/attachment/maybe.rb', line 40

def reflection
  @reflection ||= record.attachment_reflection_for(name)
end

#reloadObject

Refresh the attachment. This includes anything read from the file, such as stored attributes, and dimensions.

Use this to update the record if the underlying file has been modified outside of Bulldog. Note that the file has to be initialized from a saved file for this to work.

Example:

article = Article.create(:photo => photo_file)
article = Article.find(article.id)

# ... file is modified by an outside force ...

article.photo.reload
article.photo.dimensions  # now reflects the new dimensions


126
127
128
129
130
# File 'lib/bulldog/attachment/maybe.rb', line 126

def reload
  unload
  stream.reload
  load
end

#saved?Boolean

Return true if the original file for this attachment has been saved.

Returns:

  • (Boolean)


28
29
30
# File 'lib/bulldog/attachment/maybe.rb', line 28

def saved?
  @saved
end

#typeObject

Return the class’ attachment type.



72
73
74
# File 'lib/bulldog/attachment/maybe.rb', line 72

def type
  self.class.attachment_type
end

#unloadObject

Unload the attachment.

Called before a reload on the old attachment, or before a destroy.



97
98
99
100
101
102
103
104
105
106
# File 'lib/bulldog/attachment/maybe.rb', line 97

def unload
  storable_attributes.each do |name, storable_attribute|
    if (column_name = reflection.column_name_for_stored_attribute(name))
      record.send("#{column_name}=", nil)
    end
    if storable_attribute.memoize?
      send("memoized_#{name}").clear
    end
  end
end