Module: Bulldog::Attachment::HasDimensions
Overview
Module for dealing with dimensions.
Note that due to the way stored attributes are implemented, this module must be included after the definition of #dimensions.
Class Method Summary collapse
Instance Method Summary collapse
-
#aspect_ratio(style_name = nil) ⇒ Object
Return the aspect ratio of the named style.
-
#dimensions(style_name = nil) ⇒ Object
Return the width and height of the named style, as a 2-element array.
-
#height(style_name = nil) ⇒ Object
Return the height of the named style.
-
#width(style_name = nil) ⇒ Object
Return the width of the named style.
Class Method Details
.included(base) ⇒ Object
10 11 12 13 14 15 16 |
# File 'lib/bulldog/attachment/has_dimensions.rb', line 10 def self.included(base) super base.class_eval do storable_attribute :width , :per_style => true, :memoize => true storable_attribute :height, :per_style => true, :memoize => true end end |
Instance Method Details
#aspect_ratio(style_name = nil) ⇒ Object
Return the aspect ratio of the named style.
style_name
defaults to the attribute’s #default_style.
51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/bulldog/attachment/has_dimensions.rb', line 51 def aspect_ratio(style_name=nil) style_name ||= reflection.default_style if style_name.equal?(:original) original_width = from_examination(:@original_width) original_height = from_examination(:@original_height) original_width.to_f / original_height else w, h = *dimensions(style_name) w.to_f / h end end |
#dimensions(style_name = nil) ⇒ Object
Return the width and height of the named style, as a 2-element array.
67 68 69 70 71 72 73 74 75 76 |
# File 'lib/bulldog/attachment/has_dimensions.rb', line 67 def dimensions(style_name=nil) style_name ||= reflection.default_style if style_name.equal?(:original) original_width = from_examination(:@original_width) original_height = from_examination(:@original_height) [original_width, original_height] else resize_dimensions(dimensions(:original), reflection.styles[style_name]) end end |
#height(style_name = nil) ⇒ Object
Return the height of the named style.
style_name
defaults to the attribute’s #default_style.
37 38 39 40 41 42 43 44 |
# File 'lib/bulldog/attachment/has_dimensions.rb', line 37 def height(style_name=nil) style_name ||= reflection.default_style if style_name.equal?(:original) from_examination :@original_height else dimensions(style_name).at(1) end end |
#width(style_name = nil) ⇒ Object
Return the width of the named style.
style_name
defaults to the attribute’s #default_style.
23 24 25 26 27 28 29 30 |
# File 'lib/bulldog/attachment/has_dimensions.rb', line 23 def width(style_name=nil) style_name ||= reflection.default_style if style_name.equal?(:original) from_examination :@original_width else dimensions(style_name).at(0) end end |