Class: Bread::Basket::Poster::ImageBox

Inherits:
Box
  • Object
show all
Defined in:
lib/bread/basket/poster/image_box.rb

Constant Summary

Constants included from UnitsHelper

UnitsHelper::DIMENSIONS, UnitsHelper::HASH_SELECTOR_REGEX, UnitsHelper::MEASUREMENT_REGEX, UnitsHelper::NUMERIC_REGEX

Instance Attribute Summary collapse

Attributes inherited from Box

#bottom, #content, #height, #layout, #left, #method_name, #pending, #right, #selector_name, #specs, #stretchy, #styles, #top, #unfinished, #width

Instance Method Summary collapse

Methods inherited from Box

#add_to_determined, #determine_missing, #fetch_names, #initialize, #ready_to_resolve?, #resolve_box, #resolve_dimension, #safe?, #stretchy?, #try_dimension

Methods included from UnitsHelper

#convert_inner_units, #convert_units

Constructor Details

This class inherits a constructor from Bread::Basket::Poster::Box

Instance Attribute Details

#box_heightObject

Returns the value of attribute box_height.



6
7
8
# File 'lib/bread/basket/poster/image_box.rb', line 6

def box_height
  @box_height
end

#box_widthObject

Returns the value of attribute box_width.



6
7
8
# File 'lib/bread/basket/poster/image_box.rb', line 6

def box_width
  @box_width
end

#im_heightObject

Returns the value of attribute im_height.



6
7
8
# File 'lib/bread/basket/poster/image_box.rb', line 6

def im_height
  @im_height
end

#im_widthObject

Returns the value of attribute im_width.



6
7
8
# File 'lib/bread/basket/poster/image_box.rb', line 6

def im_width
  @im_width
end

#sizeObject (readonly)

Returns the value of attribute size.



5
6
7
# File 'lib/bread/basket/poster/image_box.rb', line 5

def size
  @size
end

Instance Method Details

#box_ratioObject



69
70
71
# File 'lib/bread/basket/poster/image_box.rb', line 69

def box_ratio
  box_width / box_height.to_f
end

#find_width_heightObject



45
46
47
48
# File 'lib/bread/basket/poster/image_box.rb', line 45

def find_width_height
  specs['width'] ||= size[0]
  specs['height'] ||= size[1]
end

#fit_width_and_heightObject



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/bread/basket/poster/image_box.rb', line 57

def fit_width_and_height
  if im_ratio > box_ratio
    w = box_width
    h = box_width / im_ratio
  else
    h = box_height
    w = box_height * im_ratio
  end
  add_to_determined('im_width', w)
  add_to_determined('im_height', h)
end

#fitted_width_and_heightObject



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/bread/basket/poster/image_box.rb', line 32

def fitted_width_and_height
  # set width and height from size if missing from css
  find_width_height
  # store these values as specifying the container's size, not
  # the image's actual final size
  specs['box_width'] = specs['width']
  specs['box_height'] = specs['height']
  # now use css notation to make this dependent on its own fitting so
  # that other Dimensions Mechanics can handle this normally
  specs['width'] = "#{method_name}.im_width"
  specs['height'] = "#{method_name}.im_height"
end

#handle_width_and_heightObject



22
23
24
25
26
27
28
29
30
# File 'lib/bread/basket/poster/image_box.rb', line 22

def handle_width_and_height
  if specs['width'] || specs['height']
    # shuffle values around for fitting
    fitted_width_and_height
  else
    specs['width'] = size[0]
    specs['height'] = size[1]
  end
end

#im_ratioObject



73
74
75
# File 'lib/bread/basket/poster/image_box.rb', line 73

def im_ratio
  size[0] / size[1].to_f
end

#image_specsObject



14
15
16
17
18
19
20
# File 'lib/bread/basket/poster/image_box.rb', line 14

def image_specs
  image_path = Bread::Basket::Poster.dir_path + '/' + specs['src']
  @size = FastImage.size image_path
  read_fail(image_path) if size.nil?
  # TODO: Learn how printing resolution in images works with prawn
  # update: it's complicated!
end

#inspectObject



82
83
84
85
86
87
88
89
# File 'lib/bread/basket/poster/image_box.rb', line 82

def inspect
  str = ''
  %w(top left width height bottom right box_width box_height
     im_width im_height).each do |dim|
    str << "#{dim}: #{send(dim)}; "
  end
  str.strip
end

#read_fail(image_path) ⇒ Object



77
78
79
80
# File 'lib/bread/basket/poster/image_box.rb', line 77

def read_fail(image_path)
  message = "Couldn't find image for #{selector_name} at #{image_path}."
  layout.give_up(message)
end

#setup_dimensionsObject



7
8
9
10
11
12
# File 'lib/bread/basket/poster/image_box.rb', line 7

def setup_dimensions
  image_specs
  handle_width_and_height
  DimensionsHelper.new(self, layout, specs)
  layout.pending << selector_name unless pending.empty?
end

#try_to_resolveObject



50
51
52
53
54
55
# File 'lib/bread/basket/poster/image_box.rb', line 50

def try_to_resolve
  if box_width.is_a?(Numeric) && box_height.is_a?(Numeric)
    fit_width_and_height
  end
  super
end