inheritable_accessors 0.1.1 → 0.1.2
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/.rspec +1 -1
- data/.travis.yml +5 -0
- data/Gemfile +15 -0
- data/README.md +70 -9
- data/inheritable_accessors.gemspec +1 -9
- data/lib/inheritable_accessors.rb +1 -0
- data/lib/inheritable_accessors/inheritable_hash.rb +18 -2
- data/lib/inheritable_accessors/inheritable_option_accessor.rb +58 -0
- data/lib/inheritable_accessors/version.rb +1 -1
- metadata +5 -46
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 887a141cf629f263b1d5d359f1b6af90cc4babc3
|
4
|
+
data.tar.gz: 59d0fbd8c834134eef28fbe8dc351175cb8c9d4d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ab916ece20a1de9df4a889deb1ef26a85a9fb968abdc8b3d7fc92005783da5e023f565367b1506c529017ecfd0b77fa69ffc2448cc2888eec837f87d2b067b75
|
7
|
+
data.tar.gz: 12a193b47d6aae6d9c26c6de548685b260469057f20e083d26c576ff696c3833c5ab2e18f7e165f03d9ff9d63451335768619fff6bf52829dc6af0860e22aa6e
|
data/.rspec
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
--format
|
1
|
+
--format progress
|
2
2
|
--color
|
data/.travis.yml
CHANGED
data/Gemfile
CHANGED
@@ -1,4 +1,19 @@
|
|
1
1
|
source 'https://rubygems.org'
|
2
2
|
|
3
|
+
group :development do
|
4
|
+
gem "byebug"
|
5
|
+
end
|
6
|
+
|
7
|
+
group :development, :test do
|
8
|
+
gem "bundler", "~> 1.8"
|
9
|
+
gem "rake", "~> 10.0"
|
10
|
+
end
|
11
|
+
|
12
|
+
group :test do
|
13
|
+
gem "rspec", "~> 3.2"
|
14
|
+
end
|
15
|
+
|
16
|
+
|
17
|
+
|
3
18
|
# Specify your gem's dependencies in inheritable-accessors.gemspec
|
4
19
|
gemspec
|
data/README.md
CHANGED
@@ -1,15 +1,76 @@
|
|
1
|
-
# InheritableAccessors
|
1
|
+
# InheritableAccessors [](https://travis-ci.org/blakechambers/inheritable-accessors)
|
2
2
|
|
3
|
-
Easily and consistently inherit attributes and configurations. This is particularly designed to work well with Rspec, which creates new inherited classes during each context.
|
3
|
+
Easily and consistently inherit attributes and configurations. This is particularly designed to work well with Rspec, which creates new inherited classes during each context.
|
4
4
|
|
5
|
-
##
|
5
|
+
## Usage
|
6
|
+
|
7
|
+
Inheritable accessors comes with 3 inheritable components that can be used:
|
8
|
+
an iheritable hash, set, and option accessor.
|
9
|
+
|
10
|
+
### InheritableHashAccessor
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
class Parent
|
14
|
+
include InheritableAccessors::InheritableHashAccessor
|
15
|
+
|
16
|
+
inheritable_hash_accessor :options
|
17
|
+
|
18
|
+
options[:a] = 1
|
19
|
+
end
|
20
|
+
|
21
|
+
class Child < Parent
|
22
|
+
options[:b] = 2
|
23
|
+
end
|
24
|
+
|
25
|
+
item = Child.new
|
26
|
+
|
27
|
+
# deleted items do not affect parent items
|
28
|
+
item.delete(:a)
|
29
|
+
item.options[:c] = 3
|
30
|
+
|
31
|
+
Parent.options.to_hash
|
32
|
+
#=> {a: 1})
|
33
|
+
Child.options.to_hash
|
34
|
+
#=> {a: 1, b: 1})
|
35
|
+
item.options.to_hash
|
36
|
+
#=> {b: 2, c: 3})
|
37
|
+
```
|
38
|
+
|
39
|
+
### InheritableOptionAccessor
|
6
40
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
41
|
+
```ruby
|
42
|
+
class MyRequest
|
43
|
+
include InheritableAccessors::InheritableHashAccessor
|
44
|
+
include InheritableAccessors::InheritableOptionAccessor
|
45
|
+
|
46
|
+
inheritable_hash_accessor :request_opts
|
47
|
+
inheritable_option_accessor :path, :action, :params, for: :request_opts
|
48
|
+
|
49
|
+
# passing an argument to the accessor will "set" that value.
|
50
|
+
action "GET"
|
51
|
+
|
52
|
+
action # => "GET"
|
53
|
+
|
54
|
+
# passing a block will use the value of the block, but *only* when it is
|
55
|
+
# called. Block values will be memoized after the first run.
|
56
|
+
path { secret_path }
|
57
|
+
|
58
|
+
protected
|
59
|
+
|
60
|
+
def secret_path
|
61
|
+
"/secret_path"
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
65
|
+
|
66
|
+
|
67
|
+
request = MyRequest.new
|
68
|
+
request.path # => "/secret_path"
|
69
|
+
request.path "/other_path"
|
70
|
+
request.path # => "/other_path"
|
71
|
+
|
72
|
+
|
73
|
+
```
|
13
74
|
|
14
75
|
## Installation
|
15
76
|
|
@@ -9,10 +9,6 @@ Gem::Specification.new do |spec|
|
|
9
9
|
spec.authors = ["Blake Chambers"]
|
10
10
|
spec.email = ["chambb1@gmail.com"]
|
11
11
|
|
12
|
-
# if spec.respond_to?(:metadata)
|
13
|
-
# spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com' to prevent pushes to rubygems.org, or delete to allow pushes to any server."
|
14
|
-
# end
|
15
|
-
|
16
12
|
spec.summary = %q{inheritable accessors}
|
17
13
|
spec.description = %q{inheritable accessors}
|
18
14
|
spec.homepage = "http://github.com/blakechambers/inheritable_accessors"
|
@@ -23,9 +19,5 @@ Gem::Specification.new do |spec|
|
|
23
19
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
24
20
|
spec.require_paths = ["lib"]
|
25
21
|
|
26
|
-
spec.
|
27
|
-
|
28
|
-
spec.add_development_dependency "bundler", "~> 1.8"
|
29
|
-
spec.add_development_dependency "rake", "~> 10.0"
|
30
|
-
spec.add_development_dependency "rspec", "~> 3.0"
|
22
|
+
spec.add_runtime_dependency "activesupport", ">= 3.2.0"
|
31
23
|
end
|
@@ -4,6 +4,7 @@ require "inheritable_accessors/inheritable_hash"
|
|
4
4
|
require "inheritable_accessors/inheritable_hash_accessor"
|
5
5
|
require "inheritable_accessors/inheritable_set"
|
6
6
|
require "inheritable_accessors/inheritable_set_accessor"
|
7
|
+
require "inheritable_accessors/inheritable_option_accessor"
|
7
8
|
|
8
9
|
module InheritableAccessors
|
9
10
|
extend ActiveSupport::Concern
|
@@ -1,15 +1,21 @@
|
|
1
1
|
require 'forwardable'
|
2
|
+
require 'set'
|
2
3
|
|
3
4
|
module InheritableAccessors
|
4
5
|
class InheritableHash
|
5
6
|
attr_accessor :__local_values__
|
6
7
|
attr_reader :__parent__
|
8
|
+
attr_reader :__deleted_keys__
|
7
9
|
extend Forwardable
|
8
10
|
|
9
11
|
WRITE_METHODS = %w{
|
10
12
|
[]= initialize merge! store
|
11
13
|
}
|
12
14
|
|
15
|
+
DELETE_METHODS = %w{
|
16
|
+
delete
|
17
|
+
}
|
18
|
+
|
13
19
|
READ_METHODS = %w{
|
14
20
|
== [] each each_pair each_value empty? eql? fetch flatten
|
15
21
|
has_key? has_value? hash include? index inspect invert key key? keys
|
@@ -22,11 +28,21 @@ module InheritableAccessors
|
|
22
28
|
def initialize(prototype=nil)
|
23
29
|
@__local_values__ = Hash.new
|
24
30
|
@__parent__ = prototype
|
31
|
+
@__deleted_keys__ = prototype ? prototype.__deleted_keys__.dup : Set.new
|
32
|
+
end
|
33
|
+
|
34
|
+
def delete(key)
|
35
|
+
@__deleted_keys__.merge [key]
|
36
|
+
__local_values__.delete(key)
|
25
37
|
end
|
26
38
|
|
27
39
|
def to_hash
|
28
40
|
if !!__parent__
|
29
|
-
__parent__.to_hash
|
41
|
+
hash = __parent__.to_hash
|
42
|
+
hash.delete_if do |key, _|
|
43
|
+
__deleted_keys__.include?(key)
|
44
|
+
end
|
45
|
+
hash.merge(__local_values__)
|
30
46
|
else
|
31
47
|
__local_values__.clone
|
32
48
|
end
|
@@ -38,4 +54,4 @@ module InheritableAccessors
|
|
38
54
|
|
39
55
|
alias :initialize_copy :inherit_copy
|
40
56
|
end
|
41
|
-
end
|
57
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
module InheritableAccessors
|
2
|
+
module InheritableOptionAccessor
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
module ClassMethods
|
6
|
+
def inheritable_option_accessor(*names)
|
7
|
+
options = names.pop if names.last.kind_of?(Hash)
|
8
|
+
|
9
|
+
opts_location = options[:for].to_s
|
10
|
+
unless respond_to?(opts_location) && send(opts_location).kind_of?(InheritableAccessors::InheritableHash)
|
11
|
+
raise ArgumentError, 'requires for: to be kind of InheritableHashAccessor'
|
12
|
+
end
|
13
|
+
|
14
|
+
names.each do |name|
|
15
|
+
name = name.to_s
|
16
|
+
|
17
|
+
module_eval <<-METHUD, __FILE__, __LINE__
|
18
|
+
def #{name}(new_val=nil, &block)
|
19
|
+
InheritableAccessors::InheritableOptionAccessor.__inheritable_option(self, #{opts_location}, :#{name}, new_val, &block)
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.#{name}(new_val=nil, &block)
|
23
|
+
InheritableAccessors::InheritableOptionAccessor.__inheritable_option(self, #{opts_location}, :#{name}, new_val, &block)
|
24
|
+
end
|
25
|
+
METHUD
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
class LetOption
|
33
|
+
attr_reader :block
|
34
|
+
|
35
|
+
def initialize(block)
|
36
|
+
@block = block
|
37
|
+
@memoized = {}
|
38
|
+
end
|
39
|
+
|
40
|
+
def with_context(context)
|
41
|
+
@memoized[context] ||= begin
|
42
|
+
context.instance_exec(&block)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def self.__inheritable_option(context, options_hash, name, value, &block)
|
48
|
+
if value
|
49
|
+
options_hash[name] = value
|
50
|
+
elsif block_given?
|
51
|
+
options_hash[name] = InheritableOptionAccessor::LetOption.new(block)
|
52
|
+
else
|
53
|
+
options_hash[name].instance_of?(InheritableOptionAccessor::LetOption) ? options_hash[name].with_context(context) : options_hash[name]
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: inheritable_accessors
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Blake Chambers
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-04-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -17,55 +17,13 @@ dependencies:
|
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: 3.2.0
|
20
|
-
type: :
|
20
|
+
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: 3.2.0
|
27
|
-
- !ruby/object:Gem::Dependency
|
28
|
-
name: bundler
|
29
|
-
requirement: !ruby/object:Gem::Requirement
|
30
|
-
requirements:
|
31
|
-
- - "~>"
|
32
|
-
- !ruby/object:Gem::Version
|
33
|
-
version: '1.8'
|
34
|
-
type: :development
|
35
|
-
prerelease: false
|
36
|
-
version_requirements: !ruby/object:Gem::Requirement
|
37
|
-
requirements:
|
38
|
-
- - "~>"
|
39
|
-
- !ruby/object:Gem::Version
|
40
|
-
version: '1.8'
|
41
|
-
- !ruby/object:Gem::Dependency
|
42
|
-
name: rake
|
43
|
-
requirement: !ruby/object:Gem::Requirement
|
44
|
-
requirements:
|
45
|
-
- - "~>"
|
46
|
-
- !ruby/object:Gem::Version
|
47
|
-
version: '10.0'
|
48
|
-
type: :development
|
49
|
-
prerelease: false
|
50
|
-
version_requirements: !ruby/object:Gem::Requirement
|
51
|
-
requirements:
|
52
|
-
- - "~>"
|
53
|
-
- !ruby/object:Gem::Version
|
54
|
-
version: '10.0'
|
55
|
-
- !ruby/object:Gem::Dependency
|
56
|
-
name: rspec
|
57
|
-
requirement: !ruby/object:Gem::Requirement
|
58
|
-
requirements:
|
59
|
-
- - "~>"
|
60
|
-
- !ruby/object:Gem::Version
|
61
|
-
version: '3.0'
|
62
|
-
type: :development
|
63
|
-
prerelease: false
|
64
|
-
version_requirements: !ruby/object:Gem::Requirement
|
65
|
-
requirements:
|
66
|
-
- - "~>"
|
67
|
-
- !ruby/object:Gem::Version
|
68
|
-
version: '3.0'
|
69
27
|
description: inheritable accessors
|
70
28
|
email:
|
71
29
|
- chambb1@gmail.com
|
@@ -87,6 +45,7 @@ files:
|
|
87
45
|
- lib/inheritable_accessors.rb
|
88
46
|
- lib/inheritable_accessors/inheritable_hash.rb
|
89
47
|
- lib/inheritable_accessors/inheritable_hash_accessor.rb
|
48
|
+
- lib/inheritable_accessors/inheritable_option_accessor.rb
|
90
49
|
- lib/inheritable_accessors/inheritable_set.rb
|
91
50
|
- lib/inheritable_accessors/inheritable_set_accessor.rb
|
92
51
|
- lib/inheritable_accessors/version.rb
|
@@ -110,7 +69,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
110
69
|
version: '0'
|
111
70
|
requirements: []
|
112
71
|
rubyforge_project:
|
113
|
-
rubygems_version: 2.4.
|
72
|
+
rubygems_version: 2.4.3
|
114
73
|
signing_key:
|
115
74
|
specification_version: 4
|
116
75
|
summary: inheritable accessors
|