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.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,10 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.8.7
4
+ - 1.9.2
5
+ - 1.9.3
6
+ - jruby-18mode
7
+ - jruby-19mode
8
+ - rbx-18mode
9
+ - rbx-19mode
10
+ - ree
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in collection_extensions.gemspec
4
+ gemspec
5
+
6
+ group :test do
7
+ gem "rake", "~> 0.9.2"
8
+ gem 'rspec', '~> 2.11.0'
9
+ end
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.
@@ -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
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+
4
+ require 'rspec/core/rake_task'
5
+
6
+ desc "Run specs"
7
+ RSpec::Core::RakeTask.new do |t|
8
+ end
9
+
10
+
11
+ task :default => :spec
@@ -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,9 @@
1
+ module CollectionExtensions
2
+ class Config
3
+ cattr_accessor :naming_convention
4
+
5
+ DEFAULT_NAMING_CONVENTION = "%sCollectionExtensions"
6
+
7
+ @@naming_convention = DEFAULT_NAMING_CONVENTION
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module CollectionExtensions
2
+ VERSION = "0.0.1"
3
+ 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