-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
226 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class SS::ColorValidator < ActiveModel::EachValidator | ||
def validate_each(record, attribute, value) | ||
return if value.blank? | ||
|
||
if SS::Color.parse(value).blank? | ||
record.errors.add(attribute, options[:message] || :malformed_color) | ||
return | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
class SS::Color | ||
class << self | ||
def parse(color) | ||
return if color.blank? | ||
|
||
color = color[1..-1] if color.start_with?("#") | ||
case color.length | ||
when 3 | ||
red = color[0] * 2 | ||
green = color[1] * 2 | ||
blue = color[2] * 2 | ||
when 6 | ||
red = color[0..1] | ||
green = color[2..3] | ||
blue = color[4..5] | ||
end | ||
return if !red || !green || !blue | ||
|
||
numeric_red = red.hex | ||
return if numeric_red == 0 && red != "00" | ||
|
||
numeric_green = green.hex | ||
return if numeric_green == 0 && green != "00" | ||
|
||
numeric_blue = blue.hex | ||
return if numeric_blue == 0 && blue != "00" | ||
|
||
SS::RandomColor::Rgb.new(numeric_red, numeric_green, numeric_blue) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
require 'spec_helper' | ||
|
||
describe SS::Color do | ||
describe ".parse" do | ||
context "with valid colors" do | ||
it do | ||
# 6-digit starting with '#' | ||
SS::Color.parse("#000000").tap do |rgb| | ||
expect(rgb.red).to eq 0 | ||
expect(rgb.green).to eq 0 | ||
expect(rgb.blue).to eq 0 | ||
end | ||
SS::Color.parse("#ffffff").tap do |rgb| | ||
expect(rgb.red).to eq 255 | ||
expect(rgb.green).to eq 255 | ||
expect(rgb.blue).to eq 255 | ||
end | ||
SS::Color.parse("#ffffff".upcase).tap do |rgb| | ||
expect(rgb.red).to eq 255 | ||
expect(rgb.green).to eq 255 | ||
expect(rgb.blue).to eq 255 | ||
end | ||
|
||
# 3-digits starting with '#' | ||
SS::Color.parse("#000").tap do |rgb| | ||
expect(rgb.red).to eq 0 | ||
expect(rgb.green).to eq 0 | ||
expect(rgb.blue).to eq 0 | ||
end | ||
SS::Color.parse("#fff").tap do |rgb| | ||
expect(rgb.red).to eq 255 | ||
expect(rgb.green).to eq 255 | ||
expect(rgb.blue).to eq 255 | ||
end | ||
SS::Color.parse("#fff".upcase).tap do |rgb| | ||
expect(rgb.red).to eq 255 | ||
expect(rgb.green).to eq 255 | ||
expect(rgb.blue).to eq 255 | ||
end | ||
|
||
# just 6-digit | ||
SS::Color.parse("000000").tap do |rgb| | ||
expect(rgb.red).to eq 0 | ||
expect(rgb.green).to eq 0 | ||
expect(rgb.blue).to eq 0 | ||
end | ||
SS::Color.parse("ffffff").tap do |rgb| | ||
expect(rgb.red).to eq 255 | ||
expect(rgb.green).to eq 255 | ||
expect(rgb.blue).to eq 255 | ||
end | ||
SS::Color.parse("ffffff".upcase).tap do |rgb| | ||
expect(rgb.red).to eq 255 | ||
expect(rgb.green).to eq 255 | ||
expect(rgb.blue).to eq 255 | ||
end | ||
|
||
# just 3-digits | ||
SS::Color.parse("000").tap do |rgb| | ||
expect(rgb.red).to eq 0 | ||
expect(rgb.green).to eq 0 | ||
expect(rgb.blue).to eq 0 | ||
end | ||
SS::Color.parse("fff").tap do |rgb| | ||
expect(rgb.red).to eq 255 | ||
expect(rgb.green).to eq 255 | ||
expect(rgb.blue).to eq 255 | ||
end | ||
SS::Color.parse("fff".upcase).tap do |rgb| | ||
expect(rgb.red).to eq 255 | ||
expect(rgb.green).to eq 255 | ||
expect(rgb.blue).to eq 255 | ||
end | ||
end | ||
end | ||
|
||
context "with invalid colors" do | ||
it do | ||
expect(SS::Color.parse(nil)).to be_nil | ||
expect(SS::Color.parse("")).to be_nil | ||
expect(SS::Color.parse("z")).to be_nil | ||
expect(SS::Color.parse("zz")).to be_nil | ||
expect(SS::Color.parse("zzz")).to be_nil | ||
expect(SS::Color.parse("zzzz")).to be_nil | ||
expect(SS::Color.parse("zzzzz")).to be_nil | ||
expect(SS::Color.parse("zzzzzz")).to be_nil | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
require 'spec_helper' | ||
|
||
describe SS::ColorValidator, type: :validator do | ||
subject { build :gws_schedule_plan, color: value } | ||
|
||
context 'with nil' do | ||
let(:value) { nil } | ||
|
||
it do | ||
is_expected.to be_valid | ||
end | ||
end | ||
|
||
context 'with blank' do | ||
let(:value) { '' } | ||
|
||
it do | ||
is_expected.to be_valid | ||
end | ||
end | ||
|
||
context 'with black' do | ||
let(:value) { '#000000' } | ||
|
||
it do | ||
is_expected.to be_valid | ||
end | ||
end | ||
|
||
context 'with white (lowercase)' do | ||
let(:value) { '#ffffff'.downcase } | ||
|
||
it do | ||
is_expected.to be_valid | ||
end | ||
end | ||
|
||
context 'with white (uppercase)' do | ||
let(:value) { '#ffffff'.upcase } | ||
|
||
it do | ||
is_expected.to be_valid | ||
end | ||
end | ||
|
||
context 'not staring with "#"' do | ||
let(:value) { 'ffffff' } | ||
|
||
it do | ||
is_expected.to be_valid | ||
end | ||
end | ||
|
||
context "length isn't 7" do | ||
let(:value) { '#fff' } | ||
|
||
it do | ||
is_expected.to be_valid | ||
end | ||
end | ||
|
||
context "not hex decimal" do | ||
let(:value) { '#ghijkl' } | ||
|
||
it do | ||
is_expected.to be_invalid | ||
end | ||
end | ||
end |