greenenvy 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/greenenvy.rb +4 -4
- data/lib/greenenvy/{open_struct_like.rb → container.rb} +15 -11
- data/lib/greenenvy/context.rb +2 -1
- data/lib/greenenvy/exceptions.rb +1 -0
- data/lib/greenenvy/loader.rb +1 -0
- data/lib/greenenvy/name_validator.rb +24 -3
- data/lib/greenenvy/version.rb +2 -1
- metadata +3 -4
- data/lib/greenenvy/name_checker.rb +0 -43
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fb560ddc1e83c96b74f608f32afd390ea13276e1a4b0ef92c11dfedcd75ba3eb
|
4
|
+
data.tar.gz: 1061c2c034186f8dfd706a6ae80451097becaa3a4ba119fb021058b0ae155c6f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8811bfada6f513a77d81ff91663b8ae03fcd2685c84fdba78094d6c190b422cbf10cda652e871eb49ced38d45966db9d81810ab6ee2914606b0e3e2b174896ea
|
7
|
+
data.tar.gz: fedb8fb3fe9acafc17680fc05bf9f63bc4a11a4911596d6e8e929238b30fa542dbb924c56f8d9036a1bc609993bd63ef3a93be21de0da45ac9f448ba118ede78
|
data/lib/greenenvy.rb
CHANGED
@@ -1,15 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
require "greenenvy/context"
|
2
3
|
require "greenenvy/exceptions"
|
3
4
|
require "greenenvy/loader"
|
4
|
-
require "greenenvy/name_checker"
|
5
5
|
require "greenenvy/name_validator"
|
6
|
-
require "greenenvy/
|
6
|
+
require "greenenvy/container"
|
7
7
|
require "greenenvy/version"
|
8
8
|
|
9
9
|
module Greenenvy
|
10
10
|
def self.load_env(env, dir)
|
11
|
-
name_validator = NameValidator.new(
|
11
|
+
name_validator = NameValidator.new(Container)
|
12
12
|
loader = Loader.new(Context, name_validator, env, dir).load
|
13
|
-
|
13
|
+
Container.new(loader)
|
14
14
|
end
|
15
15
|
end
|
@@ -1,5 +1,6 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
module Greenenvy
|
2
|
-
class
|
3
|
+
class Container
|
3
4
|
def initialize(hash)
|
4
5
|
@hash = hash
|
5
6
|
@method_missing_handler = MethodMissingHandler.new(hash)
|
@@ -9,8 +10,8 @@ module Greenenvy
|
|
9
10
|
@method_missing_handler.call(name)
|
10
11
|
end
|
11
12
|
|
12
|
-
def
|
13
|
-
@
|
13
|
+
def respond_to?(name, _=false)
|
14
|
+
@method_missing_handler.respond?(name) || super
|
14
15
|
end
|
15
16
|
|
16
17
|
class MethodMissingHandler
|
@@ -19,24 +20,27 @@ module Greenenvy
|
|
19
20
|
end
|
20
21
|
|
21
22
|
def call(name)
|
22
|
-
|
23
|
+
processed_name, predicate = process_predicate(name)
|
23
24
|
|
24
|
-
|
25
|
-
|
26
|
-
if @hash.key?(name)
|
27
|
-
result_with_predicate(name, predicate)
|
25
|
+
if @hash.key?(processed_name)
|
26
|
+
result_with_predicate(processed_name, predicate)
|
28
27
|
else
|
29
|
-
raise Exceptions::MissingKey, missing_key_message(
|
28
|
+
raise Exceptions::MissingKey, missing_key_message(processed_name)
|
30
29
|
end
|
31
30
|
end
|
32
31
|
|
32
|
+
def respond?(name)
|
33
|
+
processed_name, _ = process_predicate(name)
|
34
|
+
@hash.key?(processed_name)
|
35
|
+
end
|
36
|
+
|
33
37
|
private
|
34
38
|
|
35
39
|
def process_predicate(name)
|
36
40
|
if name[-1] == "?"
|
37
|
-
[name[0..-2], true]
|
41
|
+
[name[0..-2].to_sym, true]
|
38
42
|
else
|
39
|
-
[name, false]
|
43
|
+
[name.to_sym, false]
|
40
44
|
end
|
41
45
|
end
|
42
46
|
|
data/lib/greenenvy/context.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
module Greenenvy
|
2
3
|
class Context
|
3
|
-
def initialize(name_validator, env, code, container_class=
|
4
|
+
def initialize(name_validator, env, code, container_class=Container)
|
4
5
|
@name_validator = name_validator
|
5
6
|
@env = env
|
6
7
|
@code = code
|
data/lib/greenenvy/exceptions.rb
CHANGED
data/lib/greenenvy/loader.rb
CHANGED
@@ -1,13 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
module Greenenvy
|
2
3
|
class NameValidator
|
3
|
-
|
4
|
-
|
4
|
+
REGEXP = %r{\A[A-Za-z]+\w+\z}.freeze
|
5
|
+
|
6
|
+
def initialize(klass)
|
7
|
+
@klass = klass
|
5
8
|
end
|
6
9
|
|
7
10
|
def validate!(name)
|
8
|
-
|
11
|
+
name = name.to_sym
|
12
|
+
if reserved_method?(name) || !valid_pattern?(name)
|
9
13
|
raise Exceptions::InvalidKey, "Invalid key, #{name}."
|
10
14
|
end
|
15
|
+
name
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def reserved_method?(name)
|
21
|
+
@klass.instance_methods.include?(name)
|
22
|
+
end
|
23
|
+
|
24
|
+
if String.instance_methods.include?(:match?)
|
25
|
+
def valid_pattern?(name)
|
26
|
+
REGEXP.match?(name)
|
27
|
+
end
|
28
|
+
else
|
29
|
+
def valid_pattern?(name)
|
30
|
+
name =~ REGEXP ? true : false
|
31
|
+
end
|
11
32
|
end
|
12
33
|
end
|
13
34
|
end
|
data/lib/greenenvy/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: greenenvy
|
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
|
- Aaron Gibralter
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-03-
|
11
|
+
date: 2018-03-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -72,12 +72,11 @@ files:
|
|
72
72
|
- bin/setup
|
73
73
|
- greenenvy.gemspec
|
74
74
|
- lib/greenenvy.rb
|
75
|
+
- lib/greenenvy/container.rb
|
75
76
|
- lib/greenenvy/context.rb
|
76
77
|
- lib/greenenvy/exceptions.rb
|
77
78
|
- lib/greenenvy/loader.rb
|
78
|
-
- lib/greenenvy/name_checker.rb
|
79
79
|
- lib/greenenvy/name_validator.rb
|
80
|
-
- lib/greenenvy/open_struct_like.rb
|
81
80
|
- lib/greenenvy/version.rb
|
82
81
|
homepage: https://github.com/agibralter/greenenvy
|
83
82
|
licenses:
|
@@ -1,43 +0,0 @@
|
|
1
|
-
module Greenenvy
|
2
|
-
class NameChecker
|
3
|
-
BASIC_OBJECT_METHODS = [
|
4
|
-
:!,
|
5
|
-
:!=,
|
6
|
-
:==,
|
7
|
-
:__id__,
|
8
|
-
:__send__,
|
9
|
-
:equal?,
|
10
|
-
:instance_eval,
|
11
|
-
:instance_exec,
|
12
|
-
:method_missing,
|
13
|
-
:singleton_method_added,
|
14
|
-
:singleton_method_removed,
|
15
|
-
:singleton_method_undefined,
|
16
|
-
:initialize,
|
17
|
-
:inspect,
|
18
|
-
].freeze
|
19
|
-
|
20
|
-
REGEXP = %r{\A[A-Za-z]+\w+\z}.freeze
|
21
|
-
|
22
|
-
def valid?(name)
|
23
|
-
name = name.to_s
|
24
|
-
!reserved_method?(name) && valid_pattern?(name)
|
25
|
-
end
|
26
|
-
|
27
|
-
private
|
28
|
-
|
29
|
-
def reserved_method?(name)
|
30
|
-
BASIC_OBJECT_METHODS.include?(name)
|
31
|
-
end
|
32
|
-
|
33
|
-
if String.instance_methods.include?(:match?)
|
34
|
-
def valid_pattern?(name)
|
35
|
-
REGEXP.match?(name)
|
36
|
-
end
|
37
|
-
else
|
38
|
-
def valid_pattern?(name)
|
39
|
-
name =~ REGEXP ? true : false
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|