Class: Bread::Basket::Poster::Box

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

Direct Known Subclasses

ImageBox

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(name, layout, specs = {}) ⇒ Box

Returns a new instance of Box.



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/bread/basket/poster/box.rb', line 15

def initialize(name, layout, specs = {})
  fetch_names(name)
  @layout = layout
  @specs = specs
  @styles = specs
  @pending = []
  @unfinished = []

  setup_dimensions
  layout.image_boxes << method_name if self.is_a? ImageBox
  layout.boxes << selector_name
end

Instance Attribute Details

#bottomObject

Returns the value of attribute bottom.



12
13
14
# File 'lib/bread/basket/poster/box.rb', line 12

def bottom
  @bottom
end

#contentObject

Returns the value of attribute content.



12
13
14
# File 'lib/bread/basket/poster/box.rb', line 12

def content
  @content
end

#heightObject

Returns the value of attribute height.



12
13
14
# File 'lib/bread/basket/poster/box.rb', line 12

def height
  @height
end

#layoutObject (readonly)

Box does most of the heavy lifting of the self-referential css so it’s a little confusing. It passes dimensions off to the dimensions helper and holds the methods for setting dimensions as either pending or determined and how to resolve those issues.



10
11
12
# File 'lib/bread/basket/poster/box.rb', line 10

def layout
  @layout
end

#leftObject

Returns the value of attribute left.



12
13
14
# File 'lib/bread/basket/poster/box.rb', line 12

def left
  @left
end

#method_nameObject (readonly)

Box does most of the heavy lifting of the self-referential css so it’s a little confusing. It passes dimensions off to the dimensions helper and holds the methods for setting dimensions as either pending or determined and how to resolve those issues.



10
11
12
# File 'lib/bread/basket/poster/box.rb', line 10

def method_name
  @method_name
end

#pendingObject (readonly)

Box does most of the heavy lifting of the self-referential css so it’s a little confusing. It passes dimensions off to the dimensions helper and holds the methods for setting dimensions as either pending or determined and how to resolve those issues.



10
11
12
# File 'lib/bread/basket/poster/box.rb', line 10

def pending
  @pending
end

#rightObject

Returns the value of attribute right.



12
13
14
# File 'lib/bread/basket/poster/box.rb', line 12

def right
  @right
end

#selector_nameObject (readonly)

Box does most of the heavy lifting of the self-referential css so it’s a little confusing. It passes dimensions off to the dimensions helper and holds the methods for setting dimensions as either pending or determined and how to resolve those issues.



10
11
12
# File 'lib/bread/basket/poster/box.rb', line 10

def selector_name
  @selector_name
end

#specsObject (readonly)

Box does most of the heavy lifting of the self-referential css so it’s a little confusing. It passes dimensions off to the dimensions helper and holds the methods for setting dimensions as either pending or determined and how to resolve those issues.



10
11
12
# File 'lib/bread/basket/poster/box.rb', line 10

def specs
  @specs
end

#stretchyObject

Returns the value of attribute stretchy.



12
13
14
# File 'lib/bread/basket/poster/box.rb', line 12

def stretchy
  @stretchy
end

#stylesObject (readonly)

Box does most of the heavy lifting of the self-referential css so it’s a little confusing. It passes dimensions off to the dimensions helper and holds the methods for setting dimensions as either pending or determined and how to resolve those issues.



10
11
12
# File 'lib/bread/basket/poster/box.rb', line 10

def styles
  @styles
end

#topObject

Returns the value of attribute top.



12
13
14
# File 'lib/bread/basket/poster/box.rb', line 12

def top
  @top
end

#unfinishedObject (readonly)

Box does most of the heavy lifting of the self-referential css so it’s a little confusing. It passes dimensions off to the dimensions helper and holds the methods for setting dimensions as either pending or determined and how to resolve those issues.



10
11
12
# File 'lib/bread/basket/poster/box.rb', line 10

def unfinished
  @unfinished
end

#widthObject

Returns the value of attribute width.



12
13
14
# File 'lib/bread/basket/poster/box.rb', line 12

def width
  @width
end

Instance Method Details

#add_to_determined(dimension_name, value) ⇒ Object



75
76
77
78
79
# File 'lib/bread/basket/poster/box.rb', line 75

def add_to_determined(dimension_name, value)
  name = method_name + '.' + dimension_name
  instance_variable_set "@#{dimension_name}", value
  layout.determined[name] = value
end

#determine_missing(missing_dimension) ⇒ Object

Rubocop hates determine_missing rubocop:disable all



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/bread/basket/poster/box.rb', line 87

def determine_missing(missing_dimension)
  value = case missing_dimension
          when 'left'
            right - width
          when 'right'
            left + width
          when 'width'
            right - left
          when 'top'
            bottom + height
          when 'height'
            top - bottom
          when 'bottom'
            top - height
          end
  add_to_determined missing_dimension, value
end

#fetch_names(name) ⇒ Object



28
29
30
31
# File 'lib/bread/basket/poster/box.rb', line 28

def fetch_names(name)
  @selector_name = name
  @method_name = name.sub('.', '').gsub('-', '_')
end

#inspectObject

rubocop:enable all



105
106
107
108
109
110
111
# File 'lib/bread/basket/poster/box.rb', line 105

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

#ready_to_resolve?(hash) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/bread/basket/poster/box.rb', line 65

def ready_to_resolve?(hash)
  hash[:pending].empty? && safe?(hash[:command])
end

#resolve_boxObject



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

def resolve_box
  layout.pending.delete selector_name
  unfinished.each { |dimension| determine_missing dimension }
end

#resolve_dimension(dimension, hash) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/bread/basket/poster/box.rb', line 56

def resolve_dimension(dimension, hash)
  command_string = hash[:command].join(' ')
  value = eval command_string
  instance_variable_set "@#{dimension}", value
  pending.delete dimension
  add_to_determined dimension, value
  resolve_box if pending.empty?
end

#safe?(command_array) ⇒ Boolean

Returns:

  • (Boolean)


69
70
71
72
73
# File 'lib/bread/basket/poster/box.rb', line 69

def safe?(command_array)
  command_array.all? do |elem|
    (elem.is_a? Numeric) || ([:+, :-, :/, :*, '(', ')'].include?(elem))
  end
end

#setup_dimensionsObject



33
34
35
36
# File 'lib/bread/basket/poster/box.rb', line 33

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

#stretchy?Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/bread/basket/poster/box.rb', line 113

def stretchy?
  !stretchy.nil?
end

#try_dimension(dimensions_hash) ⇒ Object



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

def try_dimension(dimensions_hash)
  dimensions_hash[:pending].delete_if do |dimension_key, index|
    value = layout.determined[dimension_key]
    if value && value != :stretchy
      command_arr = dimensions_hash[:command]
      command_arr[index] = value
    end
  end
end

#try_to_resolveObject



38
39
40
41
42
43
44
# File 'lib/bread/basket/poster/box.rb', line 38

def try_to_resolve
  pending.each do |dimension|
    hash = send dimension
    try_dimension hash
    resolve_dimension(dimension, hash) if ready_to_resolve? hash
  end
end