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
- #cols_container(col_number, options = {}, &block) ⇒ Object
- #grid(tag, options = {}, &block) ⇒ Object
- #initialize(*args) ⇒ Object
- #method_missing(method_name, *args, &block) ⇒ Object
- #one_col_row(options = {}, &block) ⇒ Object
- #recollect(size, collection) ⇒ Object
- #respond_to?(method_name, include_private = false) ⇒ Boolean
- #spans_container(span_width, options = {}, &block) ⇒ Object
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/css_grid.rb', line 137 def method_missing method_name, *args, &block case method_name.to_s 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
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 114 115 |
# File 'lib/css_grid.rb', line 74 def cols_container col_number, = {}, &block [:rows] ||= {} [:spans] ||= {} if @nested_stack.present? [:rows].merge!({:nested => true}) [:nested_width] ||= TWELVE_STRING_INTS[@nested_stack.last.to_sym] end disable = [*.delete(:disable)] nested = [*.delete(:nested)] [:rows].merge!({:nested => true}) if nested.delete :container collection_length = TWELVE_STRING_INTS[col_number] span_width = @span_width || TWELVE_STRING_INTS_INVERT[(.delete(:nested_width) || 12) / (collection_length + ([:spans][:prepend] || 0) + ([:spans][:append] || 0))] rows = recollect(collection_length, .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, [: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, [:rows].clone){ cols.reduce(:safe_concat) } end safe_buffer = rows.reduce(:safe_concat) safe_buffer = grid(:container, .except!(:spans, :rows)){ safe_buffer } unless disable.delete :container Rails.version < "3" ? concat(safe_buffer) : 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 63 64 |
# File 'lib/css_grid.rb', line 19 def grid tag, = {}, &block .map_values! do |value| value.class == Proc ? value.call(@elt) : value end prepend = .delete :prepend prepend = if prepend.present? and TWELVE_STRING_INTS_INVERT.has_key? 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 = .delete :append append = if append and TWELVE_STRING_INTS_INVERT.has_key? append "#{ GRID_CONFIG[:classes][:append] }_#{ TWELVE_STRING_INTS_INVERT[.delete :append] }" elsif append warn "WARNING : invalid value for ':append'" end warn "WARNING : argument ':nested' is not supported for '#{ tag }'" if [:nested].present? and tag != :row if tag.to_s =~ /(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], .delete(:class), prepend, append].compact content_class << GRID_CONFIG[:classes][:nested] if .delete(:nested) .merge! :class => content_class.join(" ") safe_buffer = content_tag(.delete(:element) || GRID_CONFIG[:elements][tag], , &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
132 133 134 |
# File 'lib/css_grid.rb', line 132 def one_col_row = {}, &block cols_container :one, .merge(:disable=>:container, :rows=>{:id=>.delete(:id), :class=>.delete(:class)}), &block end |
#recollect(size, collection) ⇒ Object
66 67 68 69 70 71 72 |
# File 'lib/css_grid.rb', line 66 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
149 150 151 152 153 154 155 156 157 |
# File 'lib/css_grid.rb', line 149 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
117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/css_grid.rb', line 117 def spans_container span_width, = {}, &block if @nested_stack.present? [:nested_width] ||= TWELVE_STRING_INTS[@nested_stack.last.to_sym] end @span_width = span_width col_number = TWELVE_STRING_INTS_INVERT[(.delete(:nested_width) || 12) / TWELVE_STRING_INTS[span_width]] safe_buffer = cols_container col_number, , &block @span_width = nil safe_buffer end |