Skip to content

Instantly share code, notes, and snippets.

@bplexico
bplexico / starting_point.rb
Created Aug 16, 2013
original line to transform a three character string into a six character string
View starting_point.rb
generated_color = (color[0] * 2) + (color[1] * 2) + (color[2] * 2)
@bplexico
bplexico / color_generator.rb
Created Aug 15, 2013
ColorGenerator class after refactor
View color_generator.rb
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 Aug 15, 2013
tests created to verify starting functionality
View test_color_generator.rb
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 Dec 21, 2015
method moved to class
View starting_point.rb
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 Aug 15, 2013
method before refactor
View starting_point.rb
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 Jul 16, 2013
line with OR that can be refactored
View category_list.rb
@categories.insert(insert_at || @categories.size, uncategorized)
@bplexico
bplexico / category_list.rb
Created Jul 16, 2013
final insert_uncategorized_category
View category_list.rb
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 Jul 15, 2013
catch throw refactor
View category_list.rb
# 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 Jul 15, 2013
create new uncategorized category object
View category_list.rb
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 Jul 15, 2013
new UncategorizedCategory class
View uncategorized_category.rb
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