active_delegate 0.1.0

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,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b11efe5cb6d1a9c223450f9da445ab5dd6743128
4
+ data.tar.gz: 2c24603c1415aa21e15d85fe405797b268a899a2
5
+ SHA512:
6
+ metadata.gz: '08899cdfbc043b956ae5d626efb0ed17cc2fdca31d3642bc9dffd5a8bc09a91b9fd4e43e8bb94f441aaa5e196e8aeb92e0bc8967f20774225316d246e6a57a2f'
7
+ data.tar.gz: 669b8f94fe5b824cdff5647f0c0f7ab54efed63d763b9534b367d312036b6bb0138eeb92d8bddb5b801b2091c529acd9a5b36ff874ebdce3f67a5df58c3f71f6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in active_delegate.gemspec
4
+ gemspec
@@ -0,0 +1,120 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ active_delegate (0.1.0)
5
+ i18n (~> 0.8)
6
+ rails (~> 5.0)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ actioncable (5.1.1)
12
+ actionpack (= 5.1.1)
13
+ nio4r (~> 2.0)
14
+ websocket-driver (~> 0.6.1)
15
+ actionmailer (5.1.1)
16
+ actionpack (= 5.1.1)
17
+ actionview (= 5.1.1)
18
+ activejob (= 5.1.1)
19
+ mail (~> 2.5, >= 2.5.4)
20
+ rails-dom-testing (~> 2.0)
21
+ actionpack (5.1.1)
22
+ actionview (= 5.1.1)
23
+ activesupport (= 5.1.1)
24
+ rack (~> 2.0)
25
+ rack-test (~> 0.6.3)
26
+ rails-dom-testing (~> 2.0)
27
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
28
+ actionview (5.1.1)
29
+ activesupport (= 5.1.1)
30
+ builder (~> 3.1)
31
+ erubi (~> 1.4)
32
+ rails-dom-testing (~> 2.0)
33
+ rails-html-sanitizer (~> 1.0, >= 1.0.3)
34
+ activejob (5.1.1)
35
+ activesupport (= 5.1.1)
36
+ globalid (>= 0.3.6)
37
+ activemodel (5.1.1)
38
+ activesupport (= 5.1.1)
39
+ activerecord (5.1.1)
40
+ activemodel (= 5.1.1)
41
+ activesupport (= 5.1.1)
42
+ arel (~> 8.0)
43
+ activesupport (5.1.1)
44
+ concurrent-ruby (~> 1.0, >= 1.0.2)
45
+ i18n (~> 0.7)
46
+ minitest (~> 5.1)
47
+ tzinfo (~> 1.1)
48
+ arel (8.0.0)
49
+ builder (3.2.3)
50
+ concurrent-ruby (1.0.5)
51
+ erubi (1.6.0)
52
+ globalid (0.4.0)
53
+ activesupport (>= 4.2.0)
54
+ i18n (0.8.1)
55
+ loofah (2.0.3)
56
+ nokogiri (>= 1.5.9)
57
+ mail (2.6.5)
58
+ mime-types (>= 1.16, < 4)
59
+ method_source (0.8.2)
60
+ mime-types (3.1)
61
+ mime-types-data (~> 3.2015)
62
+ mime-types-data (3.2016.0521)
63
+ mini_portile2 (2.1.0)
64
+ minitest (5.10.2)
65
+ nio4r (2.1.0)
66
+ nokogiri (1.7.2)
67
+ mini_portile2 (~> 2.1.0)
68
+ rack (2.0.3)
69
+ rack-test (0.6.3)
70
+ rack (>= 1.0)
71
+ rails (5.1.1)
72
+ actioncable (= 5.1.1)
73
+ actionmailer (= 5.1.1)
74
+ actionpack (= 5.1.1)
75
+ actionview (= 5.1.1)
76
+ activejob (= 5.1.1)
77
+ activemodel (= 5.1.1)
78
+ activerecord (= 5.1.1)
79
+ activesupport (= 5.1.1)
80
+ bundler (>= 1.3.0, < 2.0)
81
+ railties (= 5.1.1)
82
+ sprockets-rails (>= 2.0.0)
83
+ rails-dom-testing (2.0.3)
84
+ activesupport (>= 4.2.0)
85
+ nokogiri (>= 1.6)
86
+ rails-html-sanitizer (1.0.3)
87
+ loofah (~> 2.0)
88
+ railties (5.1.1)
89
+ actionpack (= 5.1.1)
90
+ activesupport (= 5.1.1)
91
+ method_source
92
+ rake (>= 0.8.7)
93
+ thor (>= 0.18.1, < 2.0)
94
+ rake (10.5.0)
95
+ sprockets (3.7.1)
96
+ concurrent-ruby (~> 1.0)
97
+ rack (> 1, < 3)
98
+ sprockets-rails (3.2.0)
99
+ actionpack (>= 4.0)
100
+ activesupport (>= 4.0)
101
+ sprockets (>= 3.0.0)
102
+ thor (0.19.4)
103
+ thread_safe (0.3.6)
104
+ tzinfo (1.2.3)
105
+ thread_safe (~> 0.1)
106
+ websocket-driver (0.6.5)
107
+ websocket-extensions (>= 0.1.0)
108
+ websocket-extensions (0.1.2)
109
+
110
+ PLATFORMS
111
+ ruby
112
+
113
+ DEPENDENCIES
114
+ active_delegate!
115
+ bundler (~> 1.14)
116
+ minitest (~> 5.0)
117
+ rake (~> 10.0)
118
+
119
+ BUNDLED WITH
120
+ 1.14.6
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 jonian
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,36 @@
1
+ # ActiveDelegate
2
+
3
+ Stores and retrieves delegatable data through attributes on a ActiveRecord class, with support for translatable attributes.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'active_delegate'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install active_delegate
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Development
26
+
27
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment. To install this gem onto your local machine, run `bundle exec rake install`.
28
+
29
+ ## Contributing
30
+
31
+ Bug reports and pull requests are welcome on GitHub at https://github.com/hardpixel/active-delegate.
32
+
33
+
34
+ ## License
35
+
36
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,10 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << 'test'
6
+ t.libs << 'lib'
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,22 @@
1
+ require 'active_delegate/version'
2
+
3
+ module ActiveDelegate
4
+ autoload :Associations, 'active_delegate/associations'
5
+ autoload :Attributes, 'active_delegate/attributes'
6
+
7
+ class << self
8
+ def included(model_class)
9
+ model_class.extend self
10
+ end
11
+ end
12
+
13
+ def delegate_associations(*args)
14
+ options = args.extract_options!
15
+ Associations.new(self, options)
16
+ end
17
+
18
+ def delegate_attributes(*args)
19
+ options = args.extract_options!
20
+ Attributes.new(self, options)
21
+ end
22
+ end
@@ -0,0 +1,70 @@
1
+ module ActiveDelegate
2
+ class Associations
3
+ # Initialize assoctiations
4
+ def initialize(model, options)
5
+ @model = model
6
+ @options = default_options.merge(options)
7
+
8
+ delegate_associations
9
+ end
10
+
11
+ private
12
+
13
+ # Get default options
14
+ def default_options
15
+ { except: [], only: [], allow_nil: false, to: [] }
16
+ end
17
+
18
+ # Get association reflection
19
+ def association_reflection
20
+ assoc_name = @options.fetch(:to)
21
+ reflection = @model.reflect_on_association(assoc_name)
22
+
23
+ return reflection unless reflection.nil?
24
+ raise "#{@model.name} don't have the association #{assoc_name}"
25
+ end
26
+
27
+ # Get model association class
28
+ def association_class
29
+ association_reflection.klass
30
+ end
31
+
32
+ # Get all associations
33
+ def association_reflections
34
+ association_class.reflect_on_all_associations
35
+ end
36
+
37
+ # Get singular model association names
38
+ def association_names
39
+ association_reflections.map(&:name)
40
+ end
41
+
42
+ # Get delegatable associations
43
+ def delegatable_associations
44
+ associations = association_names.map(&:to_sym)
45
+ associations = associations & @options[:only].to_a if @options[:only].present?
46
+ associations = associations - @options[:except].to_a if @options[:except].present?
47
+
48
+ associations.map(&:to_sym)
49
+ end
50
+
51
+ # Check if association is collection
52
+ def collection_association?(association)
53
+ collections = association_reflections.select(&:collection?).map(&:name)
54
+ association.in? collections
55
+ end
56
+
57
+ # Delegate associations
58
+ def delegate_associations
59
+ options = { to: @options[:to], allow_nil: @options[:allow_nil] }
60
+
61
+ delegatable_associations.each do |association|
62
+ @model.delegate "#{association}", options
63
+ @model.delegate "#{association}=", options
64
+
65
+ @model.delegate "#{association}_attributes=", options rescue true
66
+ @model.delegate "build_#{association}", options unless collection_association?(association)
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,119 @@
1
+ module ActiveDelegate
2
+ autoload :ReadWrite, 'active_delegate/read_write'
3
+ autoload :Dirty, 'active_delegate/dirty'
4
+ autoload :Localized, 'active_delegate/localized'
5
+
6
+ class Attributes
7
+ # Initialize attributes
8
+ def initialize(model, options)
9
+ @model = model
10
+ @options = default_options.merge(options)
11
+
12
+ build_association
13
+ delegate_attributes
14
+ save_delegated_attributes
15
+ end
16
+
17
+ private
18
+
19
+ # Get default options
20
+ def default_options
21
+ { except: [], only: [], allow_nil: false, to: [], prefix: nil, localized: false }
22
+ end
23
+
24
+ # Get association reflection
25
+ def association_reflection
26
+ assoc_name = @options.fetch(:to)
27
+ reflection = @model.reflect_on_association(assoc_name)
28
+
29
+ return reflection unless reflection.nil?
30
+ raise "#{@model.name} don't have the association #{assoc_name}"
31
+ end
32
+
33
+ # Get model association class
34
+ def association_class
35
+ association_reflection.klass
36
+ end
37
+
38
+ # Get association attribute names
39
+ def association_attribute_names
40
+ association_class.attribute_names
41
+ end
42
+
43
+ # Default excluded attributes
44
+ def default_excluded_attributes
45
+ assoc_as = association_reflection.options[:as]
46
+ poly_attr = [:"#{assoc_as}_type", :"#{assoc_as}_id"] if assoc_as.present?
47
+
48
+ [:id, :created_at, :updated_at] + poly_attr.to_a
49
+ end
50
+
51
+ # Get delegatable attributes
52
+ def delegatable_attributes
53
+ attributes = association_attribute_names.map(&:to_sym)
54
+ attributes = attributes & @options[:only].to_a if @options[:only].present?
55
+ attributes = attributes - @options[:except].to_a if @options[:except].present?
56
+ attributes = attributes - default_excluded_attributes
57
+
58
+ attributes.map(&:to_sym)
59
+ end
60
+
61
+ # Get localized delegatable attributes
62
+ def localized_attributes
63
+ attributes = delegatable_attributes
64
+ localized = Localized.localized_methods(attributes) if @options[:localized].present?
65
+
66
+ localized.to_a.map(&:to_sym)
67
+ end
68
+
69
+ # Get delegatable methods
70
+ def delegatable_methods
71
+ attributes = delegatable_attributes + localized_attributes
72
+ readwrite = ReadWrite.readwrite_methods(attributes)
73
+ dirty = Dirty.dirty_methods(attributes)
74
+ methods = readwrite + dirty
75
+
76
+ methods.map(&:to_sym)
77
+ end
78
+
79
+ # Delegate attributes
80
+ def delegate_attributes
81
+ options = { to: @options[:to], allow_nil: @options[:allow_nil], prefix: @options[:prefix] }
82
+ @model.delegate(*delegatable_methods, options)
83
+ end
84
+
85
+ # Build association method override
86
+ def build_association
87
+ @model.class.send :define_method, :"#{@options[:to]}" do
88
+ super || @model.send(:"build_#{@options[:to]}")
89
+ end
90
+ end
91
+
92
+ # Get prefixed attributes
93
+ def prefix_attributes(attributes)
94
+ if @options[:prefix].present?
95
+ prefix = @options[:prefix]
96
+ prefix = @options[:to] if prefix.is_a? TrueClass
97
+
98
+ attributes.map { |a| :"#{prefix}_#{a}" }
99
+ else
100
+ attributes
101
+ end
102
+ end
103
+
104
+ # Save delagated attributes in model class
105
+ def save_delegated_attributes
106
+ delegated = prefix_attributes(delegatable_attributes)
107
+ @model.send :define_singleton_method, :"#{@options[:to]}_attribute_names" do
108
+ delegated
109
+ end
110
+
111
+ if @options[:localized].present?
112
+ localized = prefix_attributes(localized_attributes)
113
+ @model.send :define_singleton_method, :"#{@options[:to]}_localized_attribute_names" do
114
+ localized
115
+ end
116
+ end
117
+ end
118
+ end
119
+ end
@@ -0,0 +1,19 @@
1
+ module ActiveDelegate
2
+ module Dirty
3
+ class << self
4
+ # Get dirty methods for attributes
5
+ def dirty_methods(attributes)
6
+ @dirty_methods = attributes.to_a.flat_map do |attribute|
7
+ method_suffixes.map { |suffix| "#{attribute}#{suffix}" }
8
+ end
9
+ end
10
+
11
+ # Get method suffixes
12
+ def method_suffixes
13
+ @method_suffixes ||= Class.new do
14
+ include ::ActiveModel::Dirty
15
+ end.attribute_method_matchers.map(&:suffix).select { |m| m =~ /\A_/ }
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,26 @@
1
+ require 'i18n'
2
+
3
+ module ActiveDelegate
4
+ module Localized
5
+ class << self
6
+ # Get localized methods for attributes
7
+ def localized_methods(attributes)
8
+ @localized_methods = attributes.to_a.flat_map do |attribute|
9
+ method_suffixes.map { |suffix| "#{attribute}#{suffix}" }
10
+ end
11
+ end
12
+
13
+ # Get method suffixes
14
+ def method_suffixes
15
+ @method_suffixes ||= I18n.available_locales.map do |locale|
16
+ "_#{normalize_locale(locale)}"
17
+ end
18
+ end
19
+
20
+ # Normalize locale
21
+ def normalize_locale(locale)
22
+ "#{locale.to_s.downcase.sub("-", "_")}".freeze
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,17 @@
1
+ module ActiveDelegate
2
+ module ReadWrite
3
+ class << self
4
+ # Get readwrite methods for attributes
5
+ def readwrite_methods(attributes)
6
+ @readwrite_methods = attributes.to_a.flat_map do |attribute|
7
+ method_suffixes.map { |suffix| "#{attribute}#{suffix}" }
8
+ end
9
+ end
10
+
11
+ # Get method suffixes
12
+ def method_suffixes
13
+ ['', '=', '?']
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ module ActiveDelegate
2
+ VERSION = '0.1.0'
3
+ end
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_delegate
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jonian Guveli
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-05-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '5.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '5.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: i18n
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.8'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.8'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.14'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.14'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '5.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '5.0'
83
+ description: Stores and retrieves delegatable data through attributes on a ActiveRecord
84
+ class, with support for translatable attributes.
85
+ email:
86
+ - jonian@hardpixel.eu
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - Gemfile
92
+ - Gemfile.lock
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - lib/active_delegate.rb
97
+ - lib/active_delegate/associations.rb
98
+ - lib/active_delegate/attributes.rb
99
+ - lib/active_delegate/dirty.rb
100
+ - lib/active_delegate/localized.rb
101
+ - lib/active_delegate/read_write.rb
102
+ - lib/active_delegate/version.rb
103
+ homepage: https://github.com/hardpixel/active-delegate
104
+ licenses:
105
+ - MIT
106
+ metadata: {}
107
+ post_install_message:
108
+ rdoc_options: []
109
+ require_paths:
110
+ - lib
111
+ required_ruby_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ requirements: []
122
+ rubyforge_project:
123
+ rubygems_version: 2.6.11
124
+ signing_key:
125
+ specification_version: 4
126
+ summary: Delegate ActiveRecord model attributes and associations
127
+ test_files: []