colorizable 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.rspec +2 -0
- data/Gemfile +3 -0
- data/LICENSE +21 -0
- data/README.md +52 -0
- data/Rakefile +10 -0
- data/colorizable.gemspec +31 -0
- data/lib/colorizable.rb +11 -0
- data/lib/colorizable/color.rb +9 -0
- data/lib/colorizable/model_extensions.rb +30 -0
- data/lib/colorizable/version_number.rb +18 -0
- data/rails/init.rb +1 -0
- data/spec/active_record_helper.rb +22 -0
- data/spec/active_record_models.rb +11 -0
- data/spec/colorizable/model_extensions_spec.rb +46 -0
- data/spec/colorizable_spec.rb +7 -0
- data/spec/database.yml +3 -0
- data/spec/spec_helper.rb +15 -0
- metadata +166 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 76aa0d86161bd8357776d0b8520e23fad2188f49
|
4
|
+
data.tar.gz: af8e9b6d1d90ba4788c4ac14582093e2133be846
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ee6522325bc9406d589b6679177eadc06ee9786dad588d172924a40e11098105498f5e7adf9f4744b378672d7a320452dba4742c64ce2e704966930b957b3a1d
|
7
|
+
data.tar.gz: 5bf2f791621631e682efb7a0320a192e11af56bc455a4236adceb168140ed563b045cbe1e019b0d5772a89965ae3b4b4f6f07637cd7ea6cdfd2d73fa21aa4344
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Ideal Project Group
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
# Colorizable
|
2
|
+
|
3
|
+
This library provides integration of the [color](https://github.com/halostatue/color) gem with Rails. Use 'colorize' to specify which fields you want to be backed by `Color::RGB` objects.
|
4
|
+
|
5
|
+
## Install
|
6
|
+
|
7
|
+
Add the gem to your application's Gemfile:
|
8
|
+
|
9
|
+
```
|
10
|
+
gem 'colorizable'
|
11
|
+
```
|
12
|
+
|
13
|
+
And execute:
|
14
|
+
|
15
|
+
```
|
16
|
+
bundle
|
17
|
+
```
|
18
|
+
|
19
|
+
You can also install it yourself using:
|
20
|
+
|
21
|
+
```
|
22
|
+
gem install colorizable
|
23
|
+
```
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
### ActiveRecord
|
28
|
+
|
29
|
+
The Uniform model has three string columns called primary_color, secondary_color, and font_color. Each column has a hex value stored in it. Instead of dealing with a string, it would be helpful to deal with `Color::RGB` objects which provide a number of helpers.
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
class Uniform < ActiveRecord::Base
|
33
|
+
|
34
|
+
colorize :primary_color, :secondary_color, :font_color
|
35
|
+
|
36
|
+
end
|
37
|
+
```
|
38
|
+
|
39
|
+
Each color on the model is now a `Color::RGB` object. The objects come with a number of helpers to convert the color to other formats like rgb.
|
40
|
+
|
41
|
+
## How to contribute
|
42
|
+
|
43
|
+
1. Fork it
|
44
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
45
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
46
|
+
4. Write your tests and check everything passes
|
47
|
+
5. Push to the branch (`git push origin my-new-feature`)
|
48
|
+
6. Create new Pull Request (into the master branch)
|
49
|
+
|
50
|
+
## License
|
51
|
+
|
52
|
+
Please refer to [LICENSE](https://github.com/idealprojectgroup/colorizable/blob/master/LICENSE).
|
data/Rakefile
ADDED
data/colorizable.gemspec
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
$LOAD_PATH.unshift File.expand_path('../lib', __FILE__)
|
2
|
+
require 'colorizable/version_number'
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = 'colorizable'
|
6
|
+
s.version = Colorizable.version
|
7
|
+
s.platform = Gem::Platform::RUBY
|
8
|
+
s.summary = "Provides an integration of the color gem with Rails"
|
9
|
+
s.description = s.summary
|
10
|
+
s.authors = ["Derek Hopper"]
|
11
|
+
s.email = 'hopper.derek@gmail.com'
|
12
|
+
s.homepage = 'http://github.com/idealprojectgroup/colorizable'
|
13
|
+
s.license = 'MIT'
|
14
|
+
|
15
|
+
s.files = `git ls-files`.split("\n")
|
16
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
17
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
18
|
+
s.require_paths = ['lib']
|
19
|
+
|
20
|
+
s.required_rubygems_version = '>= 1.3.6'
|
21
|
+
s.required_ruby_version = '>= 1.9'
|
22
|
+
|
23
|
+
s.add_dependency('rails','>= 3.1')
|
24
|
+
s.add_dependency('color', '~> 1.8')
|
25
|
+
|
26
|
+
s.add_development_dependency('rspec', '~> 3.0')
|
27
|
+
s.add_development_dependency('rspec-rails')
|
28
|
+
s.add_development_dependency('database_cleaner', '~> 1.3.0')
|
29
|
+
s.add_development_dependency('sqlite3')
|
30
|
+
s.add_development_dependency('rake')
|
31
|
+
end
|
data/lib/colorizable.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
module Colorizable
|
2
|
+
module ModelExtensions
|
3
|
+
def self.included(base)
|
4
|
+
base.extend(ClassMethods)
|
5
|
+
end
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
def colorize(*fields)
|
9
|
+
fields.each do |field|
|
10
|
+
name = field
|
11
|
+
|
12
|
+
define_method name do |*args|
|
13
|
+
Color.by_hex(self[name]) rescue Color.new
|
14
|
+
end
|
15
|
+
|
16
|
+
define_method "#{name}=" do |value|
|
17
|
+
color = if value.kind_of?(Hash)
|
18
|
+
value = value.symbolize_keys
|
19
|
+
Color.new(value[:r], value[:g], value[:b]) rescue nil
|
20
|
+
else
|
21
|
+
Color.by_hex(value.to_s) rescue nil
|
22
|
+
end.try(:hex)
|
23
|
+
|
24
|
+
self[name] = color
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
data/rails/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ActiveRecord::Base.send(:include, Colorizable::ModelExtensions)
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'rails/all'
|
2
|
+
require 'database_cleaner'
|
3
|
+
require 'yaml'
|
4
|
+
|
5
|
+
dbconfig = YAML::load(IO.read(File.join(File.dirname(__FILE__), 'database.yml')))
|
6
|
+
ActiveRecord::Base.logger = Logger.new(File.join(File.dirname(__FILE__), "debug.log"))
|
7
|
+
ActiveRecord::Base.establish_connection(dbconfig[ENV['DB'] || 'sqlite'])
|
8
|
+
|
9
|
+
RSpec.configure do |config|
|
10
|
+
config.before(:suite) do
|
11
|
+
DatabaseCleaner[:active_record].strategy = :transaction
|
12
|
+
DatabaseCleaner[:active_record].clean_with(:truncation)
|
13
|
+
end
|
14
|
+
|
15
|
+
config.before(:each) do
|
16
|
+
DatabaseCleaner[:active_record].start
|
17
|
+
end
|
18
|
+
|
19
|
+
config.after(:each) do
|
20
|
+
DatabaseCleaner[:active_record].clean
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
ActiveRecord::Schema.define(:version => 1) do
|
2
|
+
create_table :themes, force: true do |t|
|
3
|
+
t.column :primary_color, :string
|
4
|
+
t.column :secondary_color, :string
|
5
|
+
t.column :accent_color, :string
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
class Theme < ActiveRecord::Base
|
10
|
+
colorize :primary_color, :secondary_color, :accent_color
|
11
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require "active_record_models"
|
2
|
+
|
3
|
+
describe Colorizable::ModelExtensions do
|
4
|
+
describe "colorize" do
|
5
|
+
it "provides a Color::RGB object for each attribute" do
|
6
|
+
theme = Theme.new(primary_color: "#333", secondary_color: "#CCC", accent_color: "#acacac")
|
7
|
+
|
8
|
+
[:primary_color, :secondary_color, :accent_color].each do |attribute|
|
9
|
+
expect(theme.send(attribute)).to be_a Color::RGB
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
it "accepts a hash of rgb values on setter" do
|
14
|
+
theme = Theme.new
|
15
|
+
theme.primary_color = { r: 204, g: 204, b: 204 }
|
16
|
+
expect(theme.primary_color.to_s).to eq "#cccccc"
|
17
|
+
end
|
18
|
+
|
19
|
+
it "accepts a hex value string on setter" do
|
20
|
+
theme = Theme.new
|
21
|
+
theme.primary_color = "#ccc"
|
22
|
+
expect(theme.primary_color.to_s).to eq "#cccccc"
|
23
|
+
end
|
24
|
+
|
25
|
+
it "does not clobber ActiveModel::Dirty" do
|
26
|
+
theme = Theme.new
|
27
|
+
theme.primary_color = "#ccc"
|
28
|
+
expect(theme.primary_color_changed?).to be true
|
29
|
+
|
30
|
+
theme = Theme.create(primary_color: "#ccc")
|
31
|
+
theme.primary_color = "#ddd"
|
32
|
+
expect(theme.primary_color_changed?).to be true
|
33
|
+
expect(theme.primary_color_was).to eq "cccccc"
|
34
|
+
end
|
35
|
+
|
36
|
+
it "provides a blank Color object if value is blank" do
|
37
|
+
theme = Theme.new(primary_color: nil)
|
38
|
+
expect(theme.primary_color.to_s).to eq "#000000"
|
39
|
+
end
|
40
|
+
|
41
|
+
it "provides a blank Color object if value is an invalid color" do
|
42
|
+
theme = Theme.new(primary_color: "asdfasdfasdf")
|
43
|
+
expect(theme.primary_color.to_s).to eq "#000000"
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
data/spec/database.yml
ADDED
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
$LOAD_PATH.unshift File.expand_path('../lib', __FILE__)
|
2
|
+
|
3
|
+
require 'active_record_helper'
|
4
|
+
require 'rspec/rails'
|
5
|
+
require 'colorizable'
|
6
|
+
|
7
|
+
RSpec.configure do |config|
|
8
|
+
config.expect_with :rspec do |expectations|
|
9
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
10
|
+
end
|
11
|
+
|
12
|
+
config.mock_with :rspec do |mocks|
|
13
|
+
mocks.verify_partial_doubles = true
|
14
|
+
end
|
15
|
+
end
|
metadata
ADDED
@@ -0,0 +1,166 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: colorizable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Derek Hopper
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-11-24 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.1'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.1'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: color
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.8'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.8'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec-rails
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: database_cleaner
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 1.3.0
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 1.3.0
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: sqlite3
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rake
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
description: Provides an integration of the color gem with Rails
|
112
|
+
email: hopper.derek@gmail.com
|
113
|
+
executables: []
|
114
|
+
extensions: []
|
115
|
+
extra_rdoc_files: []
|
116
|
+
files:
|
117
|
+
- ".gitignore"
|
118
|
+
- ".rspec"
|
119
|
+
- Gemfile
|
120
|
+
- LICENSE
|
121
|
+
- README.md
|
122
|
+
- Rakefile
|
123
|
+
- colorizable.gemspec
|
124
|
+
- lib/colorizable.rb
|
125
|
+
- lib/colorizable/color.rb
|
126
|
+
- lib/colorizable/model_extensions.rb
|
127
|
+
- lib/colorizable/version_number.rb
|
128
|
+
- rails/init.rb
|
129
|
+
- spec/active_record_helper.rb
|
130
|
+
- spec/active_record_models.rb
|
131
|
+
- spec/colorizable/model_extensions_spec.rb
|
132
|
+
- spec/colorizable_spec.rb
|
133
|
+
- spec/database.yml
|
134
|
+
- spec/spec_helper.rb
|
135
|
+
homepage: http://github.com/idealprojectgroup/colorizable
|
136
|
+
licenses:
|
137
|
+
- MIT
|
138
|
+
metadata: {}
|
139
|
+
post_install_message:
|
140
|
+
rdoc_options: []
|
141
|
+
require_paths:
|
142
|
+
- lib
|
143
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
144
|
+
requirements:
|
145
|
+
- - ">="
|
146
|
+
- !ruby/object:Gem::Version
|
147
|
+
version: '1.9'
|
148
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - ">="
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: 1.3.6
|
153
|
+
requirements: []
|
154
|
+
rubyforge_project:
|
155
|
+
rubygems_version: 2.2.2
|
156
|
+
signing_key:
|
157
|
+
specification_version: 4
|
158
|
+
summary: Provides an integration of the color gem with Rails
|
159
|
+
test_files:
|
160
|
+
- spec/active_record_helper.rb
|
161
|
+
- spec/active_record_models.rb
|
162
|
+
- spec/colorizable/model_extensions_spec.rb
|
163
|
+
- spec/colorizable_spec.rb
|
164
|
+
- spec/database.yml
|
165
|
+
- spec/spec_helper.rb
|
166
|
+
has_rdoc:
|