dinject 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0117d663baa4930c4d3fc4ecb07590964299de2d
4
- data.tar.gz: 675365a6dd4ba61f0de0550c05c2c5c4c5bcc187
3
+ metadata.gz: e5bc520adef1400c6c9b692afaa744444f668eff
4
+ data.tar.gz: 1246aac81fe48c9a221bcf1ba80be349ab46845d
5
5
  SHA512:
6
- metadata.gz: ce89a7c1e23f1c3e13378ff4c58e4cfe471eb41efa1980cb327ec0f50a983562d0c72f7e0f15d4d1c51b09b86f278beba98cd9863dacc32da61ff998ec542481
7
- data.tar.gz: 8deff621d8903d14bff29c73e8902146c28568d1ed704f10c6725ab5fa2c68467a2dcbae15c728934721d1a831c2eea0760b6dd8e60dd9268c6df6e2d99c4a8c
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 'inject'
10
+ gem 'dinject'
11
11
 
12
12
  And then execute:
13
13
 
14
14
  $ bundle
15
15
 
16
- Or install it yourself as:
16
+ Or install it yourself with:
17
17
 
18
- $ gem install inject
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 'inject'
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 only run if other rules fail
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 a HTTP request, so we prioritize
38
- # this over the :ipecho method.
39
- injector.insert(:ip_address, :ifconfig, -> { SomeModule.extract_ip(`ifconfig`) }, before: :ipecho)
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 that return `nil` will override those.
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
 
@@ -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($/)
@@ -0,0 +1 @@
1
+ require 'inject'
@@ -1,3 +1,3 @@
1
1
  module Inject
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -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.1.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: 2015-12-29 00:00:00.000000000 Z
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.4.5.1
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