divygrid 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,97 @@
1
+ module Divy
2
+ class DivGenerator
3
+ def divy_container(container_size, container_content)
4
+ style = "style=\"width: #{size_and_unit(container_size[0])}; height: #{size_and_unit(container_size[1])};\""
5
+ begin_tag = "<div class=\"divycontainer\" #{style}>\n"
6
+ end_tag = "</div>\n"
7
+ begin_tag + container_content + end_tag
8
+ end
9
+
10
+ def size_and_unit(container_size)
11
+ if container_size.to_s.match(/%/)
12
+ container_size
13
+ else
14
+ "#{container_size}px"
15
+ end
16
+ end
17
+
18
+ def divy_content(grid_size, content_arr, options = {})
19
+ html_content = ''
20
+ content_arr.each do |content_hsh|
21
+ html_content += content_as_html(grid_size, content_hsh, options)
22
+ end
23
+ html_content
24
+ end
25
+
26
+ def content_as_html(grid_size, content, options)
27
+ case content[:type]
28
+ when :html
29
+ divy_box(content[:location][0], content[:location][1], grid_size, content[:value], content[:size], options)
30
+ when :image
31
+ divy_box(content[:location][0], content[:location][1], grid_size, image_html(content[:value]), content[:size],options)
32
+ when :none
33
+ ''
34
+ end
35
+ end
36
+
37
+ def image_html(path)
38
+ "<img src=\"#{path}\" height=\"100%\" width=\"100%\"></img>"
39
+ end
40
+
41
+ def grid_html(grid_dimensions, options = {})
42
+ div_grid_html = ""
43
+ (0..(grid_dimensions[0] - 1)).each do |x|
44
+ (0..(grid_dimensions[1] - 1)).each do |y|
45
+ div_grid_html += divy_box(x, y, grid_dimensions, nil, nil, options)
46
+ end
47
+ end
48
+ div_grid_html
49
+ end
50
+
51
+ def divy_box(x = 0, y = 0, grid_size = [10, 10], content = nil, content_size = nil, options = {})
52
+ style = divy_box_stylez(x, y, grid_size, content_size, options)
53
+ "<div class=\"divybox\" style=\"#{style}\">#{content}</div>\n"
54
+ end
55
+
56
+ def divy_box_stylez(x, y, grid_size, content_size = nil, options = {})
57
+ if content_size
58
+ content_divy_box_stylez(x, y, grid_size, content_size, options)
59
+ else
60
+ general_divy_box_stylez(x, y, grid_size, content_size, options)
61
+ end
62
+ end
63
+
64
+ def general_divy_box_stylez(x, y, grid_size, content_size = nil, options = {})
65
+ bottom = scaled_value(grid_size[1], y)
66
+ left = scaled_value(grid_size[0], x)
67
+ width = scaled_percentage(grid_size[0])
68
+ height = scaled_percentage(grid_size[1])
69
+ "bottom: #{bottom}%; left: #{left}%; width: #{width}%; height: #{height}%; #{default_stylez(options)}"
70
+ end
71
+
72
+ def content_divy_box_stylez(x, y, grid_size, content_size = nil, options = {})
73
+ bottom = scaled_value(grid_size[1], (y - 0.5))
74
+ left = scaled_value(grid_size[0], (x - 0.5))
75
+ width = scaled_value(grid_size[0], content_size[0])
76
+ height = scaled_value(grid_size[1], content_size[1])
77
+ "bottom: #{bottom}%; left: #{left}%; width: #{width}%; height: #{height}%; #{default_stylez(options)}"
78
+ end
79
+
80
+ def scaled_value(total_units, val)
81
+ scaled_percentage(total_units) * val.to_f
82
+ end
83
+
84
+ def scaled_percentage(total_units)
85
+ (100 / total_units.to_f)
86
+ end
87
+
88
+ def default_stylez(options = {})
89
+ if options[:show_grid]
90
+ color = options[:grid_color] || '#000000'
91
+ "position: absolute; border-style:solid; border-color:#{color};"
92
+ else
93
+ 'position: absolute;'
94
+ end
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,13 @@
1
+ class DivyGridError < StandardError
2
+ def initialize(msg_type = :default)
3
+ msg = {
4
+ default: 'DivyGrid not so divy',
5
+ hex: 'Hex color not valid',
6
+ content_type: 'Content inputs must be an array of hashes',
7
+ content_hsh: "Content type must be of format {type: html/image/none, value: '', location: [num, num]",
8
+ container_size: 'Invalid input format, must be an array of integer numbers, or percentages as a string',
9
+ num_array: 'Invalid input format, must be an array of numbers of length 2'
10
+ }[msg_type]
11
+ super(msg)
12
+ end
13
+ end
data/lib/divy/grid.rb ADDED
@@ -0,0 +1,113 @@
1
+ module Divy
2
+ class Grid
3
+ attr_accessor :content, :container_size, :grid_dimensions, :show_grid, :grid_color
4
+
5
+ def initialize(options = {})
6
+ @content = validated_content_arr(options.fetch(:content, [{type: :none, value: '', location: [0, 0], size: [1, 1]}]))
7
+ @container_size = validated_container_size(options.fetch(:container_size, ['100%', '100%']))
8
+ @grid_dimensions = validated_number_array(options.fetch(:grid_dimensions, [10, 10]))
9
+ @show_grid = options.fetch(:show_grid, false)
10
+ @grid_color = validated_hex_color(options.fetch(:grid_color, 'invisible'))
11
+ @div_generator = Divy::DivGenerator.new
12
+ end
13
+
14
+ def add_content(input_content)
15
+ @content += validated_content_arr(input_content)
16
+ end
17
+
18
+ def html
19
+ options = {show_grid: @show_grid, grid_color: @grid_color}
20
+ content_html = @div_generator.divy_content(@grid_dimensions, @content, options)
21
+ grid_html = @div_generator.grid_html(@grid_dimensions, options)
22
+ all_content = content_html + grid_html
23
+ @div_generator.divy_container(@container_size, all_content)
24
+ end
25
+
26
+ def validated_hex_color(color)
27
+ if color =~ /^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/i || color == 'invisible'
28
+ color
29
+ else
30
+ raise DivyGridError, :hex
31
+ end
32
+ end
33
+
34
+ def validated_content_arr(arr)
35
+ if arr.kind_of?(Array)
36
+ arr.each_with_object([]) do |content_hsh, valid_arr|
37
+ valid_arr << validated_content_hsh(content_hsh)
38
+ end
39
+ else
40
+ raise DivyGridError, :content_type
41
+ end
42
+ end
43
+
44
+ def validated_content_hsh(content_hsh)
45
+ if valid_content_type?(content_hsh)
46
+ content_hsh[:location] = validated_number_array(content_hsh[:location] || [0,0])
47
+ content_hsh[:size] = validated_number_array(content_hsh[:size] || [1,1])
48
+ content_hsh[:value] ||= ''
49
+ content_hsh
50
+ else
51
+ raise DivyGridError, :content_hsh
52
+ end
53
+ end
54
+
55
+ def valid_content_type?(content_hsh)
56
+ case content_hsh[:type]
57
+ when :none, :html, :image
58
+ true
59
+ else
60
+ false
61
+ end
62
+ end
63
+
64
+ def validated_container_size(arr)
65
+ if two_numbers?(arr)
66
+ arr.map{|n| n.to_i}
67
+ elsif two_percents?(arr)
68
+ arr
69
+ else
70
+ raise DivyGridError, :container_size
71
+ end
72
+ end
73
+
74
+ def validated_number_array(arr)
75
+ if two_numbers?(arr)
76
+ arr.map{|n| n.to_i}
77
+ else
78
+ raise DivyGridError, :num_array
79
+ end
80
+ end
81
+
82
+ def two_numbers?(arr)
83
+ if arr.kind_of?(Array) &&
84
+ arr.length == 2 &&
85
+ can_be_num?(arr[0]) &&
86
+ can_be_num?(arr[1])
87
+ true
88
+ else
89
+ false
90
+ end
91
+ end
92
+
93
+ def can_be_num?(num)
94
+ (true if Float(num) rescue false)
95
+ end
96
+
97
+ def two_percents?(arr)
98
+ if arr.kind_of?(Array) &&
99
+ arr.length == 2 &&
100
+ percent?(arr[0]) &&
101
+ percent?(arr[1])
102
+ true
103
+ else
104
+ false
105
+ end
106
+ end
107
+
108
+ def percent?(percent)
109
+ percent =~ /\d{1,3}%/
110
+ end
111
+
112
+ end
113
+ end
@@ -0,0 +1,3 @@
1
+ module Divy
2
+ VERSION = '0.0.3'
3
+ end
data/lib/divy.rb ADDED
@@ -0,0 +1,8 @@
1
+ require 'divy/grid'
2
+ require 'divy/divygrid_error'
3
+ require 'divy/div_generator'
4
+ require 'divy/version'
5
+
6
+ module Divy
7
+ # Awesome code goes here
8
+ end
@@ -0,0 +1,131 @@
1
+ require './test/test_helper'
2
+ require 'yaml'
3
+
4
+ describe Divy::DivGenerator do
5
+
6
+ before do
7
+ @test_div = Divy::DivGenerator.new
8
+ @html_results = YAML.load(File.open('test/lib/divy/html_examples.yml'))
9
+ end
10
+
11
+ describe '#default_stylez' do
12
+ it 'should return the common default styles given no arguments' do
13
+ @test_div.default_stylez.must_equal 'position: absolute;'
14
+ end
15
+
16
+ it 'should return default styles given show_grid as false' do
17
+ options = {show_grid: false}
18
+ @test_div.default_stylez(options).must_equal 'position: absolute;'
19
+ end
20
+
21
+ it 'should return default grid colors if given show_grid as true' do
22
+ options = {show_grid: true}
23
+ style = 'position: absolute; border-style:solid; border-color:#000000;'
24
+ @test_div.default_stylez(options). must_equal style
25
+ end
26
+
27
+ it 'should return the input color as boredr color if show_grid true and color provided' do
28
+ options = {show_grid: true, grid_color: '#00FFFF'}
29
+ style = 'position: absolute; border-style:solid; border-color:#00FFFF;'
30
+ @test_div.default_stylez(options).must_equal style
31
+ end
32
+ end
33
+
34
+ describe '#scaled_percentage' do
35
+ it 'should return the percentage of a single unit based on number of units' do
36
+ @test_div.scaled_percentage(7).must_equal 14.285714285714286
37
+ end
38
+
39
+ it 'should respond correctly if given a string number' do
40
+ @test_div.scaled_percentage('7').must_equal 14.285714285714286
41
+ end
42
+ end
43
+
44
+ describe '#scaled_value' do
45
+ it 'should return the percentage of height based on y unit location' do
46
+ @test_div.scaled_value(10, 3).must_equal 30
47
+ end
48
+
49
+ it 'should respond correctly for a float result' do
50
+ @test_div.scaled_value(7, 3).must_equal 42.85714285714286
51
+ end
52
+
53
+ it 'should respond currently given strign numbers' do
54
+ @test_div.scaled_value('7', 3).must_equal 42.85714285714286
55
+ @test_div.scaled_value(7, '3').must_equal 42.85714285714286
56
+ end
57
+ end
58
+
59
+ describe '#divy_box_stylez' do
60
+ it 'should return the style parameters for a divybox' do
61
+ style = 'bottom: 20.0%; left: 10.0%; width: 10.0%; height: 10.0%; position: absolute;'
62
+ @test_div.divy_box_stylez(1, 2, [10, 10]).must_equal style
63
+ end
64
+
65
+ it 'should return colors if borders and colors if correct options are passed' do
66
+ options = {show_grid: true, grid_color: '#00FFFF'}
67
+ style = 'bottom: 20.0%; left: 10.0%; width: 10.0%; height: 10.0%; position: absolute; border-style:solid; border-color:#00FFFF;'
68
+ @test_div.divy_box_stylez(1, 2, [10, 10], nil, options).must_equal style
69
+ end
70
+
71
+ it 'should return modified content styles if content size is passed' do
72
+ options = {show_grid: true, grid_color: '#00FFFF'}
73
+ style = 'bottom: 15.0%; left: 5.0%; width: 20.0%; height: 20.0%; position: absolute; border-style:solid; border-color:#00FFFF;'
74
+ @test_div.divy_box_stylez(1, 2, [10, 10], [2, 2], options).must_equal style
75
+ end
76
+ end
77
+
78
+ describe '#divy_box' do
79
+ it 'should return a default box and location gven no inputs' do
80
+ div_content = "<div class=\"divybox\" style=\"bottom: 0.0%; left: 0.0%; width: 10.0%; height: 10.0%; position: absolute;\"></div>\n"
81
+ @test_div.divy_box.must_equal div_content
82
+ end
83
+
84
+ it 'should add content to the div if content is passed' do
85
+ div_content = "<div class=\"divybox\" style=\"bottom: 0.0%; left: 0.0%; width: 10.0%; height: 10.0%; position: absolute;\">1</div>\n"
86
+ @test_div.divy_box(0, 0, [10, 10], 1).must_equal div_content
87
+ end
88
+ end
89
+
90
+ describe '#divy_container' do
91
+ it 'should wrap the given content in a divycontainer div with %' do
92
+ div_content = "<div class=\"divycontainer\" style=\"width: 100%; height: 100%;\">\ntest content</div>\n"
93
+ @test_div.divy_container(['100%', '100%'], 'test content').must_equal div_content
94
+ end
95
+
96
+ it 'should wrap the given content in a divycontainer div with as px' do
97
+ div_content = "<div class=\"divycontainer\" style=\"width: 100px; height: 100px;\">\ntest content</div>\n"
98
+ @test_div.divy_container([100, 100], 'test content').must_equal div_content
99
+ end
100
+
101
+ end
102
+
103
+ describe '#divy_content' do
104
+ it 'should return all content for the given content array' do
105
+ content = [
106
+ {type: :html, value: 'coolhtmlstuff', location: [1, 3], size: [1, 1]},
107
+ {type: :image, value: 'someimagepath', location: [1, 5], size: [1, 1]},
108
+ {type: :none, value: 'gonnaignorethisanyways', location: [1, 7], size: [1, 1]}
109
+ ]
110
+ @test_div.divy_content([10, 10], content).must_equal @html_results[3]
111
+ end
112
+
113
+ it 'should include options in teh divy box if they are passed' do
114
+ options = {show_grid: true, grid_color: '#00FFFF'}
115
+ content = [
116
+ {type: :html, value: 'coolhtmlstuff', location: [1, 3], size: [1, 1]},
117
+ {type: :image, value: 'someimagepath', location: [1, 5], size: [1, 1]},
118
+ {type: :none, value: 'gonnaignorethisanyways', location: [1, 7], size: [1, 1]}
119
+ ]
120
+ @test_div.divy_content([10, 10], content, options).must_equal @html_results[2]
121
+ end
122
+ end
123
+
124
+ describe '#grid_html' do
125
+ it 'should return each square of the div grid with no options' do
126
+ @test_div.grid_html([3, 3]).must_equal @html_results[1]
127
+ end
128
+ end
129
+
130
+
131
+ end
@@ -0,0 +1,206 @@
1
+ require './test/test_helper'
2
+ require 'yaml'
3
+
4
+ describe Divy::Grid do
5
+
6
+ before do
7
+ @grid = Divy::Grid.new
8
+ @html_results = YAML.load(File.open('test/lib/divy/html_examples.yml'))
9
+ end
10
+
11
+ describe '#initialize' do
12
+ it 'should set content to the given value if valid' do
13
+ content = [{type: :html, value: 'totally valid', location: [1, 3]}]
14
+ new_grid = Divy::Grid.new(content: content)
15
+ new_grid.content.must_equal content
16
+ end
17
+
18
+ it 'should set the default value for content if none given' do
19
+ @grid.content.must_equal([{:type => :none, :value => '', :location => [0, 0], :size=>[1, 1]}])
20
+ end
21
+
22
+ it 'should set container size if valid' do
23
+ new_grid = Divy::Grid.new(container_size: [20, 20])
24
+ new_grid.container_size.must_equal [20, 20]
25
+ end
26
+
27
+ it 'should set default container size if none given' do
28
+ @grid.container_size.must_equal %w(100% 100%)
29
+ end
30
+
31
+ it 'should set grid dimensions if valid' do
32
+ new_grid = Divy::Grid.new(grid_dimensions: [7, 7])
33
+ new_grid.grid_dimensions.must_equal [7, 7]
34
+ end
35
+
36
+ it 'should set default grid dimensions if none given' do
37
+ @grid.grid_dimensions.must_equal [10,10]
38
+ end
39
+
40
+ it 'should set grid color if valid' do
41
+ new_grid = Divy::Grid.new(grid_color: '#000000')
42
+ new_grid.grid_color.must_equal '#000000'
43
+ end
44
+
45
+ it 'should set default grid color to invisible if none given' do
46
+ @grid.grid_color.must_equal 'invisible'
47
+ end
48
+
49
+ it 'should set show_grid to false if not provided' do
50
+ @grid.show_grid.must_equal false
51
+ end
52
+ end
53
+
54
+ describe '#add_content' do
55
+ it 'should add content to the content array if valid' do
56
+ content = [{type: :html, value: 'coolhtmlstuff', location: [1, 3], size: [1, 1]}]
57
+ result_content = [
58
+ {type: :none, value: '', location: [0, 0], size: [1, 1]},
59
+ content[0]
60
+ ]
61
+ @grid.add_content(content)
62
+ @grid.content.must_equal result_content
63
+ end
64
+ end
65
+
66
+ describe '#validated_content_arr' do
67
+ it 'should return all content if all valid' do
68
+ content = [
69
+ {type: :html, value: 'coolhtmlstuff', location: [1, 3]},
70
+ {type: :image, value: 'someimagepath', location: [1, 3]},
71
+ {type: :none, value: 'gonnaignorethisanyways', location: [1, 3]}
72
+ ]
73
+ @grid.validated_content_arr(content).must_equal content
74
+ end
75
+
76
+ it 'should raise an error if invalid content is provided' do
77
+ content = [
78
+ {type: :html, value: 'coolhtmlstuff', location: [1, 3]},
79
+ {type: :image, value: 'someimagepath', location: [1, 3]},
80
+ {broken_stuff: 'HAHAIBREAKINURCODEZ!'}
81
+ ]
82
+ proc{@grid.validated_content_arr(content)}.must_raise DivyGridError
83
+ end
84
+ end
85
+
86
+ describe '#validated_content_hsh' do
87
+ it 'should return content if content is valid html type' do
88
+ content = {type: :html, value: 'coolhtmlstuff', location: [1, 3]}
89
+ @grid.validated_content_hsh(content).must_equal content
90
+ end
91
+
92
+ it 'should return content if content is valid html type' do
93
+ content = {type: :image, value: 'someimagepath', location: [1, 3]}
94
+ @grid.validated_content_hsh(content).must_equal content
95
+ end
96
+
97
+ it 'should return content if content is valid html type' do
98
+ content = {type: :none, value: 'gonnaignorethisanyways', location: [1, 3]}
99
+ @grid.validated_content_hsh(content).must_equal content
100
+ end
101
+
102
+ it 'should raise an exception if the content type is not valid' do
103
+ proc{@grid.validated_content_hsh(broken_stuff: 'HAHAIBREAKINURCODEZ!')}.must_raise DivyGridError
104
+ end
105
+ end
106
+
107
+ describe '#validated hex color' do
108
+ it 'should return the color if a valid hex color' do
109
+ @grid.validated_hex_color('#FF0022').must_equal '#FF0022'
110
+ end
111
+
112
+ it 'should raise an exception if input is not a valid hex color' do
113
+ proc{@grid.validated_hex_color('#FF002')}.must_raise DivyGridError
114
+ end
115
+ end
116
+
117
+ describe '#validated_container_size' do
118
+ it 'should return the container size if both inputs are valid percents as a string' do
119
+ @grid.validated_container_size(%w(20% 88%)).must_equal ['20%', '88%']
120
+ end
121
+
122
+ it 'should return the container size if both inputs are valid ints' do
123
+ @grid.validated_container_size([56, 97]).must_equal [56, 97]
124
+ end
125
+
126
+ it 'should return the container size if both inputs are valid ints as a string' do
127
+ @grid.validated_container_size(%w(56 97)).must_equal [56, 97]
128
+ end
129
+
130
+ it 'should raise an exception if inputs are not valid ints or percents' do
131
+ proc{@grid.validated_container_size([56, 'snoo'])}.must_raise DivyGridError
132
+ end
133
+ end
134
+
135
+ describe '#validated_number_array' do
136
+ it 'should return the array if both inputs in array are numbers as float/int' do
137
+ @grid.validated_number_array([4, 8]).must_equal [4, 8]
138
+ end
139
+
140
+ it 'should return the array if the inputs are valid numbers as strings' do
141
+ @grid.validated_number_array(%w(4 8)).must_equal [4, 8]
142
+ end
143
+
144
+ it 'should return the array for a valid input of a combo of int/float/string' do
145
+ @grid.validated_number_array([9, '2']).must_equal [9, 2]
146
+ end
147
+
148
+ it 'should raise an exception if both inputs are not valid numbers' do
149
+ proc{@grid.validated_number_array(%w(meow 8))}.must_raise DivyGridError
150
+ end
151
+ end
152
+
153
+ describe '#two_numbers?' do
154
+ it 'should return true if both inputs are integers/floats' do
155
+ @grid.two_numbers?([2, 6]).must_equal true
156
+ end
157
+
158
+ it 'should return true if both inputs are numbers a as a string' do
159
+ @grid.two_numbers?(%w(2 6)).must_equal true
160
+ end
161
+
162
+ it 'should return true for a combo of ints/floats/numbers as a string' do
163
+ @grid.two_numbers?([9, '4']).must_equal true
164
+ end
165
+
166
+ it 'should return false if either input is not a valid number' do
167
+ @grid.two_numbers?(['puppy', 6]).must_equal false
168
+ end
169
+
170
+ it 'should return false if array length is not 2' do
171
+ @grid.two_numbers?([2, 6, 4]).must_equal false
172
+ end
173
+ end
174
+
175
+ describe '#two_percents?' do
176
+ it 'should return true if both numbers are valid percents as a string' do
177
+ @grid.two_percents?(%w(46% 23%)).must_equal true
178
+ end
179
+
180
+ it 'should return false if either number is not a percent as a string' do
181
+ @grid.two_percents?(%w(bleh 23%)).must_equal false
182
+ end
183
+
184
+ it 'should return false if array length is not 2' do
185
+ @grid.two_percents?(%w(46% 23% 12%)).must_equal false
186
+ end
187
+ end
188
+
189
+ describe '#html' do
190
+ it 'should return correct html content given all arguments' do
191
+ @grid.content = [
192
+ {type: :html, value: 'coolhtmlstuff', location: [1, 3], size: [1, 1]},
193
+ {type: :image, value: 'someimagepath', location: [1, 3], size: [2, 2]}
194
+ ]
195
+ @grid.grid_dimensions = [5, 5]
196
+ @grid.show_grid = true
197
+ @grid.grid_color = '#FF00CC'
198
+ @grid.container_size = [100, 100]
199
+ @grid.html.must_equal (@html_results[0] + "\n")
200
+ end
201
+
202
+ it 'should return default contents given no arguments' do
203
+ @grid.html.must_equal (@html_results[4] + "\n")
204
+ end
205
+ end
206
+ end