Class: Divy::Grid

Inherits:
Object
  • Object
show all
Defined in:
lib/divy/grid.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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(options = {})
  @content = validated_content_arr(options.fetch(:content, [{type: :none, value: '', location: [0, 0], size: [1, 1]}]))
  @container_size = validated_container_size(options.fetch(:container_size, ['100%', '100%']))
  @grid_dimensions = validated_number_array(options.fetch(:grid_dimensions, [10, 10]))
  @show_grid = options.fetch(:show_grid, false)
  @grid_color = validated_hex_color(options.fetch(:grid_color, 'invisible'))
  @div_generator = Divy::DivGenerator.new
end

Instance Attribute Details

#container_sizeObject

Returns the value of attribute container_size.



3
4
5
# File 'lib/divy/grid.rb', line 3

def container_size
  @container_size
end

#contentObject

Returns the value of attribute content.



3
4
5
# File 'lib/divy/grid.rb', line 3

def content
  @content
end

#grid_colorObject

Returns the value of attribute grid_color.



3
4
5
# File 'lib/divy/grid.rb', line 3

def grid_color
  @grid_color
end

#grid_dimensionsObject

Returns the value of attribute grid_dimensions.



3
4
5
# File 'lib/divy/grid.rb', line 3

def grid_dimensions
  @grid_dimensions
end

#show_gridObject

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

Returns:

  • (Boolean)


129
130
131
# File 'lib/divy/grid.rb', line 129

def can_be_num?(num)
  (true if Float(num) rescue false)
end

#htmlObject



54
55
56
57
58
59
60
# File 'lib/divy/grid.rb', line 54

def html
  options = {show_grid: @show_grid, grid_color: @grid_color}
  content_html = @div_generator.divy_content(@grid_dimensions, @content, options)
  grid_html = @div_generator.grid_html(@grid_dimensions, options)
  all_content = content_html + grid_html
  @div_generator.divy_container(@container_size, all_content)
end

#percent?(percent) ⇒ Boolean

Returns:

  • (Boolean)


144
145
146
# File 'lib/divy/grid.rb', line 144

def percent?(percent)
  percent =~ /\d{1,3}%/
end

#two_numbers?(arr) ⇒ Boolean

Returns:

  • (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

Returns:

  • (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

Returns:

  • (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