delegate_attributes 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 +5 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +33 -0
- data/Rakefile +1 -0
- data/delegate_attributes.gemspec +23 -0
- data/lib/delegate_attributes.rb +11 -0
- data/lib/delegate_attributes/engine.rb +4 -0
- data/lib/delegate_attributes/models/active_record_extension.rb +47 -0
- data/lib/delegate_attributes/models/fake_errors.rb +9 -0
- data/lib/delegate_attributes/railtie.rb +13 -0
- data/lib/delegate_attributes/version.rb +3 -0
- metadata +74 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 DelegateAttributes
|
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,33 @@
|
|
1
|
+
# DelegateAttributes
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'delegate_attributes'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install delegate_attributes
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
ActiveRecord model:
|
22
|
+
|
23
|
+
class Blog < ActiveRecord::Base
|
24
|
+
delegate_attributes :last_author, :errors => :fit, :writer => true, :to => :category
|
25
|
+
end
|
26
|
+
|
27
|
+
## Contributing
|
28
|
+
|
29
|
+
1. Fork it
|
30
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
31
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
32
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
33
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'delegate_attributes/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "delegate_attributes"
|
8
|
+
gem.version = DelegateAttributes::VERSION
|
9
|
+
gem.authors = ["Valery Kvon"]
|
10
|
+
gem.email = ["addagger@gmail.com"]
|
11
|
+
gem.homepage = %q{http://vkvon.ru/projects/delegate_attributes}
|
12
|
+
gem.description = %q{Includes optional reader/writer delegation and unobtrusive errors validation}
|
13
|
+
gem.summary = %q{Delegate attributes from one model instance to another with errors validation}
|
14
|
+
|
15
|
+
gem.rubyforge_project = "delegate_attributes"
|
16
|
+
gem.add_development_dependency "activemodel"
|
17
|
+
|
18
|
+
gem.files = `git ls-files`.split($/)
|
19
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
20
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
21
|
+
gem.require_paths = ["lib"]
|
22
|
+
gem.licenses = ['MIT']
|
23
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'delegate_attributes/models/fake_errors'
|
2
|
+
|
3
|
+
module DelegateAttributes
|
4
|
+
module ActiveRecordExtension
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
included do
|
8
|
+
end
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
|
12
|
+
def delegate_attributes(*args)
|
13
|
+
options = args.extract_options!
|
14
|
+
writer_regexp = /=\z/
|
15
|
+
readers = args.select {|a| a.to_s !=~ writer_regexp}
|
16
|
+
writers = args.select {|a| a.to_s =~ writer_regexp}
|
17
|
+
if options[:writer] == true
|
18
|
+
writers += readers.map {|a| "#{a}="}
|
19
|
+
end
|
20
|
+
|
21
|
+
class_eval do
|
22
|
+
delegate *(readers + writers), :to => options[:to]
|
23
|
+
end
|
24
|
+
|
25
|
+
unless options[:errors] == false
|
26
|
+
class_eval <<-EOV
|
27
|
+
validate do
|
28
|
+
object = #{options[:to]}
|
29
|
+
#{"object.instance_variable_set(:@errors, DelegateAttributes::FakeErrors.new(object))" if options[:errors].to_s == "fit"}
|
30
|
+
if !object.valid?
|
31
|
+
object.errors.messages.each do |attribute, suberrors|
|
32
|
+
if attribute.to_s.in? %w{#{readers.join(" ")}}
|
33
|
+
suberrors.each do |suberror|
|
34
|
+
errors.add(attribute, suberror)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
EOV
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'rails'
|
2
|
+
|
3
|
+
module DelegateAttributes
|
4
|
+
class Railtie < ::Rails::Railtie
|
5
|
+
config.before_initialize do
|
6
|
+
ActiveSupport.on_load :active_record do
|
7
|
+
require 'delegate_attributes/models/active_record_extension'
|
8
|
+
include ActiveRecordExtension
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
end
|
13
|
+
end
|
metadata
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: delegate_attributes
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Valery Kvon
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-01-06 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activemodel
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
description: Includes optional reader/writer delegation and unobtrusive errors validation
|
31
|
+
email:
|
32
|
+
- addagger@gmail.com
|
33
|
+
executables: []
|
34
|
+
extensions: []
|
35
|
+
extra_rdoc_files: []
|
36
|
+
files:
|
37
|
+
- .gitignore
|
38
|
+
- Gemfile
|
39
|
+
- LICENSE.txt
|
40
|
+
- README.md
|
41
|
+
- Rakefile
|
42
|
+
- delegate_attributes.gemspec
|
43
|
+
- lib/delegate_attributes.rb
|
44
|
+
- lib/delegate_attributes/engine.rb
|
45
|
+
- lib/delegate_attributes/models/active_record_extension.rb
|
46
|
+
- lib/delegate_attributes/models/fake_errors.rb
|
47
|
+
- lib/delegate_attributes/railtie.rb
|
48
|
+
- lib/delegate_attributes/version.rb
|
49
|
+
homepage: http://vkvon.ru/projects/delegate_attributes
|
50
|
+
licenses:
|
51
|
+
- MIT
|
52
|
+
post_install_message:
|
53
|
+
rdoc_options: []
|
54
|
+
require_paths:
|
55
|
+
- lib
|
56
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
63
|
+
none: false
|
64
|
+
requirements:
|
65
|
+
- - ! '>='
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0'
|
68
|
+
requirements: []
|
69
|
+
rubyforge_project: delegate_attributes
|
70
|
+
rubygems_version: 1.8.24
|
71
|
+
signing_key:
|
72
|
+
specification_version: 3
|
73
|
+
summary: Delegate attributes from one model instance to another with errors validation
|
74
|
+
test_files: []
|