Class: Bread::Basket::Poster::DimensionsHelper

Inherits:
Object
  • Object
show all
Includes:
UnitsHelper
Defined in:
lib/bread/basket/poster/dimensions_helper.rb

Constant Summary

Constants included from UnitsHelper

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from UnitsHelper

#convert_inner_units, #convert_units

Constructor Details

#initialize(box, layout, dimensions) ⇒ DimensionsHelper

Returns a new instance of DimensionsHelper.



10
11
12
13
14
15
16
17
18
19
# File 'lib/bread/basket/poster/dimensions_helper.rb', line 10

def initialize(box, layout, dimensions)
  @layout = layout
  @box = box
  @dimensions = dimensions
  @checker = BoxChecker.new box, dimensions
  fail_dimensions unless checker.box_ok?
  determine_dimensions
  box.try_to_resolve
  frame_box
end

Instance Attribute Details

#boxObject (readonly)

Returns the value of attribute box.



8
9
10
# File 'lib/bread/basket/poster/dimensions_helper.rb', line 8

def box
  @box
end

#checkerObject (readonly)

Returns the value of attribute checker.



8
9
10
# File 'lib/bread/basket/poster/dimensions_helper.rb', line 8

def checker
  @checker
end

#dimensionsObject (readonly)

Returns the value of attribute dimensions.



8
9
10
# File 'lib/bread/basket/poster/dimensions_helper.rb', line 8

def dimensions
  @dimensions
end

#layoutObject (readonly)

Returns the value of attribute layout.



8
9
10
# File 'lib/bread/basket/poster/dimensions_helper.rb', line 8

def layout
  @layout
end

Instance Method Details

#add_numeric_dimension(dimension_name, value) ⇒ Object



41
42
43
44
# File 'lib/bread/basket/poster/dimensions_helper.rb', line 41

def add_numeric_dimension(dimension_name, value)
  box.instance_variable_set "@#{dimension_name}", value
  box.add_to_determined(dimension_name, value)
end

#command_array(command_string) ⇒ Object



54
55
56
57
# File 'lib/bread/basket/poster/dimensions_helper.rb', line 54

def command_array(command_string)
  commands = command_string.split(' ')
  commands.map { |command| convert_command(command) }
end

#convert_command(command) ⇒ Object



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

def convert_command(command)
  case command
  when '*', '+', '-', '/'
    command.to_sym
  when layout.css_reader.class::NUMERIC_REGEX
    Float command
  else
    command.gsub('-', '_')
  end
end

#create_pending_dimension(dimension_name, command_string) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/bread/basket/poster/dimensions_helper.rb', line 46

def create_pending_dimension(dimension_name, command_string)
  box.pending << dimension_name
  commands = command_array command_string
  pending = create_pending_hash commands
  return_hash = { pending: pending, command: commands }
  box.instance_variable_set "@#{dimension_name}", return_hash
end

#create_pending_hash(command_array) ⇒ Object



70
71
72
73
74
75
76
77
78
# File 'lib/bread/basket/poster/dimensions_helper.rb', line 70

def create_pending_hash(command_array)
  h = {}
  command_array.each_with_index do |command, index|
    if (command.is_a? String) && (command != '(') && (command != ')')
      h[command] = index
    end
  end
  h
end

#determine_dimensionsObject



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/bread/basket/poster/dimensions_helper.rb', line 29

def determine_dimensions
  dimensions.each do |key, value|
    next unless DIMENSIONS.include? key
    case value
    when String
      create_pending_dimension key, value
    when Numeric
      add_numeric_dimension key, value
    end
  end
end

#fail_dimensionsObject



21
22
23
24
25
26
27
# File 'lib/bread/basket/poster/dimensions_helper.rb', line 21

def fail_dimensions
  message = "For the selector #{box.selector_name}, you failed to " \
  'provide enough dimensions to draw a box. You need two of these three: ' \
  'left, right, width; and either `top` or `bottom and height`. Instead you ' \
  "provided #{dimensions}."
  layout.give_up message
end

#frame_boxObject



80
81
82
83
84
85
# File 'lib/bread/basket/poster/dimensions_helper.rb', line 80

def frame_box
  # from two of left, right, width, determine other dimension
  # bottom, height are optional unless top is missing
  left_right_width
  top_bottom_height
end

#left_right_widthObject



87
88
89
90
91
92
# File 'lib/bread/basket/poster/dimensions_helper.rb', line 87

def left_right_width
  given = checker.horizontal_dimensions
  difference = %w(left right width) - given
  missing = difference.empty? ? 'right' : difference[0]
  missing_dimension given, missing
end

#make_stretchyObject



115
116
117
118
119
# File 'lib/bread/basket/poster/dimensions_helper.rb', line 115

def make_stretchy
  box.add_to_determined 'bottom', :stretchy
  box.add_to_determined 'height', :stretchy
  box.stretchy = true
end

#missing_dimension(given, missing) ⇒ Object



107
108
109
110
111
112
113
# File 'lib/bread/basket/poster/dimensions_helper.rb', line 107

def missing_dimension(given, missing)
  if given.any? { |dim| box.pending.include? dim }
    box.unfinished << missing
  else
    box.determine_missing missing
  end
end

#top_bottom_heightObject



94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/bread/basket/poster/dimensions_helper.rb', line 94

def top_bottom_height
  given = checker.vertical_dimensions
  difference = %w(top bottom height) - given
  case difference.length
  when 0
    missing_dimension given, 'bottom'
  when 1
    missing_dimension given, difference[0]
  when 2
    make_stretchy
  end
end