Class: Divy::Grid
- Inherits:
-
Object
- Object
- Divy::Grid
- Defined in:
- lib/divy/grid.rb
Instance Attribute Summary collapse
-
#container_size ⇒ Object
Returns the value of attribute container_size.
-
#content ⇒ Object
Returns the value of attribute content.
-
#grid_color ⇒ Object
Returns the value of attribute grid_color.
-
#grid_dimensions ⇒ Object
Returns the value of attribute grid_dimensions.
-
#show_grid ⇒ Object
Returns the value of attribute show_grid.
Instance Method Summary collapse
- #add_content(input_content) ⇒ Object
- #can_be_num?(num) ⇒ Boolean
- #html ⇒ Object
-
#initialize(options = {}) ⇒ Grid
constructor
A new instance of Grid.
- #percent?(percent) ⇒ Boolean
- #two_numbers?(arr) ⇒ Boolean
- #two_percents?(arr) ⇒ Boolean
- #valid_content_type?(content_hsh) ⇒ Boolean
- #validated_container_size(arr) ⇒ Object
- #validated_content_arr(arr) ⇒ Object
- #validated_content_hsh(content_hsh) ⇒ Object
- #validated_hex_color(color) ⇒ Object
- #validated_number_array(arr) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Grid
Returns a new instance of Grid.
5 6 7 8 9 10 11 12 |
# File 'lib/divy/grid.rb', line 5 def initialize( = {}) @content = validated_content_arr(.fetch(:content, [{type: :none, value: '', location: [0, 0], size: [1, 1]}])) @container_size = validated_container_size(.fetch(:container_size, ['100%', '100%'])) @grid_dimensions = validated_number_array(.fetch(:grid_dimensions, [10, 10])) @show_grid = .fetch(:show_grid, false) @grid_color = validated_hex_color(.fetch(:grid_color, 'invisible')) @div_generator = Divy::DivGenerator.new end |
Instance Attribute Details
#container_size ⇒ Object
Returns the value of attribute container_size.
3 4 5 |
# File 'lib/divy/grid.rb', line 3 def container_size @container_size end |
#content ⇒ Object
Returns the value of attribute content.
3 4 5 |
# File 'lib/divy/grid.rb', line 3 def content @content end |
#grid_color ⇒ Object
Returns the value of attribute grid_color.
3 4 5 |
# File 'lib/divy/grid.rb', line 3 def grid_color @grid_color end |
#grid_dimensions ⇒ Object
Returns the value of attribute grid_dimensions.
3 4 5 |
# File 'lib/divy/grid.rb', line 3 def grid_dimensions @grid_dimensions end |
#show_grid ⇒ Object
Returns the value of attribute show_grid.
3 4 5 |
# File 'lib/divy/grid.rb', line 3 def show_grid @show_grid end |
Instance Method Details
#add_content(input_content) ⇒ Object
50 51 52 |
# File 'lib/divy/grid.rb', line 50 def add_content(input_content) @content += validated_content_arr(input_content) end |
#can_be_num?(num) ⇒ Boolean
129 130 131 |
# File 'lib/divy/grid.rb', line 129 def can_be_num?(num) (true if Float(num) rescue false) end |
#html ⇒ Object
54 55 56 57 58 59 60 |
# File 'lib/divy/grid.rb', line 54 def html = {show_grid: @show_grid, grid_color: @grid_color} content_html = @div_generator.divy_content(@grid_dimensions, @content, ) grid_html = @div_generator.grid_html(@grid_dimensions, ) all_content = content_html + grid_html @div_generator.divy_container(@container_size, all_content) end |
#percent?(percent) ⇒ Boolean
144 145 146 |
# File 'lib/divy/grid.rb', line 144 def percent?(percent) percent =~ /\d{1,3}%/ end |
#two_numbers?(arr) ⇒ Boolean
118 119 120 121 122 123 124 125 126 127 |
# File 'lib/divy/grid.rb', line 118 def two_numbers?(arr) if arr.kind_of?(Array) && arr.length == 2 && can_be_num?(arr[0]) && can_be_num?(arr[1]) true else false end end |
#two_percents?(arr) ⇒ Boolean
133 134 135 136 137 138 139 140 141 142 |
# File 'lib/divy/grid.rb', line 133 def two_percents?(arr) if arr.kind_of?(Array) && arr.length == 2 && percent?(arr[0]) && percent?(arr[1]) true else false end end |
#valid_content_type?(content_hsh) ⇒ Boolean
91 92 93 94 95 96 97 98 |
# File 'lib/divy/grid.rb', line 91 def valid_content_type?(content_hsh) case content_hsh[:type] when :none, :html, :image true else false end end |
#validated_container_size(arr) ⇒ Object
100 101 102 103 104 105 106 107 108 |
# File 'lib/divy/grid.rb', line 100 def validated_container_size(arr) if two_numbers?(arr) arr.map{|n| n.to_i} elsif two_percents?(arr) arr else raise DivyGridError, :container_size end end |
#validated_content_arr(arr) ⇒ Object
70 71 72 73 74 75 76 77 78 |
# File 'lib/divy/grid.rb', line 70 def validated_content_arr(arr) if arr.kind_of?(Array) arr.each_with_object([]) do |content_hsh, valid_arr| valid_arr << validated_content_hsh(content_hsh) end else raise DivyGridError, :content_type end end |
#validated_content_hsh(content_hsh) ⇒ Object
80 81 82 83 84 85 86 87 88 89 |
# File 'lib/divy/grid.rb', line 80 def validated_content_hsh(content_hsh) if valid_content_type?(content_hsh) content_hsh[:location] = validated_number_array(content_hsh[:location] || [0, 0]) content_hsh[:size] = validated_number_array(content_hsh[:size] || [1, 1]) content_hsh[:value] ||= '' content_hsh else raise DivyGridError, :content_hsh end end |
#validated_hex_color(color) ⇒ Object
62 63 64 65 66 67 68 |
# File 'lib/divy/grid.rb', line 62 def validated_hex_color(color) if color =~ /^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/i || color == 'invisible' color else raise DivyGridError, :hex end end |
#validated_number_array(arr) ⇒ Object
110 111 112 113 114 115 116 |
# File 'lib/divy/grid.rb', line 110 def validated_number_array(arr) if two_numbers?(arr) arr.map{|n| n.to_i} else raise DivyGridError, :num_array end end |