dinject 0.1.0 → 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 +4 -4
- data/README.md +67 -11
- data/inject.gemspec +1 -1
- data/lib/dinject.rb +1 -0
- data/lib/inject/version.rb +1 -1
- data/lib/injector.rb +0 -1
- metadata +5 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e5bc520adef1400c6c9b692afaa744444f668eff
|
4
|
+
data.tar.gz: 1246aac81fe48c9a221bcf1ba80be349ab46845d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f4373378eb1d067367af16f05289d559110ea390c316608e1d169dd7e2663928ebc8be95ba1533592dfeba9f06fafadd4fe098a2246ce8a36bd36498754bda24
|
7
|
+
data.tar.gz: 6704057364f4e31098127bada5abae74c73493e5b3e399802ead2477afdef53715ba8e4610e6b38cc4cfc51b9cddacbb420e92ab7396b8d2cd0639cdf0b9dbd4
|
data/README.md
CHANGED
@@ -7,36 +7,39 @@ via the `injector` module.
|
|
7
7
|
|
8
8
|
Add this line to your application's Gemfile:
|
9
9
|
|
10
|
-
gem '
|
10
|
+
gem 'dinject'
|
11
11
|
|
12
12
|
And then execute:
|
13
13
|
|
14
14
|
$ bundle
|
15
15
|
|
16
|
-
Or install it yourself
|
16
|
+
Or install it yourself with:
|
17
17
|
|
18
|
-
$ gem install
|
18
|
+
$ gem install dinject
|
19
19
|
|
20
20
|
## Usage
|
21
21
|
|
22
22
|
Install the gem, then use it like this:
|
23
23
|
|
24
24
|
```ruby
|
25
|
-
require '
|
25
|
+
require 'dinject'
|
26
|
+
|
26
27
|
# Create injector
|
27
28
|
injector = Injector.new
|
29
|
+
|
28
30
|
# same as injector.insert(:epoch, :setter, Time.now, before: :all)
|
29
31
|
injector.epoch = Time.now
|
30
32
|
|
31
33
|
# Add a rule for `ip_address` that queries ipecho.net if potential other methods fail
|
32
|
-
# The `after: :all` ensures that this is
|
33
|
-
injector.insert(:ip_address, :ipecho, -> { open("http://ipecho.net/plain").read }, after: :all)
|
34
|
+
# The `after: :all` ensures that this is run last if all other rules fail to inject a non-nil value
|
35
|
+
injector.insert(:ip_address, :ipecho, -> { require 'open-uri'; open("http://ipecho.net/plain").read }, after: :all)
|
34
36
|
|
35
37
|
# Add another rule for `ip_address`.
|
36
38
|
# We name this rule "ifconfig" as it calls that UNIX command to retrieve network information
|
37
|
-
# from the system. We assume a call to `ifconfig` is faster than
|
38
|
-
# this over the :ipecho method.
|
39
|
-
|
39
|
+
# from the system. We assume a call to `ifconfig` is faster than an HTTP request, so we prioritize
|
40
|
+
# this over the :ipecho method. Note that the `before: :ipecho` here is redundant and could be
|
41
|
+
# omitted since `ipecho` already specifies to be considered last.
|
42
|
+
injector.insert(:ip_address, :ifconfig, -> { extract_ip_address(`ifconfig`) }, before: :ipecho)
|
40
43
|
|
41
44
|
# We may also use a gem that does the job for us - presumably, it uses some native syscalls
|
42
45
|
# and is the fastest method, so we give it the highest priority.
|
@@ -47,7 +50,7 @@ injector.insert(:ip_address, :some_gem, -> { require 'some_gem'; SomeGem.ip_addr
|
|
47
50
|
# a RuleNotFound error will be raised.
|
48
51
|
# Optional arguments (as in `|x, y = 42|`) have the same effect.
|
49
52
|
# For optional arguments, you must use the key arguments (`|x, y: 42|`) introduced in Ruby 2.0
|
50
|
-
# Note that rules
|
53
|
+
# Note that rules will inject those, even with `nil`.
|
51
54
|
injector.inject do |ip_address, epoch|
|
52
55
|
puts "ip: #{ip_address}, epoch: #{epoch}"
|
53
56
|
end
|
@@ -57,7 +60,60 @@ The injector works with a priority queue, so in the above example, it will try t
|
|
57
60
|
the ip_address *lazily* using `some_gem`. If `SomeGem.ip_address` returned `nil`, the next
|
58
61
|
rule `ifconfig` will be used and if that returns nil, a call to `ipecho.net` will be made via the
|
59
62
|
`ipecho` rule. If all rules return `nil`, `nil` is returned. If no rules exist,
|
60
|
-
`RuleNotFound` will be raised (or `NoMethodError` if called directly via e.g. `injector.ip_address`)
|
63
|
+
`RuleNotFound` will be raised (or `NoMethodError` if called directly via e.g. `injector.ip_address`).
|
64
|
+
|
65
|
+
Once a value is injected, it will be used for any consecutive calls (it will not be injected again).
|
66
|
+
|
67
|
+
You can also define dependencies for rules. For a real world example,
|
68
|
+
let's say you need an authentication token to make API calls to a web service,
|
69
|
+
and you want to store this token locally for future invocations of your program.
|
70
|
+
What you'd do naturally is look in the local file whether the token is already there,
|
71
|
+
and if not, check for the environment variable SOME_WEB_SERVICE_TOKEN (or e.g. the CLI parameter),
|
72
|
+
and if that's empty, invoke a login to said web service using the user credentials for which we
|
73
|
+
have established multiple rules as well. Let's see it in action:
|
74
|
+
|
75
|
+
```
|
76
|
+
# injector that already has rules/values for "token_file" and "cli_params"
|
77
|
+
|
78
|
+
injector.insert(:token, :file, -> (token_file) do
|
79
|
+
extract_token_from(token_file.read)
|
80
|
+
end, before: :all)
|
81
|
+
|
82
|
+
injector.insert(:token, :cli, -> (cli_params) do
|
83
|
+
cli_params[:web_service_token]
|
84
|
+
end, after: :file)
|
85
|
+
|
86
|
+
injector.insert(:token, :env, -> do
|
87
|
+
ENV["SOME_WEB_SERVICE_TOKEN"]
|
88
|
+
end, after: :cli)
|
89
|
+
|
90
|
+
injector.insert(:token, :login, -> (username, password) do
|
91
|
+
SomeWebService.login(username, password).token
|
92
|
+
end, after: :all)
|
93
|
+
|
94
|
+
injector.insert(:username, :cli_params -> (cli_params) do
|
95
|
+
cli_params[:username]
|
96
|
+
end, before: :all)
|
97
|
+
|
98
|
+
injector.insert(:username, :prompt, -> do
|
99
|
+
$stdout.print "(someweb.service) Username: "
|
100
|
+
$stdin.gets.chomp
|
101
|
+
end, after: :all)
|
102
|
+
|
103
|
+
injector.insert(:password, :cli_params -> (cli_params) do
|
104
|
+
cli_params[:password]
|
105
|
+
end, before: :all)
|
106
|
+
|
107
|
+
injector.insert(:password, :prompt, -> do
|
108
|
+
require 'io/console'
|
109
|
+
begin
|
110
|
+
$stdout.print "(someweb.service) Password: "
|
111
|
+
$stdin.noecho(&:gets).chomp
|
112
|
+
ensure puts
|
113
|
+
end
|
114
|
+
end, after: :all)
|
115
|
+
```
|
116
|
+
|
61
117
|
|
62
118
|
## Contributing
|
63
119
|
|
data/inject.gemspec
CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
|
|
10
10
|
spec.email = ["cydrop@gmail.com"]
|
11
11
|
spec.summary = %q{Inject objects into variables by name or symbol}
|
12
12
|
spec.description = %q{Inspired by angular's inject, with addition of rules and flexibility.}
|
13
|
-
spec.homepage = ""
|
13
|
+
spec.homepage = "https://github.com/muja/inject.rb"
|
14
14
|
spec.licenses = ["MIT"]
|
15
15
|
|
16
16
|
spec.files = `git ls-files`.split($/)
|
data/lib/dinject.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'inject'
|
data/lib/inject/version.rb
CHANGED
data/lib/injector.rb
CHANGED
@@ -56,7 +56,6 @@ class Injector
|
|
56
56
|
result = overrides.fetch(index) do
|
57
57
|
# then fetch by value
|
58
58
|
overrides.fetch(value) do
|
59
|
-
puts type
|
60
59
|
# Unfortunately, optional arguments are not possible, as
|
61
60
|
# a Ruby block passed to `inject` will have exclusively optional arguments.
|
62
61
|
# Instead, key arguments introduced in Ruby 2.0 should be used.
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dinject
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Danyel Bayraktar
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-11-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: pqueue
|
@@ -37,11 +37,12 @@ files:
|
|
37
37
|
- README.md
|
38
38
|
- Rakefile
|
39
39
|
- inject.gemspec
|
40
|
+
- lib/dinject.rb
|
40
41
|
- lib/inject.rb
|
41
42
|
- lib/inject/rule.rb
|
42
43
|
- lib/inject/version.rb
|
43
44
|
- lib/injector.rb
|
44
|
-
homepage:
|
45
|
+
homepage: https://github.com/muja/inject.rb
|
45
46
|
licenses:
|
46
47
|
- MIT
|
47
48
|
metadata: {}
|
@@ -61,7 +62,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
61
62
|
version: '0'
|
62
63
|
requirements: []
|
63
64
|
rubyforge_project:
|
64
|
-
rubygems_version: 2.
|
65
|
+
rubygems_version: 2.5.1
|
65
66
|
signing_key:
|
66
67
|
specification_version: 4
|
67
68
|
summary: Inject objects into variables by name or symbol
|