Skip to content

Instantly share code, notes, and snippets.

@bplexico
bplexico / starting_point.rb
Created August 16, 2013 13:53
original line to transform a three character string into a six character string
generated_color = (color[0] * 2) + (color[1] * 2) + (color[2] * 2)
@bplexico
bplexico / color_generator.rb
Created August 15, 2013 21:56
ColorGenerator class after refactor
class ColorGenerator
def initialize(color)
@color = color
end
def generated_color
return make_hex(@color.ljust(6, @color)) if (1..2).include?(@color.length)
return make_hex(@color.scan(/((.))/).flatten.join) if @color.length == 3
return make_hex(@color.ljust(6, '0')) if (4..5).include?(@color.length)
@bplexico
bplexico / test_color_generator.rb
Created August 15, 2013 21:54
tests created to verify starting functionality
require_relative 'color_generator'
gem 'minitest'
require 'minitest/autorun'
class ColorGeneratorTest < Minitest::Test
def test_unhandled_color_returns_value_passed_in
assert_equal '#d3d3d3', ColorGenerator.new('d3d3d3').generated_color
end
@bplexico
bplexico / starting_point.rb
Last active December 21, 2015 03:49
method moved to class
class ColorGenerator
def initialize(color)
@color = color
end
def get_hex(color)
case
when color.length == 1
generated_color = color * 6
when color.length == 2
@bplexico
bplexico / starting_point.rb
Created August 15, 2013 21:37
method before refactor
def self.get_hex(color)
case
when color.length == 1
generated_color = color * 6
when color.length == 2
generated_color = color * 3
when color.length == 3
generated_color = (color[0] * 2) + (color[1] * 2) + (color[2] * 2)
when color.length < 6 && color.length > 3
generated_color = color + ("0" * (6 - color.length))
@bplexico
bplexico / category_list.rb
Created July 16, 2013 14:23
line with OR that can be refactored
@categories.insert(insert_at || @categories.size, uncategorized)
@bplexico
bplexico / category_list.rb
Created July 16, 2013 14:20
final insert_uncategorized_category
def insert_uncategorized_category(uncategorized)
insert_at = catch(:idx){
@categories.each_with_index do |c, idx|
throw(:idx, idx) if (uncategorized.topics_week || 0) > (c.topics_week || 0)
end
@categories.size
}
@categories.insert(insert_at, uncategorized)
end
@bplexico
bplexico / category_list.rb
Created July 15, 2013 21:33
catch throw refactor
# OLD
def insert_uncategorized_category(uncategorized)
insert_at = nil
@categories.each_with_index do |c, idx|
if (uncategorized.topics_week || 0) > (c.topics_week || 0)
insert_at = idx
break
end
end
@bplexico
bplexico / category_list.rb
Created July 15, 2013 21:29
create new uncategorized category object
def add_uncategorized
# Support for uncategorized topics
uncategorized_topics = Topic.uncategorized_topics
if uncategorized_topics.present?
uncategorized = UncategorizedCategory.new(uncategorized_topics)
...
@bplexico
bplexico / uncategorized_category.rb
Created July 15, 2013 21:21
new UncategorizedCategory class
class UncategorizedCategory < Category
def initialize(uncategorized_topics)
super({name: SiteSetting.uncategorized_name,
slug: Slug.for(SiteSetting.uncategorized_name),
color: SiteSetting.uncategorized_color,
text_color: SiteSetting.uncategorized_text_color,
featured_topics: uncategorized_topics}.merge(Topic.totals))
end
end