inherited_class_var 0.1.0 → 0.1.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.
- checksums.yaml +4 -4
- data/Gemfile +3 -2
- data/README.md +56 -5
- data/inherited_class_var.gemspec +2 -13
- data/lib/inherited_class_var/version.rb +1 -1
- data/lib/inherited_class_var.rb +11 -8
- metadata +3 -33
- data/.ruby-gemset +0 -1
- data/.ruby-version +0 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 11b94c5852279a71dac8b6d886a9c15a536b9a8f
|
4
|
+
data.tar.gz: b2a9b2bc666ef7c34630363bd3d9ceaf98317e01
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 84e87a0974eaa0f97be23329bd79e1156bcf091db69bfcacc7a121136a2b5c0ffb1c3ea62d395dfa8febb9e5e7486b1617f89f7160b8c107c69bd754e9200ecc
|
7
|
+
data.tar.gz: 38873074a0667a0b75e0a2eb0c7bddc4a272f4c1a1431be6ee0be7ac3f8cabe7bd2ce16f26ca948a77acb597d8828d6808b5120f14b36b69913370feb102866d
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -2,13 +2,13 @@
|
|
2
2
|
|
3
3
|
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/inherited_class_var`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
4
|
|
5
|
-
[](https://codeclimate.com/github/FinalCAD/inherited_class_var)
|
6
6
|
|
7
|
-
[](https://gemnasium.com/FinalCAD/inherited_class_var)
|
8
8
|
|
9
|
-
[](https://travis-ci.org/FinalCAD/inherited_class_var) (Travis CI)
|
10
10
|
|
11
|
-
[](https://coveralls.io/github/FinalCAD/inherited_class_var?branch=master)
|
12
12
|
|
13
13
|
[](http://badge.fury.io/rb/inherited_class_var)
|
14
14
|
|
@@ -31,7 +31,58 @@ Or install it yourself as:
|
|
31
31
|
|
32
32
|
## Usage
|
33
33
|
|
34
|
-
|
34
|
+
You can follow this example, if you want to create Models with columns information and these informations still available after inheritance
|
35
|
+
|
36
|
+
Create a Model module with `inherited_class_var`
|
37
|
+
|
38
|
+
```
|
39
|
+
require 'inherited_class_var'
|
40
|
+
|
41
|
+
module Model
|
42
|
+
extend ActiveSupport::Concern
|
43
|
+
|
44
|
+
included do
|
45
|
+
include InheritedClassVar
|
46
|
+
inherited_class_hash :columns
|
47
|
+
end
|
48
|
+
|
49
|
+
module ClassMethods
|
50
|
+
|
51
|
+
protected
|
52
|
+
|
53
|
+
def column(column_name, options={})
|
54
|
+
merge_columns(column_name.to_sym => options)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
```
|
59
|
+
|
60
|
+
`merge_columns` it's bring by `inherited_class_var`
|
61
|
+
|
62
|
+
```
|
63
|
+
class ModelBase
|
64
|
+
include Model
|
65
|
+
|
66
|
+
column :id, type: Integer
|
67
|
+
end
|
68
|
+
```
|
69
|
+
|
70
|
+
Gives
|
71
|
+
```
|
72
|
+
ModelBase.columns # => {:id=>{:type=>Integer}}
|
73
|
+
```
|
74
|
+
|
75
|
+
```
|
76
|
+
class UserModel < ModelBase
|
77
|
+
|
78
|
+
column :name, type: String
|
79
|
+
end
|
80
|
+
```
|
81
|
+
|
82
|
+
Gives
|
83
|
+
```
|
84
|
+
UserModel.columns # => {:id=>{:type=>Integer}, :name=>{:type=>String}}
|
85
|
+
```
|
35
86
|
|
36
87
|
## Development
|
37
88
|
|
data/inherited_class_var.gemspec
CHANGED
@@ -11,22 +11,11 @@ Gem::Specification.new do |spec|
|
|
11
11
|
spec.homepage = 'https://github.com/FinalCAD/inherited_class_var'
|
12
12
|
spec.license = 'MIT'
|
13
13
|
|
14
|
-
# Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
|
15
|
-
# delete this section to allow pushing this gem to any host.
|
16
|
-
# if spec.respond_to?(:metadata)
|
17
|
-
# spec.metadata['allowed_push_host'] = 'TODO: Set to 'http://mygemserver.com''
|
18
|
-
# else
|
19
|
-
# raise 'RubyGems 2.0 or newer is required to protect against public gem pushes.'
|
20
|
-
# end
|
21
|
-
|
22
14
|
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
23
15
|
spec.bindir = 'exe'
|
24
16
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
25
17
|
spec.require_paths = ['lib']
|
26
18
|
|
27
|
-
spec.add_dependency 'activesupport',
|
28
|
-
|
29
|
-
spec.add_development_dependency 'bundler', '~> 1.10'
|
30
|
-
spec.add_development_dependency 'rake', '~> 10.0'
|
31
|
-
spec.add_development_dependency 'rspec', '~> 3.3'
|
19
|
+
spec.add_dependency 'activesupport', '~> 4.2'
|
20
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
32
21
|
end
|
data/lib/inherited_class_var.rb
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
require 'inherited_class_var/version'
|
2
2
|
|
3
3
|
require 'active_support/concern'
|
4
|
-
# require 'active_support/core_ext/module/deprecation' # https://github.com/rails/rails/commit/1e62457a6d4307edc026591f8c8a8335d9627768
|
5
4
|
require 'active_support/dependencies/autoload'
|
6
5
|
require 'active_support/core_ext'
|
7
6
|
require 'active_support/json'
|
@@ -25,6 +24,7 @@ module InheritedClassVar
|
|
25
24
|
protected
|
26
25
|
|
27
26
|
# @param variable_name [Symbol] class variable name
|
27
|
+
# @option options [Array] :dependencies array of dependent method names
|
28
28
|
def inherited_class_hash(variable_name, options={})
|
29
29
|
options = check_and_merge_options(options, dependencies: [])
|
30
30
|
|
@@ -37,14 +37,16 @@ module InheritedClassVar
|
|
37
37
|
end
|
38
38
|
|
39
39
|
hidden_variable_name = hidden_variable_name(variable_name)
|
40
|
+
|
40
41
|
define_singleton_method variable_name do
|
41
42
|
inherited_class_var(hidden_variable_name, {}, :merge)
|
42
43
|
end
|
44
|
+
|
43
45
|
define_singleton_method "merge_#{variable_name}" do |merge_value|
|
44
46
|
value = instance_variable_get(hidden_variable_name) || instance_variable_set(hidden_variable_name, {})
|
45
47
|
|
46
48
|
deep_clear_class_cache(hidden_variable_name)
|
47
|
-
options[:dependencies].each {|dependency_name| deep_clear_class_cache(hidden_variable_name(dependency_name)) }
|
49
|
+
options[:dependencies].each { |dependency_name| deep_clear_class_cache(hidden_variable_name(dependency_name)) }
|
48
50
|
|
49
51
|
value.merge!(merge_value)
|
50
52
|
end
|
@@ -60,7 +62,7 @@ module InheritedClassVar
|
|
60
62
|
# @return [Array<Module>] inherited_ancestors of included_module (including self)
|
61
63
|
def inherited_ancestors(included_module=InheritedClassVar)
|
62
64
|
included_model_index = ancestors.index(included_module)
|
63
|
-
included_model_index == 0 ? [included_module] : ancestors[0..(included_model_index - 1)]
|
65
|
+
included_model_index == 0 ? [included_module] : (ancestors[0..(included_model_index - 1)] - [InvalidOptions])
|
64
66
|
end
|
65
67
|
|
66
68
|
# @param accessor_method_name [Symbol] method to access the inherited_custom_class
|
@@ -90,14 +92,15 @@ module InheritedClassVar
|
|
90
92
|
# @return [Object] a class variable merged across ancestors until inherited_class_module
|
91
93
|
def inherited_class_var(variable_name, default_value, merge_method)
|
92
94
|
class_cache(variable_name) do
|
93
|
-
|
95
|
+
current_value = default_value
|
96
|
+
|
97
|
+
ancestor_values = inherited_ancestors.map { |ancestor| ancestor.instance_variable_get(variable_name) }.compact
|
94
98
|
|
95
|
-
|
96
|
-
|
97
|
-
value = ancestor_value.public_send(merge_method, value) if ancestor_value.present?
|
99
|
+
ancestor_values.each do |ancestor_value|
|
100
|
+
current_value = current_value.public_send(merge_method, ancestor_value)
|
98
101
|
end
|
99
102
|
|
100
|
-
|
103
|
+
current_value
|
101
104
|
end
|
102
105
|
end
|
103
106
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: inherited_class_var
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Steve Chung
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: exe
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2016-02-18 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -25,20 +25,6 @@ dependencies:
|
|
25
25
|
- - "~>"
|
26
26
|
- !ruby/object:Gem::Version
|
27
27
|
version: '4.2'
|
28
|
-
- !ruby/object:Gem::Dependency
|
29
|
-
name: bundler
|
30
|
-
requirement: !ruby/object:Gem::Requirement
|
31
|
-
requirements:
|
32
|
-
- - "~>"
|
33
|
-
- !ruby/object:Gem::Version
|
34
|
-
version: '1.10'
|
35
|
-
type: :development
|
36
|
-
prerelease: false
|
37
|
-
version_requirements: !ruby/object:Gem::Requirement
|
38
|
-
requirements:
|
39
|
-
- - "~>"
|
40
|
-
- !ruby/object:Gem::Version
|
41
|
-
version: '1.10'
|
42
28
|
- !ruby/object:Gem::Dependency
|
43
29
|
name: rake
|
44
30
|
requirement: !ruby/object:Gem::Requirement
|
@@ -53,20 +39,6 @@ dependencies:
|
|
53
39
|
- - "~>"
|
54
40
|
- !ruby/object:Gem::Version
|
55
41
|
version: '10.0'
|
56
|
-
- !ruby/object:Gem::Dependency
|
57
|
-
name: rspec
|
58
|
-
requirement: !ruby/object:Gem::Requirement
|
59
|
-
requirements:
|
60
|
-
- - "~>"
|
61
|
-
- !ruby/object:Gem::Version
|
62
|
-
version: '3.3'
|
63
|
-
type: :development
|
64
|
-
prerelease: false
|
65
|
-
version_requirements: !ruby/object:Gem::Requirement
|
66
|
-
requirements:
|
67
|
-
- - "~>"
|
68
|
-
- !ruby/object:Gem::Version
|
69
|
-
version: '3.3'
|
70
42
|
description: Let inherited class var to authorize inheritance
|
71
43
|
email:
|
72
44
|
- hello@stevenchung.ca
|
@@ -78,8 +50,6 @@ files:
|
|
78
50
|
- ".coveralls.yml"
|
79
51
|
- ".gitignore"
|
80
52
|
- ".rspec"
|
81
|
-
- ".ruby-gemset"
|
82
|
-
- ".ruby-version"
|
83
53
|
- ".travis.yml"
|
84
54
|
- CHANGELOG.md
|
85
55
|
- CODE_OF_CONDUCT.md
|
@@ -114,7 +84,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
114
84
|
version: '0'
|
115
85
|
requirements: []
|
116
86
|
rubyforge_project:
|
117
|
-
rubygems_version: 2.4.
|
87
|
+
rubygems_version: 2.4.5.1
|
118
88
|
signing_key:
|
119
89
|
specification_version: 4
|
120
90
|
summary: Let inherited class var
|
data/.ruby-gemset
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
csv2hash
|
data/.ruby-version
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
2.2.2
|