integer-inheritance 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4b45f29a1e3866e71502397408bce1fd83ff247f
4
+ data.tar.gz: bb7c0d11682b315014cfade5cdffe384a57a9db9
5
+ SHA512:
6
+ metadata.gz: a01fb9fb9789a115cfff74479174575f996e23e3dad4a854bf24d7b0629abd73813cffa95a788e50effdf04c8d9073d11b0a6ff276b9007659e7a9582978d447
7
+ data.tar.gz: b2cf3a17be2b06f48a1578a1cc193a40837bf2c33137eeddcf710c4ae26f06eca135af9901d65fa63f21ea562f070dca5fd5f63ce6e9904b5fca882d79566664
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
15
+ .idea
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ source 'https://rubygems.org'
5
+
6
+ # Specify your gem's dependencies in integer-inheritance.gemspec
7
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2016 Yaroslav Konoplov
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,30 @@
1
+ ```ruby
2
+ class Post < ActiveRecord::Base
3
+ uses_integer_inheritance
4
+ end
5
+
6
+ class Article < Post
7
+ end
8
+
9
+ class Publication < Post
10
+ end
11
+
12
+ # Add to initializers or to some script that is not reloaded:
13
+ # (this adds support for reloading in development mode. See ActionDispatch::Reloader)
14
+ IntegerInheritance.describe do
15
+
16
+ # Use full class name here (only strings!)
17
+ with 'Post' do
18
+ map 1, to: 'Post'
19
+ map 2, to: 'Article'
20
+
21
+ # You can specify more than one type
22
+ map 3, 4, to: 'Publication'
23
+ end
24
+ end
25
+ ```
26
+
27
+ ## Gemfile
28
+ ```ruby
29
+ gem 'integer-inheritance', '~> 1.0'
30
+ ```
@@ -0,0 +1,21 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = 'integer-inheritance'
6
+ s.version = '1.0.1'
7
+ s.authors = ['Yaroslav Konoplov']
8
+ s.email = ['eahome00@gmail.com']
9
+ s.summary = 'Single table inheritance mechanism built on integer types'
10
+ s.description = 'Single table inheritance mechanism built on integer types'
11
+ s.homepage = 'http://github.com/yivo/integer-inheritance'
12
+ s.license = 'MIT'
13
+
14
+ s.executables = `git ls-files -z -- bin/*`.split("\x0").map{ |f| File.basename(f) }
15
+ s.files = `git ls-files -z`.split("\x0")
16
+ s.test_files = `git ls-files -z -- {test,spec,features}/*`.split("\x0")
17
+ s.require_paths = ['lib']
18
+
19
+ s.add_dependency 'activesupport', '>= 3.0', '< 6.0'
20
+ s.add_dependency 'activerecord', '>= 3.0', '< 6.0'
21
+ end
@@ -0,0 +1,35 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ require 'active_support/concern'
5
+ require 'active_support/core_ext/string'
6
+ require 'active_record'
7
+
8
+ require 'integer-inheritance/macro'
9
+ require 'integer-inheritance/extension'
10
+ require 'integer-inheritance/evaluator'
11
+ require 'integer-inheritance/column_types'
12
+
13
+ module IntegerInheritance
14
+ class << self
15
+ def mappings
16
+ @mappings ||= {}
17
+ end
18
+
19
+ def describe(&block)
20
+ Evaluator.instance.instance_eval(&block)
21
+ end
22
+
23
+ attr_accessor :default_column
24
+ end
25
+
26
+ self.default_column = 'inheritance_type'
27
+ end
28
+
29
+ class ActiveRecord::Base
30
+ include IntegerInheritance::Macro
31
+ end
32
+
33
+ class ActiveRecord::ConnectionAdapters::TableDefinition
34
+ include IntegerInheritance::ColumnTypes
35
+ end
@@ -0,0 +1,12 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ module IntegerInheritance
5
+ module ColumnTypes
6
+ def inheritance_column(*args)
7
+ options = { null: false, index: true, limit: 1, default: 1 }.merge!(args.extract_options!)
8
+ column_names = args.presence || [IntegerInheritance.default_column]
9
+ column_names.each { |name| column(name, :integer, options) }
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,21 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ module IntegerInheritance
5
+ class Evaluator
6
+ include Singleton
7
+
8
+ def with(base_class_name, &block)
9
+ @mapping = {}
10
+ instance_eval(&block)
11
+ (IntegerInheritance.mappings[base_class_name.to_s] ||= {}).merge!(@mapping)
12
+ ensure
13
+ @mapping = nil
14
+ end
15
+
16
+ def map(*sti_types, to:)
17
+ full_class_name = to.to_s
18
+ sti_types.each { |t| @mapping[t.to_i] = full_class_name }
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,55 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ module IntegerInheritance
5
+ module Extension
6
+ extend ActiveSupport::Concern
7
+
8
+ included do
9
+ cattr_accessor :inheritance_mapping, instance_accessor: false
10
+ self.inheritance_mapping = {}
11
+ end
12
+
13
+ def as_json(options = {})
14
+ json = super(options)
15
+ col = self.class.inheritance_column
16
+ json[col] = send(col)
17
+ json
18
+ end
19
+
20
+ module ClassMethods
21
+
22
+ # http://apidock.com/rails/ActiveRecord/Inheritance/ClassMethods/sti_name
23
+ def sti_name
24
+ inheritance_mapping.key(super)
25
+ end
26
+
27
+ alias inheritance_type sti_name
28
+
29
+ # http://apidock.com/rails/ActiveRecord/Inheritance/ClassMethods/find_sti_class
30
+ def find_sti_class(type)
31
+ lookup = inheritance_mapping[type.to_i]
32
+ lookup ? super(lookup) : super
33
+ end
34
+
35
+ private
36
+ # lib/active_record/inheritance.rb
37
+ def subclass_from_attributes(attrs)
38
+ col = inheritance_column
39
+ type = col.is_a?(Symbol) ? attrs[col] || attrs[col.to_s] : attrs[col] || attrs[col.to_sym]
40
+
41
+ return if type.nil?
42
+
43
+ return if type == inheritance_type
44
+
45
+ subclass = inheritance_mapping[type].safe_constantize
46
+ unless descendants.include?(subclass)
47
+ raise ActiveRecord::SubclassNotFound, %{ Invalid single-table inheritance type:
48
+ either subclass can't be mapped to #{type}
49
+ either #{subclass} is not a subclass of #{name} }.squish
50
+ end
51
+ subclass
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,22 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ module IntegerInheritance
5
+ module Macro
6
+ extend ActiveSupport::Concern
7
+
8
+ module ClassMethods
9
+ def uses_integer_inheritance(options = {})
10
+ include IntegerInheritance::Extension unless respond_to?(:inheritance_mapping)
11
+
12
+ self.inheritance_column = options.fetch(:column, IntegerInheritance.default_column)
13
+
14
+ # This is workaround for ActionDispatch::Reloader (in development environment)
15
+ class_name = name
16
+ mapping = IntegerInheritance.mappings[class_name]
17
+ self.inheritance_mapping = mapping if mapping
18
+ IntegerInheritance.mappings[class_name] = self.inheritance_mapping
19
+ end
20
+ end
21
+ end
22
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: integer-inheritance
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Yaroslav Konoplov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-10-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '6.0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '3.0'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '6.0'
33
+ - !ruby/object:Gem::Dependency
34
+ name: activerecord
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '3.0'
40
+ - - "<"
41
+ - !ruby/object:Gem::Version
42
+ version: '6.0'
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '3.0'
50
+ - - "<"
51
+ - !ruby/object:Gem::Version
52
+ version: '6.0'
53
+ description: Single table inheritance mechanism built on integer types
54
+ email:
55
+ - eahome00@gmail.com
56
+ executables: []
57
+ extensions: []
58
+ extra_rdoc_files: []
59
+ files:
60
+ - ".gitignore"
61
+ - Gemfile
62
+ - LICENSE.txt
63
+ - README.md
64
+ - integer-inheritance.gemspec
65
+ - lib/integer-inheritance.rb
66
+ - lib/integer-inheritance/column_types.rb
67
+ - lib/integer-inheritance/evaluator.rb
68
+ - lib/integer-inheritance/extension.rb
69
+ - lib/integer-inheritance/macro.rb
70
+ homepage: http://github.com/yivo/integer-inheritance
71
+ licenses:
72
+ - MIT
73
+ metadata: {}
74
+ post_install_message:
75
+ rdoc_options: []
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ requirements: []
89
+ rubyforge_project:
90
+ rubygems_version: 2.5.1
91
+ signing_key:
92
+ specification_version: 4
93
+ summary: Single table inheritance mechanism built on integer types
94
+ test_files: []