meander 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 +7 -0
- data/.gitignore +14 -0
- data/.rspec +2 -0
- data/.rubocop.yml +14 -0
- data/.travis.yml +5 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +41 -0
- data/Rakefile +6 -0
- data/bin/console +7 -0
- data/bin/setup +6 -0
- data/lib/meander/common_methods.rb +42 -0
- data/lib/meander/mutable.rb +126 -0
- data/lib/meander/plain.rb +71 -0
- data/lib/meander/version.rb +3 -0
- data/lib/meander.rb +7 -0
- data/meander.gemspec +41 -0
- metadata +145 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ff8d696385670ddbb344d817d92194bc31cdb880
|
4
|
+
data.tar.gz: d9caa19b08da3a35f8bf4c60a5363b3c2e23432e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 84a6f5794aae64b1ddc88cfc2112613c4e362b067e3e2b9112ebdedbfdb0da277cce9a4e983055ca10fcb59cad9cf2cb932f9734beac4710395c9bd61202e284
|
7
|
+
data.tar.gz: 6e5a019466c37f96864471e93b051d3376ebffe2e75a3823a1cd1765a09cd8706d852aa842369c80d3c7b4aed0e7661d90b12e6c70400cc0ea3483546633bd24
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Kostrov Alexander
|
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,41 @@
|
|
1
|
+
# Meander
|
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/meander`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'meander'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install meander
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
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.
|
30
|
+
|
31
|
+
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).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/meander.
|
36
|
+
|
37
|
+
|
38
|
+
## License
|
39
|
+
|
40
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
41
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
data/bin/setup
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
module Meander
|
2
|
+
module CommonMethods # :nodoc:
|
3
|
+
module ClassMethods # :nodoc:
|
4
|
+
def hash_or_cover_class?(value)
|
5
|
+
value.is_a?(Hash) || value.is_a?(cover_class)
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.included(base)
|
10
|
+
base.extend ClassMethods
|
11
|
+
base.instance_eval do
|
12
|
+
def cover_class=(val)
|
13
|
+
@cover_class = val
|
14
|
+
end
|
15
|
+
|
16
|
+
def cover_class
|
17
|
+
@cover_class ||= self
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def define_getter(method)
|
25
|
+
define_singleton_method method do |&b|
|
26
|
+
if key?(method)
|
27
|
+
b.call(self[method]) if block_given?
|
28
|
+
self[method]
|
29
|
+
else
|
30
|
+
instance_eval(method) do |name|
|
31
|
+
undef_method name
|
32
|
+
end
|
33
|
+
send method, &block
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def new_key_method?(method)
|
39
|
+
method =~ /^([[:word:]]+)\=$/
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,126 @@
|
|
1
|
+
require 'meander/plain'
|
2
|
+
require 'delegate'
|
3
|
+
require 'forwardable'
|
4
|
+
|
5
|
+
module Meander
|
6
|
+
##
|
7
|
+
# This class is a mutable version of Meander::Plain
|
8
|
+
#
|
9
|
+
# All keys you alter will be keep up to date if dependend objects updated
|
10
|
+
# == Usage
|
11
|
+
# === Nesting value updates
|
12
|
+
# a = {}
|
13
|
+
# m = Meander::Mutable.new(a)
|
14
|
+
# a['key'] = 1
|
15
|
+
# m.key # => 1
|
16
|
+
# === Value overloading
|
17
|
+
# a = {}
|
18
|
+
# m = Meander::Mutable.new(a)
|
19
|
+
# m.key = 1
|
20
|
+
# m.key # => 1
|
21
|
+
# a # => {}
|
22
|
+
# === Deep value overloading
|
23
|
+
# a = {a: {b: {c: 1}}}
|
24
|
+
# m = Meander::Mutable.new(a)
|
25
|
+
# m.a.b.merge!({d: 2})
|
26
|
+
# m.a.b.c # => 1
|
27
|
+
# m.a.b.d # => 2
|
28
|
+
# a # => {a: {b: {c: 1}}}
|
29
|
+
# a[:a][:b][:c] = 3
|
30
|
+
# m.a.b.c # => 3
|
31
|
+
# ==== Notice
|
32
|
+
# Meander::Mutable support only one up to date object reference
|
33
|
+
# a = {a: 1}
|
34
|
+
# m = Meander::Mutable.new(a)
|
35
|
+
# b = {b: 2}
|
36
|
+
# m.merge!(b)
|
37
|
+
# a[:a] = 3
|
38
|
+
# b[:b] = 4
|
39
|
+
# m.a # => 3 # Value is up to date
|
40
|
+
# m.b # => 2 # Attention! Value remains unchanged
|
41
|
+
class Mutable < Delegator
|
42
|
+
class MutableProxy < Plain # :nodoc:
|
43
|
+
self.cover_class = Mutable
|
44
|
+
end
|
45
|
+
|
46
|
+
include CommonMethods
|
47
|
+
|
48
|
+
def initialize(obj = {})
|
49
|
+
super
|
50
|
+
@delegate_sd_obj = obj
|
51
|
+
@own_keys = MutableProxy.new
|
52
|
+
end
|
53
|
+
|
54
|
+
def __getobj__
|
55
|
+
@delegate_sd_obj
|
56
|
+
end
|
57
|
+
|
58
|
+
def __setobj__(obj)
|
59
|
+
@delegate_sd_obj = obj
|
60
|
+
end
|
61
|
+
|
62
|
+
def key?(key)
|
63
|
+
@own_keys.key?(key) ||
|
64
|
+
begin
|
65
|
+
obj = __getobj__
|
66
|
+
obj.key?(key.to_s) || obj.key?(key.to_sym)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
def keys
|
71
|
+
origin = __getobj__
|
72
|
+
own = @own_keys
|
73
|
+
[(origin && origin.keys), (own && own.keys)].flatten
|
74
|
+
.compact.map(&:to_s).uniq
|
75
|
+
end
|
76
|
+
|
77
|
+
def [](key)
|
78
|
+
val = nil
|
79
|
+
if @own_keys.key? key
|
80
|
+
val = @own_keys[key]
|
81
|
+
else
|
82
|
+
val = get_delegated_value key
|
83
|
+
if val.is_a?(Hash) || val.is_a?(Mutable)
|
84
|
+
val = Mutable.new(val)
|
85
|
+
self[key] = val
|
86
|
+
end
|
87
|
+
end
|
88
|
+
val
|
89
|
+
end
|
90
|
+
|
91
|
+
def respond_to_missing?(method, include_private = false)
|
92
|
+
@own_keys.respond_to?(method) || delegated_key?(method.to_s) || super
|
93
|
+
end
|
94
|
+
|
95
|
+
def method_missing(method, *args, &block)
|
96
|
+
if @own_keys.respond_to?(method) || block_given?
|
97
|
+
@own_keys.send method, *args, &block
|
98
|
+
elsif delegated_key? method
|
99
|
+
define_getter method
|
100
|
+
send method, *args, &block
|
101
|
+
else
|
102
|
+
super
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
extend Forwardable
|
107
|
+
def_delegator :@own_keys, :[]=
|
108
|
+
|
109
|
+
private
|
110
|
+
|
111
|
+
def delegated_key?(key)
|
112
|
+
__getobj__.keys.map(&:to_s).include? key.to_s
|
113
|
+
end
|
114
|
+
|
115
|
+
def get_delegated_value(key)
|
116
|
+
delegated = __getobj__
|
117
|
+
if key.respond_to?(:to_s) && delegated.key?(key.to_s)
|
118
|
+
delegated[key.to_s]
|
119
|
+
elsif key.respond_to?(:to_sym) && delegated.key?(key.to_sym)
|
120
|
+
delegated[key.to_sym]
|
121
|
+
else
|
122
|
+
delegated[key]
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
require 'thor/core_ext/hash_with_indifferent_access'
|
2
|
+
|
3
|
+
module Meander
|
4
|
+
##
|
5
|
+
# == Brief
|
6
|
+
# This class is a sugar filled version of HashWithIndifferentAccess
|
7
|
+
#
|
8
|
+
# It supports key-based method calling and value block evaluation
|
9
|
+
# == Configuration
|
10
|
+
# You can set class that will be applied to newly assigned values
|
11
|
+
# require 'active_support/core_ext/hash'
|
12
|
+
# class MyClass < Meander::Plain
|
13
|
+
# cover_class = HashWithIndifferentAccess
|
14
|
+
# end
|
15
|
+
#
|
16
|
+
# m = MyClass.new
|
17
|
+
# m[:a] = {}
|
18
|
+
# m[:a].class # => ActiveSupport::HashWithIndifferentAccess
|
19
|
+
# == Usage
|
20
|
+
# === Key based method evaluation
|
21
|
+
# m = Meander::Plain.new({:a => 1})
|
22
|
+
# m.a # => 1
|
23
|
+
# === New value assignment
|
24
|
+
# n = Meander::Plain.new
|
25
|
+
# n.a = 1
|
26
|
+
# n.a # => 1
|
27
|
+
# === Block values evaluation
|
28
|
+
# k = Meander::Plain.new({config: nil})
|
29
|
+
# k.config do |k|
|
30
|
+
# k.path = "some_config_path.yml"
|
31
|
+
# end
|
32
|
+
# k.config.path # => "some_config_path.yml"
|
33
|
+
class Plain < ::Thor::CoreExt::HashWithIndifferentAccess
|
34
|
+
include CommonMethods
|
35
|
+
|
36
|
+
def initialize(val = {})
|
37
|
+
val ||= {}
|
38
|
+
super(val)
|
39
|
+
end
|
40
|
+
|
41
|
+
def []=(key, value)
|
42
|
+
if value.is_a?(Hash) && !value.is_a?(self.class.cover_class)
|
43
|
+
super(key, self.class.cover_class.new(value))
|
44
|
+
else
|
45
|
+
super
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def method_missing(method, *args, &block)
|
50
|
+
method = method.to_s
|
51
|
+
if new_key_method? method
|
52
|
+
key_name = method.gsub(/\=$/, '')
|
53
|
+
send :[]=, key_name, *args, &block
|
54
|
+
elsif block_given?
|
55
|
+
val = self[method]
|
56
|
+
val = {} unless self.class.hash_or_cover_class?(val)
|
57
|
+
send :[]=, method, val
|
58
|
+
yield(self[method])
|
59
|
+
elsif key?(method)
|
60
|
+
define_getter(method)
|
61
|
+
send method, &block
|
62
|
+
else
|
63
|
+
super
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def respond_to_missing?(method, include_private = false)
|
68
|
+
new_key_method?(method) || key?(method) || super
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
data/lib/meander.rb
ADDED
data/meander.gemspec
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
lib = File.expand_path('../lib', __FILE__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require 'meander/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = 'meander'
|
9
|
+
spec.version = Meander::VERSION
|
10
|
+
spec.authors = ['Kostrov Alexander']
|
11
|
+
spec.email = ['bombazook@gmail.com']
|
12
|
+
|
13
|
+
spec.summary = 'Just another ruby Hash extension'
|
14
|
+
spec.description = 'Hashie clone with a bit more sugar'
|
15
|
+
spec.homepage = 'http://github.com/bombazook/meander'
|
16
|
+
spec.license = 'MIT'
|
17
|
+
|
18
|
+
spec.metadata['allowed_push_host'] = 'https://rubygems.org'
|
19
|
+
|
20
|
+
if spec.respond_to?(:metadata)
|
21
|
+
spec.metadata['allowed_push_host'] = 'https://rubygems.org'
|
22
|
+
else
|
23
|
+
raise 'RubyGems 2.0 or newer is required to protect against ' \
|
24
|
+
'public gem pushes.'
|
25
|
+
end
|
26
|
+
|
27
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
28
|
+
f.match(%r{^(test|spec|features)/})
|
29
|
+
end
|
30
|
+
spec.bindir = 'exe'
|
31
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
32
|
+
spec.require_paths = ['lib']
|
33
|
+
|
34
|
+
spec.add_runtime_dependency 'thor', '~> 0.19.4'
|
35
|
+
|
36
|
+
spec.add_development_dependency 'bundler', '~> 1.14'
|
37
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
38
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
39
|
+
spec.add_development_dependency 'rubocop'
|
40
|
+
spec.add_development_dependency 'rubocop-rspec'
|
41
|
+
end
|
metadata
ADDED
@@ -0,0 +1,145 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: meander
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Kostrov Alexander
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-05-03 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: thor
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.19.4
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.19.4
|
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.14'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.14'
|
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
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rubocop
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rubocop-rspec
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
description: Hashie clone with a bit more sugar
|
98
|
+
email:
|
99
|
+
- bombazook@gmail.com
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- ".gitignore"
|
105
|
+
- ".rspec"
|
106
|
+
- ".rubocop.yml"
|
107
|
+
- ".travis.yml"
|
108
|
+
- Gemfile
|
109
|
+
- LICENSE.txt
|
110
|
+
- README.md
|
111
|
+
- Rakefile
|
112
|
+
- bin/console
|
113
|
+
- bin/setup
|
114
|
+
- lib/meander.rb
|
115
|
+
- lib/meander/common_methods.rb
|
116
|
+
- lib/meander/mutable.rb
|
117
|
+
- lib/meander/plain.rb
|
118
|
+
- lib/meander/version.rb
|
119
|
+
- meander.gemspec
|
120
|
+
homepage: http://github.com/bombazook/meander
|
121
|
+
licenses:
|
122
|
+
- MIT
|
123
|
+
metadata:
|
124
|
+
allowed_push_host: https://rubygems.org
|
125
|
+
post_install_message:
|
126
|
+
rdoc_options: []
|
127
|
+
require_paths:
|
128
|
+
- lib
|
129
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
130
|
+
requirements:
|
131
|
+
- - ">="
|
132
|
+
- !ruby/object:Gem::Version
|
133
|
+
version: '0'
|
134
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
requirements: []
|
140
|
+
rubyforge_project:
|
141
|
+
rubygems_version: 2.6.11
|
142
|
+
signing_key:
|
143
|
+
specification_version: 4
|
144
|
+
summary: Just another ruby Hash extension
|
145
|
+
test_files: []
|