class Divy::Grid

Attributes

container_size[RW]
content[RW]
grid_color[RW]
grid_dimensions[RW]
show_grid[RW]

Public Class Methods

new(options = {}) click to toggle source
# 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

Public Instance Methods

add_content(input_content) click to toggle source
# File lib/divy/grid.rb, line 50
def add_content(input_content)
  @content += validated_content_arr(input_content)
end
can_be_num?(num) click to toggle source
# File lib/divy/grid.rb, line 129
def can_be_num?(num)
  (true if Float(num) rescue false)
end
container_size=(input) click to toggle source
# File lib/divy/grid.rb, line 24
def container_size=(input)
  @container_size = validated_container_size(input)
  rescue
    @container_size = @container_size
end
content=(input) click to toggle source
# File lib/divy/grid.rb, line 14
def content=(input)
  if input.kind_of?(Array)
    @content = validated_content_arr(input)
  elsif input.kind_of?(Hash)
    @content = [validated_content_hsh(input)]
  end
  rescue
    @content = @content
end
grid_color=(input) click to toggle source
# File lib/divy/grid.rb, line 36
def grid_color=(input)
  @grid_color = validated_hex_color(input)
  rescue
    @grid_color = @grid_color
end
grid_dimensions=(input) click to toggle source
# File lib/divy/grid.rb, line 30
def grid_dimensions=(input)
  @grid_dimensions = validated_number_array(input)
  rescue
    @grid_dimensions = @grid_dimensions
end
html() click to toggle source
# 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) click to toggle source
# File lib/divy/grid.rb, line 144
def percent?(percent)
  percent =~ /\d{1,3}%/
end
show_grid=(input) click to toggle source
# File lib/divy/grid.rb, line 42
def show_grid=(input)
  if input.kind_of?(TrueClass) || input.kind_of?(FalseClass)
    @show_grid = input
  else
    @show_grid = @show_grid
  end
end
two_numbers?(arr) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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