Module: Sinatra::Bootstrap::Helpers

Defined in:
lib/sinatra/bootstrap/helpers.rb

Instance Method Summary collapse

Instance Method Details

#cols(tag, attrb = {}, &block) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/sinatra/bootstrap/helpers.rb', line 5

def cols(tag, attrb = {}, &block)
  cols = attrb[:cols] || ''
  offset = attrb[:offset] || ''
  extr_class = attrb[:class].nil? ? '' : " #{attrb[:class]}"

  attrb.delete :cols
  attrb.delete :offset
  attrb.delete :class

  extr_attrb = ''
  attrb.each do |key, value|
    extr_attrb << " #{key}=\"#{value}\""
  end
  
  #validate input with regex
  unless /^([abcd]{1}\d+){1,}$/ === cols || /^([abcd]{1}\d+){1,}$/ === offset then return '' end

  #set invalid to empty
  unless /^([abcd]{1}\d+){1,}$/ === cols then cols = '' end
  unless /^([abcd]{1}\d+){1,}$/ === offset then offset = '' end

  values = { cols: [], offset: [] }

  /a{1}(\d+)/.match(cols) do |m|
    values[:cols] << ['xs', m.captures.first]
  end
  /b{1}(\d+)/.match(cols) do |m|
    values[:cols] << ['sm', m.captures.first]
  end
  /c{1}(\d+)/.match(cols) do |m|
    values[:cols] << ['md', m.captures.first]
  end
  /d{1}(\d+)/.match(cols) do |m|
    values[:cols] << ['lg', m.captures.first]
  end

  /a{1}(\d+)/.match(offset) do |m|
    values[:offset] << ['xs', m.captures.first]
  end
  /b{1}(\d+)/.match(offset) do |m|
    values[:offset] << ['sm', m.captures.first]
  end
  /c{1}(\d+)/.match(offset) do |m|
    values[:offset] << ['md', m.captures.first]
  end
  /d{1}(\d+)/.match(offset) do |m|
    values[:offset] << ['lg', m.captures.first]
  end
  #e.g. values now = { cols: [["xs", "2"], ["sm", "3"]], offset: [["sm", "3"], ["md", "3"]] }

  classes = ''
  values[:cols].each do |type, number|
    classes << "col-#{type}-#{number} "
  end

  values[:offset].each do |type, number|
    classes << "col-#{type}-offset-#{number} "
  end
  classes.strip!

  concat_content "<#{tag} class=\"#{classes}#{extr_class}\"#{extr_attrb}>"
  concat_content "#{ capture_html &block }</#{tag}>"
end

#container(tag, attrb = {}, &block) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/sinatra/bootstrap/helpers.rb', line 69

def container(tag, attrb = {}, &block)
  extr_class = attrb[:class].nil? ? '' : " #{attrb[:class]}"

  attrb.delete :class

  extr_attrb = ''
  attrb.each do |key, value|
    extr_attrb << " #{key}=\"#{value}\""
  end

  concat_content "<#{tag} class=\"container#{extr_class}\"#{extr_attrb}>"
  concat_content "#{ capture_html &block }</#{tag}>"
end

#row(tag, attrb = {}, &block) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/sinatra/bootstrap/helpers.rb', line 83

def row(tag, attrb = {}, &block)
  extr_class = attrb[:class].nil? ? '' : " #{attrb[:class]}"

  attrb.delete :class

  extr_attrb = ''
  attrb.each do |key, value|
    extr_attrb << " #{key}=\"#{value}\""
  end

  concat_content "<#{tag} class=\"row#{extr_class}\"#{extr_attrb}>"
  concat_content "#{ capture_html &block }</#{tag}>"
end