Module: GridHelper

Defined in:
lib/css_grid.rb

Constant Summary collapse

TWELVE_STRING_INTS =
{ :one => 1, :two => 2, :three => 3, :four => 4, :five => 5, :six => 6, 
:seven => 7, :eight => 8, :nine => 9, :ten => 10, :eleven => 11, :twelve => 12 }
TWELVE_STRING_INTS_INVERT =
TWELVE_STRING_INTS.invert
TWELVE_FOR_REGEXP =
TWELVE_STRING_INTS.keys.join '|'

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
# File 'lib/css_grid.rb', line 115

def method_missing method_name, *args, &block
  case method_name.to_sym
  when /^(container|row|(#{ TWELVE_FOR_REGEXP })_span)$/
    self.grid($1.to_sym, *args, &block)
  when /^(#{ TWELVE_FOR_REGEXP })_cols?_container$/
    self.cols_container($1.to_sym, *args, &block)
  when /^(#{ TWELVE_FOR_REGEXP })_spans?_container$/
    self.spans_container($1.to_sym, *args, &block)
  else super
  end
end

Instance Method Details

#cols_container(col_number, options = {}, &block) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/css_grid.rb', line 54

def cols_container col_number, options = {}, &block
  options[:rows] ||= {}
  options[:spans] ||= {}
  
  if @nested_stack.present?
    options[:rows].merge!({:nested => true})
    options[:nested_width] ||= TWELVE_STRING_INTS[@nested_stack.last.to_sym]
  end
      
  disable = [*options.delete(:disable)]
  nested = [*options.delete(:nested)]
  
  options[:rows].merge!({:nested => true}) if nested.delete :container    
  
  collection_length = TWELVE_STRING_INTS[col_number]
  span_width = @span_width || TWELVE_STRING_INTS_INVERT[(options.delete(:nested_width) || 12) / (collection_length + (options[:spans][:prepend] || 0) + (options[:spans][:append] || 0))]
  
  rows = recollect(collection_length, options.delete(:collection) || [1]).map do |collection_mini|
    cols = collection_mini.map do |elt|

      if disable.include? :spans
        capture(elt, &block)
        
      else
        grid("#{ span_width }_span".to_sym, options[:spans].clone) do
          safe_buffer = capture(elt, &block)
          safe_buffer = grid(:row, :nested=>true){ safe_buffer } if nested.include? :spans
          
          safe_buffer
        end
      end
    end
    
    grid(:row, options[:rows].clone){ cols.reduce(:safe_concat) }
  end
  
  safe_buffer = rows.reduce(:safe_concat)
  safe_buffer = grid(:container, :id=>options.delete(:id), :class=>options.delete(:class)){ safe_buffer } unless disable.delete :container
  safe_buffer
end

#grid(tag, options = {}, &block) ⇒ Object



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
# File 'lib/css_grid.rb', line 18

def grid tag, options = {}, &block
  prepend = TWELVE_STRING_INTS_INVERT[options.delete :prepend]
  append = TWELVE_STRING_INTS_INVERT[options.delete :append]
  
  warn "WARNING : argument ':nested' is not supported for '#{ tag }'" if options[:nested].present? and tag != :row
  
  if tag =~ /(one|two|three|four|five|six|seven|eight|nine|ten|eleven|twelve)_span$/
      @nested_stack << $1
      
      unstack = true
  else
    warn "WARNING : argument ':prepend' is not supported for '#{ tag }'" if prepend.present?
    warn "WARNING : argument ':append' is not supported for '#{ tag }'" if append.present?
          
    unstack = false
  end
  
  content_class = [GRID_CONFIG[:classes][tag], options.delete(:class)]
  content_class << "#{ GRID_CONFIG[:classes][:prepend] }_#{ prepend }" if prepend
  content_class << "#{ GRID_CONFIG[:classes][:append] }_#{ append }" if append
  content_class << GRID_CONFIG[:classes][:nested] if options.delete(:nested)
  
  safe_buffer = (GRID_CONFIG[:elements][tag], nil, :id => options.delete(:id), :class => content_class.join(" ") , &block)
  
  @nested_stack.pop if unstack
  safe_buffer
end

#initialize(*args) ⇒ Object



13
14
15
16
# File 'lib/css_grid.rb', line 13

def initialize *args
  @nested_stack = []
  super
end

#one_col_row(options = {}, &block) ⇒ Object



110
111
112
# File 'lib/css_grid.rb', line 110

def one_col_row options = {}, &block
  cols_container :one, options.merge(:disable=>:container, :rows=>{:id=>options.delete(:id), :class=>options.delete(:class)}), &block
end

#recollect(size, collection) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/css_grid.rb', line 46

def recollect size, collection
  recollected = []
  0.step(collection.size - 1, size) do |i|
    recollected << collection[i..i + size - 1]
  end
  recollected
end

#respond_to?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


127
128
129
130
131
132
133
134
135
# File 'lib/css_grid.rb', line 127

def respond_to? method_name, include_private = false
  case method_name.to_s
  when  /^(container|row|(#{ TWELVE_FOR_REGEXP })_span)$/, 
        /^(#{ TWELVE_FOR_REGEXP })_cols?_container$/,
        /^(#{ TWELVE_FOR_REGEXP })_spans?_container$/
    true
  else super
  end
end

#spans_container(span_width, options = {}, &block) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/css_grid.rb', line 95

def spans_container span_width, options = {}, &block
  if @nested_stack.present?
    options[:nested_width] ||= TWELVE_STRING_INTS[@nested_stack.last.to_sym]
  end
  
  @span_width = span_width
  col_number = TWELVE_STRING_INTS_INVERT[(options.delete(:nested_width) || 12) / TWELVE_STRING_INTS[span_width]]
  
  safe_buffer = cols_container col_number, options, &block
  
  @span_width = nil
  safe_buffer
end