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
GRID_CONFIG =
{ :classes => Hash.new{ |hash, key| hash[key] = key }, 
:elements => Hash.new(:div).merge(:container => :section) }
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



135
136
137
138
139
140
141
142
143
144
145
# File 'lib/css_grid.rb', line 135

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



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/css_grid.rb', line 72

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|
      @elt = 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
    @elt = nil

    grid(:row, options[:rows].clone){ cols.reduce(:safe_concat) }
  end
  
  safe_buffer = rows.reduce(:safe_concat)
  safe_buffer = grid(:container, options.except!(:spans, :rows)){ safe_buffer } unless disable.delete :container
  safe_buffer
end

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



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

def grid tag, options = {}, &block
  options.map_values! do |value|        
    value.class == Proc ? value.call(@elt) : value 
  end
  
  prepend = if options[:prepend].present? and TWELVE_STRING_INTS_INVERT.has_key? (prepend = options.delete(:prepend)).abs
    if prepend > 0
      "#{ GRID_CONFIG[:classes][:prepend] }_#{ TWELVE_STRING_INTS_INVERT[prepend] }"
    else
      "#{ GRID_CONFIG[:classes][:minus] }_#{ TWELVE_STRING_INTS_INVERT[prepend.abs] }"
    end
  elsif prepend
    warn "WARNING : invalid value for ':prepend'"
  end
  
  append = if options[:append].present? and TWELVE_STRING_INTS_INVERT.has_key? (append = options.delete(:append))
    "#{ GRID_CONFIG[:classes][:append] }_#{ TWELVE_STRING_INTS_INVERT[options.delete :append] }"
  elsif append
    warn "WARNING : invalid value for ':append'"
  end
  
  
  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), prepend, append].compact
  content_class << GRID_CONFIG[:classes][:nested] if options.delete(:nested)
  options.merge! :class => content_class.join(" ")
                               
  safe_buffer = (options.delete(:element) || GRID_CONFIG[:elements][tag], options, &block)
  
  @nested_stack.pop if unstack
  safe_buffer
end

#initialize(*args) ⇒ Object



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

def initialize *args
  @nested_stack = []
  super
end

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



130
131
132
# File 'lib/css_grid.rb', line 130

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



64
65
66
67
68
69
70
# File 'lib/css_grid.rb', line 64

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)


147
148
149
150
151
152
153
154
155
# File 'lib/css_grid.rb', line 147

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



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

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