collection_extensions 0.0.1
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.
- data/.gitignore +17 -0
- data/.rspec +1 -0
- data/.travis.yml +10 -0
- data/Gemfile +9 -0
- data/LICENSE +22 -0
- data/README.md +57 -0
- data/Rakefile +11 -0
- data/collection_extensions.gemspec +16 -0
- data/lib/collection_extensions.rb +25 -0
- data/lib/collection_extensions/cattr.rb +46 -0
- data/lib/collection_extensions/config.rb +9 -0
- data/lib/collection_extensions/version.rb +3 -0
- data/spec/collection_extensions_spec.rb +69 -0
- metadata +59 -0
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Chris Doyle
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
# CollectionExtensions
|
2
|
+
|
3
|
+
Sometimes an operation just doesn't fit well into a scope, but you don't want to lose your declarative code style
|
4
|
+
by operating on all the objects individually. This gem adds a few lines of code to make it easier to add methods to collections of objects.
|
5
|
+
|
6
|
+
<img src="https://secure.travis-ci.org/arches/collection_extensions.png" />
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
gem 'collection_extensions'
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install collection_extensions
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
Include the module in your model and specify what collections you want to extend:
|
25
|
+
|
26
|
+
class User < ActiveRecord::Base
|
27
|
+
include CollectionExtensions
|
28
|
+
has_many :orders, :preferences
|
29
|
+
extend_collections :orders, :preferences
|
30
|
+
|
31
|
+
# the original unextended collection is still available, aliased as orig_* (eg, orig_orders or orig_preferences)
|
32
|
+
end
|
33
|
+
|
34
|
+
This will use the modules OrdersCollectionExtensions and PreferencesCollectionExtensions to extend those associations
|
35
|
+
whenever you use them. For example:
|
36
|
+
|
37
|
+
module OrdersCollectionExtensions
|
38
|
+
def for_product_id(product_id)
|
39
|
+
# 'self' is the array of orders
|
40
|
+
select {|o| o.line_items.collect(&:product_id).include? product_id}
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
> User.find(1).orders.for_product_id(2)
|
45
|
+
|
46
|
+
If you want a different naming convention, set the config variable using `%s` string substitution:
|
47
|
+
|
48
|
+
CollectionExtensions::Config.naming_convention = "MethodsForCollectionsOf%s"
|
49
|
+
|
50
|
+
|
51
|
+
## Contributing
|
52
|
+
|
53
|
+
1. Fork it
|
54
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
55
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
56
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
57
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/collection_extensions/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Chris Doyle"]
|
6
|
+
gem.email = ["archslide@gmail.com"]
|
7
|
+
gem.description = %q{Lightweight framework for adding methods to groups of ActiveRecord objects}
|
8
|
+
gem.summary = %q{Light-weight framework for adding methods to groups of ActiveRecord objects}
|
9
|
+
gem.homepage = "http://github.com/arches/collection_extensions"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
13
|
+
gem.name = "collection_extensions"
|
14
|
+
gem.require_paths = ["lib"]
|
15
|
+
gem.version = CollectionExtensions::VERSION
|
16
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require "collection_extensions/version"
|
2
|
+
require "collection_extensions/cattr"
|
3
|
+
require "collection_extensions/config"
|
4
|
+
|
5
|
+
module CollectionExtensions
|
6
|
+
|
7
|
+
def self.included(base)
|
8
|
+
base.extend ClassMethods
|
9
|
+
end
|
10
|
+
|
11
|
+
module ClassMethods
|
12
|
+
def extend_collections(*associations)
|
13
|
+
Array(associations).each do |association|
|
14
|
+
alias_method "orig_#{association}", association
|
15
|
+
|
16
|
+
define_method association do
|
17
|
+
upper_camel = association.to_s.split("_").collect{|piece| piece.capitalize}.join
|
18
|
+
extender = Object.const_get(CollectionExtensions::Config.naming_convention % upper_camel)
|
19
|
+
send("orig_#{association}").extend(extender)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
class Class
|
2
|
+
def cattr_reader(*syms)
|
3
|
+
syms.flatten.each do |sym|
|
4
|
+
next if sym.is_a?(Hash)
|
5
|
+
class_eval(<<-EOS, __FILE__, __LINE__)
|
6
|
+
unless defined? @@#{sym}
|
7
|
+
@@#{sym} = nil
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.#{sym}
|
11
|
+
@@#{sym}
|
12
|
+
end
|
13
|
+
|
14
|
+
def #{sym}
|
15
|
+
@@#{sym}
|
16
|
+
end
|
17
|
+
EOS
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def cattr_writer(*syms)
|
22
|
+
options = syms.last.is_a?(Hash) ? syms.pop : {}
|
23
|
+
syms.flatten.each do |sym|
|
24
|
+
class_eval(<<-EOS, __FILE__, __LINE__)
|
25
|
+
unless defined? @@#{sym}
|
26
|
+
@@#{sym} = nil
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.#{sym}=(obj)
|
30
|
+
@@#{sym} = obj
|
31
|
+
end
|
32
|
+
|
33
|
+
#{"
|
34
|
+
def #{sym}=(obj)
|
35
|
+
@@#{sym} = obj
|
36
|
+
end
|
37
|
+
" unless options[:instance_writer] == false }
|
38
|
+
EOS
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def cattr_accessor(*syms)
|
43
|
+
cattr_reader(*syms)
|
44
|
+
cattr_writer(*syms)
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
require 'collection_extensions'
|
2
|
+
|
3
|
+
module ExamplesCollectionExtensions
|
4
|
+
def operate_on_all_examples
|
5
|
+
"operate_on_all_examples"
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
module FoobarsCollectionExtensions
|
10
|
+
def operate_on_all_foobars
|
11
|
+
"operate_on_all_foobars"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
module MethodsForCollectionsOfExamples
|
16
|
+
def operate_on_all_examples
|
17
|
+
"from a module with a different naming convention"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
class CollectionTester
|
22
|
+
include CollectionExtensions
|
23
|
+
|
24
|
+
def examples
|
25
|
+
"original examples method"
|
26
|
+
end
|
27
|
+
|
28
|
+
def foobars
|
29
|
+
"original foobars method"
|
30
|
+
end
|
31
|
+
|
32
|
+
extend_collections :examples, :foobars
|
33
|
+
end
|
34
|
+
|
35
|
+
describe CollectionExtensions do
|
36
|
+
let(:ct) { CollectionTester.new }
|
37
|
+
|
38
|
+
describe "#extend_collections" do
|
39
|
+
it "aliases the examples" do
|
40
|
+
ct.orig_examples.should == "original examples method"
|
41
|
+
ct.examples.should be_a ExamplesCollectionExtensions
|
42
|
+
ct.examples.operate_on_all_examples.should == "operate_on_all_examples"
|
43
|
+
end
|
44
|
+
|
45
|
+
it "aliases the foobars" do
|
46
|
+
ct.orig_foobars.should == "original foobars method"
|
47
|
+
ct.foobars.should be_a FoobarsCollectionExtensions
|
48
|
+
ct.foobars.operate_on_all_foobars.should == "operate_on_all_foobars"
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
describe "changing the naming convention" do
|
54
|
+
let(:ct) { CollectionTester.new }
|
55
|
+
|
56
|
+
before do
|
57
|
+
CollectionTester::Config.naming_convention = "MethodsForCollectionsOf%s"
|
58
|
+
end
|
59
|
+
|
60
|
+
it "extends the association with the proper module" do
|
61
|
+
ct.orig_examples.should == "original examples method"
|
62
|
+
ct.examples.should be_a MethodsForCollectionsOfExamples
|
63
|
+
ct.examples.operate_on_all_examples.should == "from a module with a different naming convention"
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
describe "" do
|
68
|
+
|
69
|
+
end
|
metadata
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: collection_extensions
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Chris Doyle
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-09-04 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Lightweight framework for adding methods to groups of ActiveRecord objects
|
15
|
+
email:
|
16
|
+
- archslide@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- .rspec
|
23
|
+
- .travis.yml
|
24
|
+
- Gemfile
|
25
|
+
- LICENSE
|
26
|
+
- README.md
|
27
|
+
- Rakefile
|
28
|
+
- collection_extensions.gemspec
|
29
|
+
- lib/collection_extensions.rb
|
30
|
+
- lib/collection_extensions/cattr.rb
|
31
|
+
- lib/collection_extensions/config.rb
|
32
|
+
- lib/collection_extensions/version.rb
|
33
|
+
- spec/collection_extensions_spec.rb
|
34
|
+
homepage: http://github.com/arches/collection_extensions
|
35
|
+
licenses: []
|
36
|
+
post_install_message:
|
37
|
+
rdoc_options: []
|
38
|
+
require_paths:
|
39
|
+
- lib
|
40
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
47
|
+
none: false
|
48
|
+
requirements:
|
49
|
+
- - ! '>='
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: '0'
|
52
|
+
requirements: []
|
53
|
+
rubyforge_project:
|
54
|
+
rubygems_version: 1.8.19
|
55
|
+
signing_key:
|
56
|
+
specification_version: 3
|
57
|
+
summary: Light-weight framework for adding methods to groups of ActiveRecord objects
|
58
|
+
test_files:
|
59
|
+
- spec/collection_extensions_spec.rb
|