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.



15
16
17
# File 'lib/bulldog/attachment/maybe.rb', line 15

def name
  @name
end

#recordObject (readonly)

Returns the value of attribute record.



15
16
17
# File 'lib/bulldog/attachment/maybe.rb', line 15

def record
  @record
end

#saved=(value) ⇒ Object (writeonly)

Set the saved flag.



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

def saved=(value)
  @saved = value
end

#streamObject (readonly)

Returns the value of attribute stream.



15
16
17
# File 'lib/bulldog/attachment/maybe.rb', line 15

def stream
  @stream
end

#valueObject (readonly)

Returns the value of attribute value.



15
16
17
# File 'lib/bulldog/attachment/maybe.rb', line 15

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.



55
56
57
58
# File 'lib/bulldog/attachment/maybe.rb', line 55

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.



41
42
43
44
45
46
47
# File 'lib/bulldog/attachment/maybe.rb', line 41

def ==(other)
  other.is_a?(Bulldog::Attachment::Maybe) &&
    record == other.record &&
    name == other.name &&
    value == other.value &&
    saved? == other.saved?
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).



152
153
154
155
156
157
158
159
160
# File 'lib/bulldog/attachment/maybe.rb', line 152

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).



170
171
172
173
174
175
176
177
178
# File 'lib/bulldog/attachment/maybe.rb', line 170

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.



75
76
77
78
79
80
81
82
# File 'lib/bulldog/attachment/maybe.rb', line 75

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.



129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/bulldog/attachment/maybe.rb', line 129

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.



33
34
35
# File 'lib/bulldog/attachment/maybe.rb', line 33

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


119
120
121
122
123
# File 'lib/bulldog/attachment/maybe.rb', line 119

def reload
  unload
  stream.reload
  load
end

#saved?Boolean

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

Returns:

  • (Boolean)


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

def saved?
  @saved
end

#typeObject

Return the class’ attachment type.



65
66
67
# File 'lib/bulldog/attachment/maybe.rb', line 65

def type
  self.class.attachment_type
end

#unloadObject

Unload the attachment.

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



90
91
92
93
94
95
96
97
98
99
# File 'lib/bulldog/attachment/maybe.rb', line 90

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