inherited_class_var 0.1.1 → 0.2.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 11b94c5852279a71dac8b6d886a9c15a536b9a8f
4
- data.tar.gz: b2a9b2bc666ef7c34630363bd3d9ceaf98317e01
3
+ metadata.gz: 7e265f14ae462bf2896bb319cd5652912b583e13
4
+ data.tar.gz: cf4cc16e671cb1de045ebb1033677cb798fade78
5
5
  SHA512:
6
- metadata.gz: 84e87a0974eaa0f97be23329bd79e1156bcf091db69bfcacc7a121136a2b5c0ffb1c3ea62d395dfa8febb9e5e7486b1617f89f7160b8c107c69bd754e9200ecc
7
- data.tar.gz: 38873074a0667a0b75e0a2eb0c7bddc4a272f4c1a1431be6ee0be7ac3f8cabe7bd2ce16f26ca948a77acb597d8828d6808b5120f14b36b69913370feb102866d
6
+ metadata.gz: 5cd98dfc7200e8f5b4a1c911dcbe0ccb78568d0477d744d03762b6e2d52ae3765f0800fa50a56572fa5572e9476332a28076e6c0e4272c4095139a1addc9e15b
7
+ data.tar.gz: 965c29758a5896359733275d8faf86c9cfb30d8f8108e9b275fef11351af0c56f19d6b457bbde88d1b32f811c68cca5903900b4db2a5efd98af41134eca9070e
data/.gitignore CHANGED
@@ -7,3 +7,4 @@
7
7
  /pkg/
8
8
  /spec/reports/
9
9
  /tmp/
10
+ /.idea/*
data/.rspec CHANGED
@@ -1,3 +1,2 @@
1
1
  --color
2
- --fail-fast
3
2
  --format documentation
data/Gemfile CHANGED
@@ -3,6 +3,9 @@ source 'https://rubygems.org'
3
3
  # Specify your gem's dependencies in inherited_class_var.gemspec
4
4
  gemspec
5
5
 
6
+ gem "bundler", "> 1.3"
7
+ gem "rake", "~> 10.0"
8
+
6
9
  group :test do
7
10
  gem 'rspec'
8
11
  gem 'coveralls'
@@ -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', '~> 4.2'
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
@@ -1,3 +1,3 @@
1
1
  module InheritedClassVar
2
- VERSION = '0.1.1'
2
+ VERSION = '0.2.0'
3
3
  end
@@ -1,76 +1,44 @@
1
- require 'inherited_class_var/version'
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/invalid_options'
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
- included do
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, options={})
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
- current_value = default_value
96
-
97
- ancestor_values = inherited_ancestors.map { |ancestor| ancestor.instance_variable_get(variable_name) }.compact
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
- # @param variable_name [Symbol] variable_name to cache against
108
- # @return [String] the cache variable name for the cache
109
- def inherited_class_variable_name(variable_name)
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
- # Memozies a inherited_class_variable_name
122
- # @param variable_name [Symbol] variable_name to cache against
123
- def class_cache(variable_name)
124
- #
125
- # equal to: (has @)inherited_class_variable_name ||= yield
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.1.1
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-02-18 00:00:00.000000000 Z
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/invalid_options.rb
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.4.5.1
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
@@ -1,3 +0,0 @@
1
- ### VERSION 0.1.0
2
-
3
- * Skeleton and first parsing
data/UPGRADE.md DELETED
@@ -1,3 +0,0 @@
1
- # Upgrading
2
-
3
- # Upgrading from xxx to xxx
@@ -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