anyway_config 1.4.0 → 1.4.1

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
  SHA256:
3
- metadata.gz: b57f348945f2dbb59224655d3967880fd8c02adba6b54748b14436266c4c0c5d
4
- data.tar.gz: 36101f8dcb28679c746846d861a220ec28d836eb62cbbdaa5cb3f1254b56446b
3
+ metadata.gz: e1fad9e1aac1823b53c56253352f21964432c8c93fc7515308ac729aaead836f
4
+ data.tar.gz: 172f7208a1df440f37e531801623e54a31b65524b321e22ea2f821418a0b6e73
5
5
  SHA512:
6
- metadata.gz: d623918a56e50ab8c2f7eba539d2a4c1ea487a3192277525b559443d49328ee2cb70e9739bdd8524298ef3bff69fde6819b26262e245dafd7712a8a2c5a50d27
7
- data.tar.gz: 60a2be222bc918750f6896bb4fcb1c2f1a2570ca32780a3f55e8d3610f8f3fdc78033971c9104968f5970c9b3943272f54fe911995dc43819f774a669010c80a
6
+ metadata.gz: a29addaa15fd5c9476476a4a3d3e9b4652116314f606c07b88dbc223cee8d626014a8d0ef18d8ca8c10ffbe355c760b462dfcb27d9299df399404d4c4187e149
7
+ data.tar.gz: c30bfbae04f2da1367f213be8fdb1312aed3c198472bb3a66695c85b59bcee1c0aca5b0c7ab099ca463c4d3c67b28c538950b5f9b9294887896bf707872303eb
data/README.md CHANGED
@@ -164,7 +164,7 @@ Example usage:
164
164
 
165
165
  ```ruby
166
166
  class MyConfig < Anyway::Config
167
- attr_config :host, :log_level, :concurrency, server_args: {}
167
+ attr_config :host, :log_level, :concurrency, :debug, server_args: {}
168
168
 
169
169
  # specify which options shouldn't be handled by option parser
170
170
  ignore_options :server_args
@@ -174,9 +174,17 @@ class MyConfig < Anyway::Config
174
174
  concurrency: "number of threads to use"
175
175
  )
176
176
 
177
+ # mark some options as flag
178
+ flag_options :debug
179
+
177
180
  # extend an option parser object (i.e. add banner or version/help handlers)
178
- extend_options do |parser|
181
+ extend_options do |parser, config|
179
182
  parser.banner = "mycli [options]"
183
+
184
+ parser.on("--server-args VALUE") do |value|
185
+ config.server_args = JSON.parse(value)
186
+ end
187
+
180
188
  parser.on_tail "-h", "--help" do
181
189
  puts parser
182
190
  end
@@ -3,12 +3,14 @@
3
3
  require 'anyway/ext/deep_dup'
4
4
  require 'anyway/ext/deep_freeze'
5
5
  require 'anyway/ext/hash'
6
+ require 'anyway/ext/string_serialize'
6
7
  require 'anyway/option_parser_builder'
7
8
 
8
9
  module Anyway # :nodoc:
9
10
  using Anyway::Ext::DeepDup
10
11
  using Anyway::Ext::DeepFreeze
11
12
  using Anyway::Ext::Hash
13
+ using Anyway::Ext::StringSerialize
12
14
 
13
15
  # Base config class
14
16
  # Provides `attr_config` method to describe
@@ -38,13 +40,21 @@ module Anyway # :nodoc:
38
40
  end
39
41
 
40
42
  def ignore_options(*args)
41
- @ignore_options ||= []
42
- @ignore_options |= args
43
+ args.each do |name|
44
+ option_parser_descriptors[name.to_s][:ignore] = true
45
+ end
43
46
  end
44
47
 
45
48
  def describe_options(**hargs)
46
- @option_parser_descriptions ||= {}
47
- @option_parser_descriptions.merge!(hargs.stringify_keys!)
49
+ hargs.each do |name, desc|
50
+ option_parser_descriptors[name.to_s][:desc] = desc
51
+ end
52
+ end
53
+
54
+ def flag_options(*args)
55
+ args.each do |name|
56
+ option_parser_descriptors[name.to_s][:flag] = true
57
+ end
48
58
  end
49
59
 
50
60
  def extend_options(&block)
@@ -52,12 +62,11 @@ module Anyway # :nodoc:
52
62
  end
53
63
 
54
64
  def option_parser_options
55
- ignored_options = @ignore_options || []
56
- descriptions = @option_parser_descriptions || {}
57
65
  config_attributes.each_with_object({}) do |key, result|
58
- next if ignored_options.include?(key.to_sym)
66
+ descriptor = option_parser_descriptors[key.to_s]
67
+ next if descriptor[:ignore] == true
59
68
 
60
- result[key] ||= descriptions[key.to_s]
69
+ result[key] = descriptor
61
70
  end
62
71
  end
63
72
 
@@ -79,6 +88,10 @@ module Anyway # :nodoc:
79
88
 
80
89
  private
81
90
 
91
+ def option_parser_descriptors
92
+ @option_parser_descriptors ||= Hash.new { |h, k| h[k] = {} }
93
+ end
94
+
82
95
  def underscore_name
83
96
  return unless name
84
97
 
@@ -152,9 +165,9 @@ module Anyway # :nodoc:
152
165
  def option_parser
153
166
  @option_parser ||= begin
154
167
  parser = OptionParserBuilder.call(self.class.option_parser_options) do |key, arg|
155
- set_value(key, arg)
168
+ set_value(key, arg.is_a?(String) ? arg.serialize : arg)
156
169
  end
157
- self.class.option_parser_extension&.call(parser) || parser
170
+ self.class.option_parser_extension&.call(parser, self) || parser
158
171
  end
159
172
  end
160
173
 
@@ -1,14 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'anyway/ext/deep_dup'
4
- require 'anyway/ext/string'
4
+ require 'anyway/ext/string_serialize'
5
5
 
6
6
  module Anyway
7
7
  # Parses environment variables and provides
8
8
  # method-like access
9
9
  class Env
10
10
  using Anyway::Ext::DeepDup
11
- using Anyway::Ext::String
11
+ using Anyway::Ext::StringSerialize
12
12
 
13
13
  def initialize
14
14
  @data = {}
@@ -3,13 +3,13 @@
3
3
  module Anyway
4
4
  module Ext
5
5
  # Extend String through refinements
6
- module String
7
- refine ::String do
8
- # Regexp to detect array values
9
- # Array value is a values that contains at least one comma
10
- # and doesn't start/end with quote
11
- ARRAY_RXP = /\A[^'"].*\s*,\s*.*[^'"]\z/
6
+ module StringSerialize
7
+ # Regexp to detect array values
8
+ # Array value is a values that contains at least one comma
9
+ # and doesn't start/end with quote
10
+ ARRAY_RXP = /\A[^'"].*\s*,\s*.*[^'"]\z/
12
11
 
12
+ refine ::String do
13
13
  # rubocop:disable Metrics/MethodLength
14
14
  # rubocop:disable Metrics/CyclomaticComplexity
15
15
  def serialize
@@ -1,19 +1,16 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'optparse'
4
- require 'anyway/ext/string'
5
4
 
6
5
  module Anyway # :nodoc:
7
- using Anyway::Ext::String
8
-
9
6
  # Initializes the OptionParser instance using the given configuration
10
7
  class OptionParserBuilder
11
8
  class << self
12
9
  def call(options)
13
10
  OptionParser.new do |opts|
14
- options.each do |key, description|
15
- opts.on(*option_parser_on_args(key, description)) do |arg|
16
- yield [key, arg.serialize]
11
+ options.each do |key, descriptor|
12
+ opts.on(*option_parser_on_args(key, **descriptor)) do |arg|
13
+ yield [key, arg]
17
14
  end
18
15
  end
19
16
  end
@@ -21,9 +18,9 @@ module Anyway # :nodoc:
21
18
 
22
19
  private
23
20
 
24
- def option_parser_on_args(key, description)
25
- on_args = ["--#{key.to_s.tr('_', '-')} VALUE"]
26
- on_args << description unless description.nil?
21
+ def option_parser_on_args(key, flag: false, desc: nil)
22
+ on_args = ["--#{key.to_s.tr('_', '-')}#{flag ? '' : ' VALUE'}"]
23
+ on_args << desc unless desc.nil?
27
24
  on_args
28
25
  end
29
26
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Anyway # :nodoc:
4
- VERSION = "1.4.0"
4
+ VERSION = "1.4.1"
5
5
  end
@@ -241,11 +241,12 @@ describe Anyway::Config do
241
241
  Class.new(described_class) do
242
242
  config_name 'optparse'
243
243
  attr_config :host, :port, :log_level, :debug
244
+ flag_options :debug
244
245
  end
245
246
  end
246
247
 
247
248
  it "parses ARGC string" do
248
- config_instance.parse_options!(%w[--host localhost --port 3333 --log-level debug --debug T])
249
+ config_instance.parse_options!(%w[--host localhost --port 3333 --log-level debug --debug])
249
250
  expect(config_instance.host).to eq("localhost")
250
251
  expect(config_instance.port).to eq(3333)
251
252
  expect(config_instance.log_level).to eq("debug")
@@ -300,9 +301,13 @@ describe Anyway::Config do
300
301
  config_name 'optparse'
301
302
  attr_config :host, :log_level, :concurrency, server_args: {}
302
303
 
303
- extend_options do |parser|
304
+ extend_options do |parser, config|
304
305
  parser.banner = "mycli [options]"
305
306
 
307
+ parser.on("--server-args VALUE") do |value|
308
+ config.server_args = JSON.parse(value)
309
+ end
310
+
306
311
  parser.on_tail "-h", "--help" do
307
312
  puts parser
308
313
  end
@@ -313,6 +318,13 @@ describe Anyway::Config do
313
318
  it "allows to customize the parser" do
314
319
  expect(config_instance.option_parser.help).to include("mycli [options]")
315
320
  end
321
+
322
+ it "passes config to extension" do
323
+ config_instance.parse_options!(
324
+ ['--server-args', '{"host":"0.0.0.0"}']
325
+ )
326
+ expect(config_instance.server_args["host"]).to eq "0.0.0.0"
327
+ end
316
328
  end
317
329
  end
318
330
  end
@@ -2,8 +2,8 @@
2
2
 
3
3
  require 'spec_helper'
4
4
 
5
- describe Anyway::Ext::String do
6
- using Anyway::Ext::String
5
+ describe Anyway::Ext::StringSerialize do
6
+ using Anyway::Ext::StringSerialize
7
7
 
8
8
  it "serializes a string", :aggregate_failures do
9
9
  expect("1,2, 3".serialize).to eq [1, 2, 3]
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: anyway_config
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.0
4
+ version: 1.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vladimir Dementyev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-10-29 00:00:00.000000000 Z
11
+ date: 2018-10-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -82,7 +82,7 @@ files:
82
82
  - lib/anyway/ext/deep_dup.rb
83
83
  - lib/anyway/ext/deep_freeze.rb
84
84
  - lib/anyway/ext/hash.rb
85
- - lib/anyway/ext/string.rb
85
+ - lib/anyway/ext/string_serialize.rb
86
86
  - lib/anyway/option_parser_builder.rb
87
87
  - lib/anyway/rails/config.rb
88
88
  - lib/anyway/version.rb
@@ -103,7 +103,7 @@ files:
103
103
  - spec/ext/deep_dup_spec.rb
104
104
  - spec/ext/deep_freeze_spec.rb
105
105
  - spec/ext/hash_spec.rb
106
- - spec/ext/string_spec.rb
106
+ - spec/ext/string_serialize_spec.rb
107
107
  - spec/spec_helper.rb
108
108
  - spec/spec_norails_helper.rb
109
109
  - spec/support/cool_config.rb