delegate_all_for 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -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,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in delegate_all_for.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Jason Rust
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,51 @@
1
+ # DelegateAllFor
2
+
3
+ Inspired by the ["Delegating Your
4
+ Attributes"](http://killswitchcollective.com/articles/21_delegating_your_attributes)
5
+ article this gem allows for easy [delegation](http://apidock.com/rails/Module/delegate) of all columns of an ActiveRecord association, including
6
+ the getter, setter, and predicate for each column.
7
+
8
+ ## Usage
9
+
10
+ ```ruby
11
+ class Parent < ActiveRecord::Base
12
+ has_one :child
13
+ # columns: name
14
+ delegate_all_for :child, except: [:sort], also_include: [:extra_method]
15
+ end
16
+
17
+ class Child < ActiveRecord::Base
18
+ belongs_to :parent
19
+ # columns: name, description, sort
20
+ def extra_method; 'a little extra' end
21
+ end
22
+
23
+ Parent.new.description # Delegated to Child
24
+ Parent.new.description? # Delegated to Child
25
+ Parent.new.description= # Delegated to Child
26
+ Parent.new.extra_method # Delegated to Child
27
+ Parent.new.name # Still comes from Parent
28
+ Parent.new.sort # method_missing
29
+ ```
30
+
31
+ ## Installation
32
+
33
+ Add this line to your application's Gemfile:
34
+
35
+ gem 'delegate_all_for'
36
+
37
+ And then execute:
38
+
39
+ $ bundle
40
+
41
+ Or install it yourself as:
42
+
43
+ $ gem install delegate_all_for
44
+
45
+ ## Contributing
46
+
47
+ 1. Fork it
48
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
49
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
50
+ 4. Push to the branch (`git push origin my-new-feature`)
51
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,17 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/delegate_all_for/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Jason Rust"]
6
+ gem.email = ["jason@lessonplanet.com"]
7
+ gem.description = %q{Easy delegation of all columns of an ActiveRecord association}
8
+ gem.summary = %q{Easy delegation of all columns of an ActiveRecord association}
9
+ gem.homepage = "https://github.com/LessonPlanet/delegate_all_for"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "delegate_all_for"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = DelegateAllFor::VERSION
17
+ end
@@ -0,0 +1,3 @@
1
+ module DelegateAllFor
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,46 @@
1
+ require 'active_record'
2
+ require 'active_record/version'
3
+
4
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
5
+
6
+ require 'delegate_all_for/version'
7
+
8
+ $LOAD_PATH.shift
9
+
10
+ module DelegateAllFor
11
+ # For all columns of the specified association to the current object,
12
+ # the getter, setter, and predicate are delegated.
13
+ #
14
+ # Supported options:
15
+ # [:except]
16
+ # An array of column names to exclude from delegation
17
+ # [:also_include]
18
+ # An array of method names to also delegate
19
+ def delegate_all_for(*attr_names)
20
+ options = { except: [], also_include: [] }
21
+ options.update(attr_names.extract_options!)
22
+ options.assert_valid_keys(:except, :also_include)
23
+
24
+ exclude_columns = self.column_names.dup.concat(options[:except].map(&:to_s))
25
+ puts exclude_columns.inspect
26
+ attr_names.each do |association_name|
27
+ if reflection = reflect_on_association(association_name)
28
+ options[:also_include].each do |m|
29
+ class_eval(%{delegate :#{m}, :to => :#{association_name}})
30
+ end
31
+ puts (reflection.klass.column_names - exclude_columns).inspect
32
+ (reflection.klass.column_names - exclude_columns).each do |column_name|
33
+ class_eval(%{delegate :#{column_name}, :to => :#{association_name}})
34
+ class_eval(%{delegate :#{column_name}=, :to => :#{association_name}})
35
+ class_eval(%{delegate :#{column_name}?, :to => :#{association_name}})
36
+ end
37
+ else
38
+ raise ArgumentError, "No association found for name `#{association_name}'. Has it been defined yet?"
39
+ end
40
+ end
41
+ end
42
+ end
43
+
44
+ if defined?(ActiveRecord::Base)
45
+ ActiveRecord::Base.extend DelegateAllFor
46
+ end
@@ -0,0 +1,34 @@
1
+ require File.expand_path('../../spec_helper', __FILE__)
2
+
3
+ # Still need to figure out how to get this to work
4
+ =begin
5
+ class Parent < ActiveRecord::Base
6
+ def self.column_names; %w(one two) end
7
+
8
+ has_one :child
9
+ delegate_all_for :child, except: [:three], also_include: [:extra]
10
+
11
+ def initialize; end
12
+ def two; 'parent' end
13
+ end
14
+
15
+ class Child < ActiveRecord::Base
16
+ belongs_to :parent
17
+ def self.column_names; %w(two three four) end
18
+ def two; 'child' end
19
+ def extra; true end
20
+ end
21
+
22
+ describe DelegateAllFor do
23
+ describe 'delegate_all_for' do
24
+ subject { Parent.new }
25
+
26
+ it { should respond_to? :three }
27
+ it { should respond_to? :three? }
28
+ it { should respond_to? :three= }
29
+ it { should respond_to? :extra }
30
+ it { should_not respond_to? :three }
31
+ its(:two) { should == 'parent' }
32
+ end
33
+ end
34
+ =end
@@ -0,0 +1,7 @@
1
+ require File.expand_path('../../lib/delegate_all_for', __FILE__)
2
+
3
+ RSpec.configure do |config|
4
+ config.treat_symbols_as_metadata_keys_with_true_values = true
5
+ config.run_all_when_everything_filtered = true
6
+ config.filter_run :focus
7
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: delegate_all_for
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jason Rust
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-05-02 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Easy delegation of all columns of an ActiveRecord association
15
+ email:
16
+ - jason@lessonplanet.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - .rspec
23
+ - Gemfile
24
+ - LICENSE
25
+ - README.md
26
+ - Rakefile
27
+ - delegate_all_for.gemspec
28
+ - lib/delegate_all_for.rb
29
+ - lib/delegate_all_for/version.rb
30
+ - spec/delegate_all_for/delegate_all_for_spec.rb
31
+ - spec/spec_helper.rb
32
+ homepage: https://github.com/LessonPlanet/delegate_all_for
33
+ licenses: []
34
+ post_install_message:
35
+ rdoc_options: []
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ! '>='
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ requirements: []
51
+ rubyforge_project:
52
+ rubygems_version: 1.8.17
53
+ signing_key:
54
+ specification_version: 3
55
+ summary: Easy delegation of all columns of an ActiveRecord association
56
+ test_files:
57
+ - spec/delegate_all_for/delegate_all_for_spec.rb
58
+ - spec/spec_helper.rb