collectable 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/MIT-LICENSE +20 -0
- data/README.md +62 -0
- data/lib/collectable/version.rb +3 -0
- data/lib/collectable.rb +26 -0
- data/spec/collectable_spec.rb +95 -0
- data/spec/spec_helper.rb +9 -0
- metadata +66 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
YTE4ODA2MDhmMmViYWE5YzU2MTkyMTRlYWI5ZGViYjI4ZTMxY2ZjOA==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
YjVhMzBiODc0ZDEwM2UxMjMxZDYzYzE0Njk0ZmVhOGVkM2E0MDJjYw==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
MDkxMGFlNTNkOTVhZmZmYjI0MzdmOGU1NjYyMGE5MGY2ZTQwMmRmZThiNzQw
|
10
|
+
NmRiMGQyNzhmZTZlNTg1MzY3YWE5MTUxNGM5MzIzNzEyMzYzM2MwOGVlNzY3
|
11
|
+
MmZiOGY1ZTc3MmY3MzZkM2E4NDViYWZjNmNjMTY1MzMyMWNlYjM=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
MTFiODQ4NGFmYjE5NGYyNGZiYTI2ZTQ5ZjAzNTFjMDA0NmM1MGFiM2E3Yjc0
|
14
|
+
YmVmNGI2ODA4NDE5ODFiODliMDRmYTIwNWYxZjQ4YTEyMzJmYTI3NjVjMDFl
|
15
|
+
MTNkYTY2NDM4NGI3YTI1ZmQzNGQxNWE1YTcxMTRlMDY0NjIzNTQ=
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2014 Dennis Walters
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
## Collectable ##
|
2
|
+
|
3
|
+
So, I serialize stuff into arrays in my AR objects where it doesn't make
|
4
|
+
sense to go through the rigamaroe of actually making an empty AR model
|
5
|
+
to use for the association. This is just a bit of glue to make using
|
6
|
+
that pattern a bit easier with
|
7
|
+
|
8
|
+
There's not much to this thing, and what's there exists only to serve the
|
9
|
+
needs that I've had for this sort of thing.
|
10
|
+
|
11
|
+
That said, pull requests are totally welcome :)
|
12
|
+
|
13
|
+
## Gem Setup ##
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
gem install collectable
|
17
|
+
|
18
|
+
# Gemfile
|
19
|
+
gem 'collectable'
|
20
|
+
```
|
21
|
+
## Basic usage ##
|
22
|
+
|
23
|
+
The short version is "include the module and define some traits." That's also
|
24
|
+
the long version, as it were.
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
class HairColor
|
28
|
+
include Collectable
|
29
|
+
|
30
|
+
trait :blonde
|
31
|
+
trait :brown
|
32
|
+
trait :red
|
33
|
+
trait :black
|
34
|
+
trait :purple
|
35
|
+
trait :tie_dye
|
36
|
+
end
|
37
|
+
```
|
38
|
+
|
39
|
+
Then, to actually use the thing, do something like this in your view:
|
40
|
+
|
41
|
+
```erb
|
42
|
+
<%= form_for @person do |f| %>
|
43
|
+
<%= f.collection_check_boxes(:hair_color, HairColor.collected, HairColor.value, HairColor.name) %>
|
44
|
+
<% end %>
|
45
|
+
```
|
46
|
+
|
47
|
+
## Contributing ##
|
48
|
+
|
49
|
+
Do you use git-flow? I sure do. Please base anything you do off of
|
50
|
+
[the develop branch](https://github.com/ess/absolution/tree/develop).
|
51
|
+
|
52
|
+
1. Fork it.
|
53
|
+
2. Perform some BDD magic. Seriously. Be testing.
|
54
|
+
3. Submit a pull request.
|
55
|
+
|
56
|
+
## History ##
|
57
|
+
|
58
|
+
* 0.0.1 - Public release
|
59
|
+
|
60
|
+
## License ##
|
61
|
+
|
62
|
+
MIT License. Copyright 2014 Dennis Walters
|
data/lib/collectable.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'ostruct'
|
2
|
+
|
3
|
+
module Collectable
|
4
|
+
def self.included(base)
|
5
|
+
base.extend(ClassMethods)
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
def traits
|
10
|
+
@traits ||= []
|
11
|
+
end
|
12
|
+
|
13
|
+
def trait(trait_name)
|
14
|
+
traits.push(trait_name.to_s.downcase)
|
15
|
+
traits.uniq!
|
16
|
+
end
|
17
|
+
|
18
|
+
def collected(sort_field = :name)
|
19
|
+
traits.each_with_index.map {|name, index|
|
20
|
+
OpenStruct.new(name: name, val: index)
|
21
|
+
}.sort {|a,b|
|
22
|
+
a.send(sort_field) <=> b.send(sort_field)
|
23
|
+
}
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,95 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'collectable'
|
3
|
+
|
4
|
+
class Dummy
|
5
|
+
include Collectable
|
6
|
+
end
|
7
|
+
|
8
|
+
class TraitDummy
|
9
|
+
include Collectable
|
10
|
+
end
|
11
|
+
|
12
|
+
class TraitsDummy
|
13
|
+
include Collectable
|
14
|
+
end
|
15
|
+
|
16
|
+
class CollectedDummy
|
17
|
+
include Collectable
|
18
|
+
end
|
19
|
+
|
20
|
+
describe Collectable do
|
21
|
+
let(:klass) {Collectable}
|
22
|
+
|
23
|
+
context 'when included' do
|
24
|
+
let(:dummy) {Dummy}
|
25
|
+
|
26
|
+
it 'knows how to define traits' do
|
27
|
+
expect(dummy).to respond_to(:trait)
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'can report its tratis' do
|
31
|
+
expect(dummy).to respond_to(:traits)
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'knows how to collect the traits' do
|
35
|
+
expect(dummy).to respond_to(:collected)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
describe '.trait' do
|
40
|
+
let(:trait_dummy) {
|
41
|
+
TraitDummy
|
42
|
+
}
|
43
|
+
|
44
|
+
it 'can take a symbol' do
|
45
|
+
expect {trait_dummy.trait :Blah}.not_to raise_error
|
46
|
+
end
|
47
|
+
|
48
|
+
it 'can take a string' do
|
49
|
+
expect {trait_dummy.trait 'Trait 2'}.not_to raise_error
|
50
|
+
end
|
51
|
+
|
52
|
+
it 'adds a trait to the collectable traits' do
|
53
|
+
new_trait = :New_Trait
|
54
|
+
trait_dummy.trait new_trait
|
55
|
+
expect(trait_dummy.traits).to include(new_trait.to_s.downcase)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
describe '.traits' do
|
60
|
+
let(:traits_dummy) {
|
61
|
+
TraitsDummy
|
62
|
+
}
|
63
|
+
|
64
|
+
let(:new_trait) {:new_trait}
|
65
|
+
|
66
|
+
it 'is an array' do
|
67
|
+
expect(traits_dummy.traits).to be_a(Array)
|
68
|
+
end
|
69
|
+
|
70
|
+
it 'contains the traits defined by the class' do
|
71
|
+
expect(traits_dummy.traits).to be_empty
|
72
|
+
|
73
|
+
traits_dummy.trait new_trait
|
74
|
+
expect(traits_dummy.traits).to include(new_trait.to_s.downcase)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
describe '.collected' do
|
79
|
+
let(:collected_dummy) {
|
80
|
+
CollectedDummy
|
81
|
+
}
|
82
|
+
|
83
|
+
it 'is an array' do
|
84
|
+
expect(collected_dummy.collected).to be_a(Array)
|
85
|
+
end
|
86
|
+
|
87
|
+
it 'contains OpenStruct representations of the known traits' do
|
88
|
+
collected_dummy.trait :trait_1
|
89
|
+
collected_dummy.trait :trait_2
|
90
|
+
|
91
|
+
expect(collected_dummy.collected.map(&:name)).
|
92
|
+
to eql(collected_dummy.traits.sort)
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: collectable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Dennis Walters
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-10-10 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rspec
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ! '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ! '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
description: Make PORO models work with collection_check_boxes
|
28
|
+
email:
|
29
|
+
- pooster@gmail.com
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- lib/collectable/version.rb
|
35
|
+
- lib/collectable.rb
|
36
|
+
- MIT-LICENSE
|
37
|
+
- README.md
|
38
|
+
- spec/collectable_spec.rb
|
39
|
+
- spec/spec_helper.rb
|
40
|
+
homepage: http://github.com/ess/collectable
|
41
|
+
licenses:
|
42
|
+
- MIT
|
43
|
+
metadata: {}
|
44
|
+
post_install_message:
|
45
|
+
rdoc_options: []
|
46
|
+
require_paths:
|
47
|
+
- lib
|
48
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ! '>='
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '0'
|
53
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ! '>='
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '0'
|
58
|
+
requirements: []
|
59
|
+
rubyforge_project:
|
60
|
+
rubygems_version: 2.1.11
|
61
|
+
signing_key:
|
62
|
+
specification_version: 4
|
63
|
+
summary: Make PORO models work with collection_check_boxes
|
64
|
+
test_files:
|
65
|
+
- spec/collectable_spec.rb
|
66
|
+
- spec/spec_helper.rb
|