Class: Bread::Basket::Poster::Layout

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(metadata) ⇒ Layout

Returns a new instance of Layout.



10
11
12
13
14
15
16
# File 'lib/bread/basket/poster/layout.rb', line 10

def initialize()
   = 
  @type = determine_type
  @stylesheet = find_stylesheet(['stylesheet'])
  @css_reader = CSSReader.new(stylesheet, self)
  css_reader.do_your_thing!
end

Instance Attribute Details

#bottomObject

Returns the value of attribute bottom.



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

def bottom
  @bottom
end

#boxesObject

Returns the value of attribute boxes.



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

def boxes
  @boxes
end

#css_readerObject (readonly)

Returns the value of attribute css_reader.



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

def css_reader
  @css_reader
end

#determinedObject

Returns the value of attribute determined.



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

def determined
  @determined
end

#font_familyObject

Returns the value of attribute font_family.



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

def font_family
  @font_family
end

#font_sizeObject

Returns the value of attribute font_size.



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

def font_size
  @font_size
end

#heightObject

Returns the value of attribute height.



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

def height
  @height
end

#image_boxesObject

Returns the value of attribute image_boxes.



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

def image_boxes
  @image_boxes
end

#leftObject

Returns the value of attribute left.



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

def left
  @left
end

#marginObject

Returns the value of attribute margin.



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

def margin
  @margin
end

#metadataObject (readonly)

Returns the value of attribute metadata.



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

def 
  
end

#pendingObject

Returns the value of attribute pending.



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

def pending
  @pending
end

#rightObject

Returns the value of attribute right.



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

def right
  @right
end

#stylesheetObject (readonly)

Returns the value of attribute stylesheet.



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

def stylesheet
  @stylesheet
end

#topObject

Returns the value of attribute top.



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

def top
  @top
end

#typeObject (readonly)

Returns the value of attribute type.



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

def type
  @type
end

#widthObject

Returns the value of attribute width.



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

def width
  @width
end

Instance Method Details

#create_attribute(key, value) ⇒ Object



59
60
61
62
63
64
# File 'lib/bread/basket/poster/layout.rb', line 59

def create_attribute(key, value)
  new_key = key.gsub('-', '_').sub('.', '').to_sym
  define_singleton_method(new_key) { value }
  # This is called at end of each attribute's definition
  try_to_resolve_pendings unless pending.nil?
end

#determine_typeObject



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/bread/basket/poster/layout.rb', line 18

def determine_type
  case ['layout']
  when 'block'
    :block
  when 'flow'
    :flow
  else
    handle_else ['layout']
    :flow
  end
end

#empty_defaultsObject



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

def empty_defaults
  @pending = []
  @determined = {}
  @boxes = []
  @image_boxes = []
end

#find_stylesheet(stylesheet_name) ⇒ Object



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

def find_stylesheet(stylesheet_name)
  if stylesheet_name
    path = Bread::Basket::Poster.dir_path + '/' + stylesheet_name
    path += '.css' unless stylesheet_name.include?('.css')
    path
  else
    puts 'Warning: no stylesheet given, using template instead.'
    template
  end
end

#flow?Boolean

Returns:

  • (Boolean)


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

def flow?
  type == :flow
end

#give_up(message) ⇒ Object



100
101
102
103
104
# File 'lib/bread/basket/poster/layout.rb', line 100

def give_up(message)
  puts '== Aborting =='
  puts message
  exit
end

#handle_defaultsObject



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/bread/basket/poster/layout.rb', line 66

def handle_defaults
  empty_defaults
  @font_size ||= 36
  @font_family ||= 'Helvetica'
  @margin ||= 36
  # add dimensions to the determined hash for reference
  # open to a better solution here :)
  %w(width height left right top bottom margin font_size
     font_family).each do |method_name|
    determined[method_name] = eval("@#{method_name}")
  end
end

#handle_else(layout) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/bread/basket/poster/layout.rb', line 51

def handle_else(layout)
  if layout
    puts "Warning: Unrecognized layout `#{layout}`, using flow instead"
  else
    puts 'Warning: No layout specified, defaulting to flow.'
  end
end

#templateObject



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

def template
  template = File.expand_path('./samples/block_sample.css')
  template = File.expand_path('./samples/flow_sample.css') if flow?
  template
end

#try_to_resolve_pendingsObject



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/bread/basket/poster/layout.rb', line 86

def try_to_resolve_pendings
  pending.reverse_each do |name|
    # very limited match on columns for now
    match = name.match(/columns\[(\d)\]/)
    if match
      index = match[1].to_i
      columns[index].try_to_resolve
    else
      box = send name.sub('.', '').gsub('-', '_')
      box.try_to_resolve
    end
  end
end