inherited_class_var 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.rspec +0 -1
- data/Gemfile +3 -0
- data/inherited_class_var.gemspec +1 -2
- data/lib/inherited_class_var/cache.rb +54 -0
- data/lib/inherited_class_var/version.rb +1 -1
- data/lib/inherited_class_var.rb +33 -74
- metadata +4 -20
- data/CHANGELOG.md +0 -3
- data/UPGRADE.md +0 -3
- data/lib/inherited_class_var/invalid_options.rb +0 -13
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7e265f14ae462bf2896bb319cd5652912b583e13
|
4
|
+
data.tar.gz: cf4cc16e671cb1de045ebb1033677cb798fade78
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5cd98dfc7200e8f5b4a1c911dcbe0ccb78568d0477d744d03762b6e2d52ae3765f0800fa50a56572fa5572e9476332a28076e6c0e4272c4095139a1addc9e15b
|
7
|
+
data.tar.gz: 965c29758a5896359733275d8faf86c9cfb30d8f8108e9b275fef11351af0c56f19d6b457bbde88d1b32f811c68cca5903900b4db2a5efd98af41134eca9070e
|
data/.gitignore
CHANGED
data/.rspec
CHANGED
data/Gemfile
CHANGED
data/inherited_class_var.gemspec
CHANGED
@@ -16,6 +16,5 @@ Gem::Specification.new do |spec|
|
|
16
16
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
17
17
|
spec.require_paths = ['lib']
|
18
18
|
|
19
|
-
spec.add_dependency 'activesupport',
|
20
|
-
spec.add_development_dependency 'rake', '~> 10.0'
|
19
|
+
spec.add_dependency 'activesupport', '~> 4.2'
|
21
20
|
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
#
|
2
|
+
# Methods to help cache the inherited variable
|
3
|
+
#
|
4
|
+
# Given:
|
5
|
+
# Parent -> Child
|
6
|
+
#
|
7
|
+
# and Parent.send(:inherited_class_hash, :some_var)
|
8
|
+
#
|
9
|
+
# Parent.merge_some_var(a: 1)
|
10
|
+
# Child.some_var # => { a: 1 }, this requires going up the ancestors (Parent, Grandparent, etc.) and merging, so we cache the result
|
11
|
+
# Child.some_var # => { a: 1 }, cached, no calculation
|
12
|
+
#
|
13
|
+
# Parent.merge_some_var(b: 2) # => this should clear the cache for all descendents
|
14
|
+
# Child.some_var # => { a: 1, b: 2 } # => cache was cleared, so recalculate via merging up ancestors
|
15
|
+
#
|
16
|
+
module InheritedClassVar
|
17
|
+
module Cache
|
18
|
+
extend ActiveSupport::Concern
|
19
|
+
|
20
|
+
class_methods do
|
21
|
+
|
22
|
+
# Clears the cache for a variable (must be public)
|
23
|
+
# @param variable_name [Symbol] variable_name to cache against
|
24
|
+
def clear_class_cache(variable_name)
|
25
|
+
instance_variable_set inherited_class_variable_name(variable_name), nil
|
26
|
+
end
|
27
|
+
|
28
|
+
protected
|
29
|
+
# Memozies a inherited_class_variable_name
|
30
|
+
# @param variable_name [Symbol] variable_name to cache against
|
31
|
+
def class_cache(variable_name)
|
32
|
+
#
|
33
|
+
# equal to: (has @)inherited_class_variable_name ||= yield
|
34
|
+
#
|
35
|
+
cache_variable_name = inherited_class_variable_name(variable_name)
|
36
|
+
instance_variable_get(cache_variable_name) || instance_variable_set(cache_variable_name, yield)
|
37
|
+
end
|
38
|
+
|
39
|
+
# Clears the cache for a variable and the same variable for all it's dependant descendants
|
40
|
+
# @param variable_name [Symbol] variable_name to cache against
|
41
|
+
def deep_clear_class_cache(variable_name)
|
42
|
+
([self] + descendants).each do |descendant|
|
43
|
+
descendant.try(:clear_class_cache, variable_name)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
# @param variable_name [Symbol] variable_name to cache against
|
48
|
+
# @return [String] the cache variable name for the cache
|
49
|
+
def inherited_class_variable_name(variable_name)
|
50
|
+
:"#{variable_name}_inherited_class_cache"
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
data/lib/inherited_class_var.rb
CHANGED
@@ -1,76 +1,44 @@
|
|
1
|
-
require '
|
2
|
-
|
3
|
-
require 'active_support/concern'
|
4
|
-
require 'active_support/dependencies/autoload'
|
5
|
-
require 'active_support/core_ext'
|
6
|
-
require 'active_support/json'
|
1
|
+
require 'active_support/all'
|
7
2
|
|
8
|
-
require 'inherited_class_var/
|
3
|
+
require 'inherited_class_var/version'
|
4
|
+
require 'inherited_class_var/cache'
|
9
5
|
|
10
6
|
module InheritedClassVar
|
11
7
|
extend ActiveSupport::Concern
|
12
8
|
|
13
|
-
|
14
|
-
include InvalidOptions
|
15
|
-
end
|
9
|
+
include Cache
|
16
10
|
|
17
11
|
class_methods do
|
18
|
-
# Clears the cache for a variable
|
19
|
-
# @param variable_name [Symbol] variable_name to cache against
|
20
|
-
def clear_class_cache(variable_name)
|
21
|
-
instance_variable_set inherited_class_variable_name(variable_name), nil
|
22
|
-
end
|
23
|
-
|
24
12
|
protected
|
25
13
|
|
14
|
+
#
|
15
|
+
# Easy Open API
|
16
|
+
#
|
17
|
+
|
26
18
|
# @param variable_name [Symbol] class variable name
|
27
19
|
# @option options [Array] :dependencies array of dependent method names
|
28
|
-
def inherited_class_hash(variable_name
|
29
|
-
options = check_and_merge_options(options, dependencies: [])
|
30
|
-
|
31
|
-
options[:dependencies].each do |dependency_name|
|
32
|
-
define_singleton_method dependency_name do
|
33
|
-
class_cache(hidden_variable_name(dependency_name)) do
|
34
|
-
send("_#{dependency_name}")
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
20
|
+
def inherited_class_hash(variable_name)
|
39
21
|
hidden_variable_name = hidden_variable_name(variable_name)
|
40
22
|
|
41
23
|
define_singleton_method variable_name do
|
42
24
|
inherited_class_var(hidden_variable_name, {}, :merge)
|
43
25
|
end
|
44
26
|
|
45
|
-
define_singleton_method "merge_#{variable_name}" do |merge_value|
|
27
|
+
define_singleton_method :"merge_#{variable_name}" do |merge_value|
|
46
28
|
value = instance_variable_get(hidden_variable_name) || instance_variable_set(hidden_variable_name, {})
|
47
|
-
|
48
29
|
deep_clear_class_cache(hidden_variable_name)
|
49
|
-
options[:dependencies].each { |dependency_name| deep_clear_class_cache(hidden_variable_name(dependency_name)) }
|
50
|
-
|
51
30
|
value.merge!(merge_value)
|
52
31
|
end
|
53
32
|
end
|
54
33
|
|
55
|
-
# @param variable_name [Symbol] class variable name based on
|
56
|
-
# @return [Symbol] the hidden variable name for class_cache
|
57
|
-
def hidden_variable_name(variable_name)
|
58
|
-
"@_#{variable_name}".to_sym
|
59
|
-
end
|
60
|
-
|
61
|
-
# @param included_module [Module] module to search for
|
62
|
-
# @return [Array<Module>] inherited_ancestors of included_module (including self)
|
63
|
-
def inherited_ancestors(included_module=InheritedClassVar)
|
64
|
-
included_model_index = ancestors.index(included_module)
|
65
|
-
included_model_index == 0 ? [included_module] : (ancestors[0..(included_model_index - 1)] - [InvalidOptions])
|
66
|
-
end
|
67
|
-
|
68
34
|
# @param accessor_method_name [Symbol] method to access the inherited_custom_class
|
69
35
|
# @param base_parent_class [Class] class that the custom class inherits from if there's no parent
|
70
36
|
# @return [Class] a custom class with the inheritance following self. for example:
|
71
37
|
#
|
72
38
|
# grandparent -> parent -> self
|
73
39
|
#
|
40
|
+
# we want self::inherited_custom_class to inherit from inherited_custom_class of all the ancestors
|
41
|
+
#
|
74
42
|
# grandparent has inherited_custom_class, but parent, doesn't.
|
75
43
|
#
|
76
44
|
# then: base_parent_class -> grandparent::inherited_custom_class -> self::inherited_custom_class
|
@@ -86,46 +54,37 @@ module InheritedClassVar
|
|
86
54
|
klass
|
87
55
|
end
|
88
56
|
|
57
|
+
#
|
58
|
+
# Helpers to make different types of inherited class variables
|
59
|
+
#
|
60
|
+
|
61
|
+
# @param variable_name [Symbol] class variable name based on
|
62
|
+
# @return [Symbol] the hidden variable name for class variable
|
63
|
+
def hidden_variable_name(variable_name)
|
64
|
+
:"@_#{variable_name}"
|
65
|
+
end
|
66
|
+
|
89
67
|
# @param variable_name [Symbol] class variable name (recommend :@_variable_name)
|
90
68
|
# @param default_value [Object] default value of the class variable
|
91
69
|
# @param merge_method [Symbol] method to merge values of the class variable
|
92
70
|
# @return [Object] a class variable merged across ancestors until inherited_class_module
|
93
71
|
def inherited_class_var(variable_name, default_value, merge_method)
|
94
72
|
class_cache(variable_name) do
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
ancestor_values.each do |ancestor_value|
|
100
|
-
current_value = current_value.public_send(merge_method, ancestor_value)
|
101
|
-
end
|
102
|
-
|
103
|
-
current_value
|
73
|
+
inherited_ancestors.map { |ancestor| ancestor.instance_variable_get(variable_name) }
|
74
|
+
.compact
|
75
|
+
.reduce(default_value, merge_method)
|
104
76
|
end
|
105
77
|
end
|
106
78
|
|
107
|
-
#
|
108
|
-
#
|
109
|
-
|
110
|
-
"#{variable_name}_inherited_class_cache"
|
111
|
-
end
|
112
|
-
|
113
|
-
# Clears the cache for a variable and the same variable for all it's dependant descendants
|
114
|
-
# @param variable_name [Symbol] variable_name to cache against
|
115
|
-
def deep_clear_class_cache(variable_name)
|
116
|
-
([self] + descendants).each do |descendant|
|
117
|
-
descendant.try(:clear_class_cache, variable_name)
|
118
|
-
end
|
119
|
-
end
|
79
|
+
#
|
80
|
+
# More Helpers
|
81
|
+
#
|
120
82
|
|
121
|
-
#
|
122
|
-
# @
|
123
|
-
def
|
124
|
-
|
125
|
-
|
126
|
-
#
|
127
|
-
cache_variable_name = inherited_class_variable_name(variable_name)
|
128
|
-
instance_variable_get(cache_variable_name) || instance_variable_set(cache_variable_name, yield)
|
83
|
+
# @param included_module [Module] module to search for
|
84
|
+
# @return [Array<Module>] inherited_ancestors of included_module (including self)
|
85
|
+
def inherited_ancestors(included_module=InheritedClassVar)
|
86
|
+
included_model_index = ancestors.index(included_module)
|
87
|
+
included_model_index == 0 ? [included_module] : (ancestors[0..(included_model_index - 1)])
|
129
88
|
end
|
130
89
|
end
|
131
90
|
end
|
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.
|
4
|
+
version: 0.2.0
|
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: 2016-
|
12
|
+
date: 2016-05-31 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: rake
|
30
|
-
requirement: !ruby/object:Gem::Requirement
|
31
|
-
requirements:
|
32
|
-
- - "~>"
|
33
|
-
- !ruby/object:Gem::Version
|
34
|
-
version: '10.0'
|
35
|
-
type: :development
|
36
|
-
prerelease: false
|
37
|
-
version_requirements: !ruby/object:Gem::Requirement
|
38
|
-
requirements:
|
39
|
-
- - "~>"
|
40
|
-
- !ruby/object:Gem::Version
|
41
|
-
version: '10.0'
|
42
28
|
description: Let inherited class var to authorize inheritance
|
43
29
|
email:
|
44
30
|
- hello@stevenchung.ca
|
@@ -51,18 +37,16 @@ files:
|
|
51
37
|
- ".gitignore"
|
52
38
|
- ".rspec"
|
53
39
|
- ".travis.yml"
|
54
|
-
- CHANGELOG.md
|
55
40
|
- CODE_OF_CONDUCT.md
|
56
41
|
- Gemfile
|
57
42
|
- LICENSE.txt
|
58
43
|
- README.md
|
59
44
|
- Rakefile
|
60
|
-
- UPGRADE.md
|
61
45
|
- bin/console
|
62
46
|
- bin/setup
|
63
47
|
- inherited_class_var.gemspec
|
64
48
|
- lib/inherited_class_var.rb
|
65
|
-
- lib/inherited_class_var/
|
49
|
+
- lib/inherited_class_var/cache.rb
|
66
50
|
- lib/inherited_class_var/version.rb
|
67
51
|
homepage: https://github.com/FinalCAD/inherited_class_var
|
68
52
|
licenses:
|
@@ -84,7 +68,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
84
68
|
version: '0'
|
85
69
|
requirements: []
|
86
70
|
rubyforge_project:
|
87
|
-
rubygems_version: 2.
|
71
|
+
rubygems_version: 2.5.1
|
88
72
|
signing_key:
|
89
73
|
specification_version: 4
|
90
74
|
summary: Let inherited class var
|
data/CHANGELOG.md
DELETED
data/UPGRADE.md
DELETED
@@ -1,13 +0,0 @@
|
|
1
|
-
module InvalidOptions
|
2
|
-
extend ActiveSupport::Concern
|
3
|
-
|
4
|
-
class_methods do
|
5
|
-
protected
|
6
|
-
def check_and_merge_options(options, default_options)
|
7
|
-
invalid_options = options.keys - default_options.keys
|
8
|
-
raise ArgumentError.new("Invalid option(s): #{invalid_options}") if invalid_options.present?
|
9
|
-
|
10
|
-
options.reverse_merge(default_options)
|
11
|
-
end
|
12
|
-
end
|
13
|
-
end
|