inherited_class_var 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1b3a4825303f3b5e5a3b941ce7a6c7fe45481b37
4
+ data.tar.gz: 6353e89d09cae6c90675b82ebaec3c99aced9cf4
5
+ SHA512:
6
+ metadata.gz: 940ef48e70e3fcff95ccc8975ef7b8aa0c629738f4fc38fd184b4fa252f475247f4cbb844373d6995480d81c42c00e7a3ad9b2c87146d379073b1c07434224c1
7
+ data.tar.gz: d29c1cf3e940b328c3f76d3860a645086200400be261f3c3034825bfc9f3137763a89020ec968606d1213c45729cc9490a8cdfb52068609452c53f6bed890534
data/.coveralls.yml ADDED
@@ -0,0 +1 @@
1
+ service_name: travis-ci
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --fail-fast
3
+ --format documentation
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ csv2hash
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.2.2
data/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.3
4
+ - ruby-head
5
+ before_install: gem install bundler -v 1.10.6
6
+ matrix:
7
+ allow_failures:
8
+ - rvm: ruby-head
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ ### VERSION 0.1.0
2
+
3
+ * Skeleton and first parsing
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in inherited_class_var.gemspec
4
+ gemspec
5
+
6
+ group :test do
7
+ gem 'coveralls', require: false
8
+ gem 'pry'
9
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Joel AZEMAR
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.
data/README.md ADDED
@@ -0,0 +1,49 @@
1
+ # InheritedClassVar
2
+
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
+
5
+ [![Code Climate](https://codeclimate.com/github/joel/inherited_class_var.png)](https://codeclimate.com/github/joel/inherited_class_var)
6
+
7
+ [![Dependency Status](https://gemnasium.com/joel/inherited_class_var.svg)](https://gemnasium.com/joel/inherited_class_var)
8
+
9
+ [![Build Status](https://travis-ci.org/joel/inherited_class_var.svg?branch=master)](https://travis-ci.org/joel/inherited_class_var) (Travis CI)
10
+
11
+ [![Coverage Status](https://coveralls.io/repos/joel/inherited_class_var/badge.svg?branch=master&service=github)](https://coveralls.io/github/joel/inherited_class_var?branch=master)
12
+
13
+ [![Gem Version](https://badge.fury.io/rb/inherited_class_var.svg)](http://badge.fury.io/rb/inherited_class_var)
14
+
15
+
16
+ ## Installation
17
+
18
+ Add this line to your application's Gemfile:
19
+
20
+ ```ruby
21
+ gem 'inherited_class_var'
22
+ ```
23
+
24
+ And then execute:
25
+
26
+ $ bundle
27
+
28
+ Or install it yourself as:
29
+
30
+ $ gem install inherited_class_var
31
+
32
+ ## Usage
33
+
34
+ TODO: Write usage instructions here
35
+
36
+ ## Development
37
+
38
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
39
+
40
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
41
+
42
+ ## Contributing
43
+
44
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/inherited_class_var. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
45
+
46
+
47
+ ## License
48
+
49
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
data/UPGRADE.md ADDED
@@ -0,0 +1,3 @@
1
+ # Upgrading
2
+
3
+ # Upgrading from xxx to xxx
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'inherited_class_var'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require 'pry'
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,32 @@
1
+ require_relative 'lib/inherited_class_var/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = 'inherited_class_var'
5
+ spec.version = InheritedClassVar::VERSION
6
+ spec.authors = ['Steve Chung', 'Joel AZEMAR']
7
+ spec.email = ['hello@stevenchung.ca','joel.azemar@gmail.com']
8
+
9
+ spec.summary = %q{Let inherited class var}
10
+ spec.description = %q{Let inherited class var to authorize inheritance}
11
+ spec.homepage = 'https://github.com/FinalCAD/inherited_class_var'
12
+ spec.license = 'MIT'
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
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
23
+ spec.bindir = 'exe'
24
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
25
+ spec.require_paths = ['lib']
26
+
27
+ spec.add_dependency 'activesupport', '~> 4.2'
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'
32
+ end
@@ -0,0 +1,13 @@
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
@@ -0,0 +1,3 @@
1
+ module InheritedClassVar
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,128 @@
1
+ require 'inherited_class_var/version'
2
+
3
+ require 'active_support/concern'
4
+ # require 'active_support/core_ext/module/deprecation' # https://github.com/rails/rails/commit/1e62457a6d4307edc026591f8c8a8335d9627768
5
+ require 'active_support/dependencies/autoload'
6
+ require 'active_support/core_ext'
7
+ require 'active_support/json'
8
+
9
+ require 'inherited_class_var/invalid_options'
10
+
11
+ module InheritedClassVar
12
+ extend ActiveSupport::Concern
13
+
14
+ included do
15
+ include InvalidOptions
16
+ end
17
+
18
+ class_methods do
19
+ # Clears the cache for a variable
20
+ # @param variable_name [Symbol] variable_name to cache against
21
+ def clear_class_cache(variable_name)
22
+ instance_variable_set inherited_class_variable_name(variable_name), nil
23
+ end
24
+
25
+ protected
26
+
27
+ # @param variable_name [Symbol] class variable name
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
+
39
+ hidden_variable_name = hidden_variable_name(variable_name)
40
+ define_singleton_method variable_name do
41
+ inherited_class_var(hidden_variable_name, {}, :merge)
42
+ end
43
+ define_singleton_method "merge_#{variable_name}" do |merge_value|
44
+ value = instance_variable_get(hidden_variable_name) || instance_variable_set(hidden_variable_name, {})
45
+
46
+ deep_clear_class_cache(hidden_variable_name)
47
+ options[:dependencies].each {|dependency_name| deep_clear_class_cache(hidden_variable_name(dependency_name)) }
48
+
49
+ value.merge!(merge_value)
50
+ end
51
+ end
52
+
53
+ # @param variable_name [Symbol] class variable name based on
54
+ # @return [Symbol] the hidden variable name for class_cache
55
+ def hidden_variable_name(variable_name)
56
+ "@_#{variable_name}".to_sym
57
+ end
58
+
59
+ # @param included_module [Module] module to search for
60
+ # @return [Array<Module>] inherited_ancestors of included_module (including self)
61
+ def inherited_ancestors(included_module=InheritedClassVar)
62
+ included_model_index = ancestors.index(included_module)
63
+ included_model_index == 0 ? [included_module] : ancestors[0..(included_model_index - 1)]
64
+ end
65
+
66
+ # @param accessor_method_name [Symbol] method to access the inherited_custom_class
67
+ # @param base_parent_class [Class] class that the custom class inherits from if there's no parent
68
+ # @return [Class] a custom class with the inheritance following self. for example:
69
+ #
70
+ # grandparent -> parent -> self
71
+ #
72
+ # grandparent has inherited_custom_class, but parent, doesn't.
73
+ #
74
+ # then: base_parent_class -> grandparent::inherited_custom_class -> self::inherited_custom_class
75
+ def inherited_custom_class(accessor_method_name, base_parent_class)
76
+ parent_class = inherited_ancestors[1..-1].find do |klass|
77
+ klass.respond_to?(accessor_method_name)
78
+ end.try(accessor_method_name)
79
+ parent_class ||= base_parent_class
80
+
81
+ klass = Class.new(parent_class)
82
+ # how else can i get the current scopes name...
83
+ klass.send(:define_singleton_method, :name, &eval("-> { \"#{name}#{base_parent_class.name.demodulize}\" }"))
84
+ klass
85
+ end
86
+
87
+ # @param variable_name [Symbol] class variable name (recommend :@_variable_name)
88
+ # @param default_value [Object] default value of the class variable
89
+ # @param merge_method [Symbol] method to merge values of the class variable
90
+ # @return [Object] a class variable merged across ancestors until inherited_class_module
91
+ def inherited_class_var(variable_name, default_value, merge_method)
92
+ class_cache(variable_name) do
93
+ value = default_value
94
+
95
+ inherited_ancestors.each do |ancestor|
96
+ ancestor_value = ancestor.instance_variable_get(variable_name)
97
+ value = ancestor_value.public_send(merge_method, value) if ancestor_value.present?
98
+ end
99
+
100
+ value
101
+ end
102
+ end
103
+
104
+ # @param variable_name [Symbol] variable_name to cache against
105
+ # @return [String] the cache variable name for the cache
106
+ def inherited_class_variable_name(variable_name)
107
+ "#{variable_name}_inherited_class_cache"
108
+ end
109
+
110
+ # Clears the cache for a variable and the same variable for all it's dependant descendants
111
+ # @param variable_name [Symbol] variable_name to cache against
112
+ def deep_clear_class_cache(variable_name)
113
+ ([self] + descendants).each do |descendant|
114
+ descendant.try(:clear_class_cache, variable_name)
115
+ end
116
+ end
117
+
118
+ # Memozies a inherited_class_variable_name
119
+ # @param variable_name [Symbol] variable_name to cache against
120
+ def class_cache(variable_name)
121
+ #
122
+ # equal to: (has @)inherited_class_variable_name ||= yield
123
+ #
124
+ cache_variable_name = inherited_class_variable_name(variable_name)
125
+ instance_variable_get(cache_variable_name) || instance_variable_set(cache_variable_name, yield)
126
+ end
127
+ end
128
+ end
metadata ADDED
@@ -0,0 +1,121 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: inherited_class_var
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Steve Chung
8
+ - Joel AZEMAR
9
+ autorequire:
10
+ bindir: exe
11
+ cert_chain: []
12
+ date: 2015-11-04 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '4.2'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
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
+ - !ruby/object:Gem::Dependency
43
+ name: rake
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '10.0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ 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
+ description: Let inherited class var to authorize inheritance
71
+ email:
72
+ - hello@stevenchung.ca
73
+ - joel.azemar@gmail.com
74
+ executables: []
75
+ extensions: []
76
+ extra_rdoc_files: []
77
+ files:
78
+ - ".coveralls.yml"
79
+ - ".gitignore"
80
+ - ".rspec"
81
+ - ".ruby-gemset"
82
+ - ".ruby-version"
83
+ - ".travis.yml"
84
+ - CHANGELOG.md
85
+ - CODE_OF_CONDUCT.md
86
+ - Gemfile
87
+ - LICENSE.txt
88
+ - README.md
89
+ - Rakefile
90
+ - UPGRADE.md
91
+ - bin/console
92
+ - bin/setup
93
+ - inherited_class_var.gemspec
94
+ - lib/inherited_class_var.rb
95
+ - lib/inherited_class_var/invalid_options.rb
96
+ - lib/inherited_class_var/version.rb
97
+ homepage: https://github.com/FinalCAD/inherited_class_var
98
+ licenses:
99
+ - MIT
100
+ metadata: {}
101
+ post_install_message:
102
+ rdoc_options: []
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ required_rubygems_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ requirements: []
116
+ rubyforge_project:
117
+ rubygems_version: 2.4.6
118
+ signing_key:
119
+ specification_version: 4
120
+ summary: Let inherited class var
121
+ test_files: []