opto 1.7.0 → 1.8.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5ac3340f2ab72942ad62dd4589d773e7713ebb24
4
- data.tar.gz: 5882bb7b1bb860e10413ca659887c1e166f39bad
3
+ metadata.gz: 9dedbddd78ab84c43366135f15133aaa367044e7
4
+ data.tar.gz: 48e32fd24ef67476c171115dca96de827f2c90ce
5
5
  SHA512:
6
- metadata.gz: 10ea21720b0b1d07c4d7deaa43424f296ac9b3dff137db003c2e40b87ba37604dbb864592268a1ffc30dfb0d8e46a1823a61954dfbf63e96f49df8a72d4fe4b6
7
- data.tar.gz: 05c279f893883d1ac3b7dfabc8d9034719b22069369a24c6e7ed99f82e5f6ea192ce661cdb4278c68c4d4d68464446dd92160018f5a54626e6b91375f75a6d9a
6
+ metadata.gz: 7ea855a635ab51af7342cccf8b1f7aee5ae26def1564bf878c15e5316c116221d7097d6e0ed4276aa7d7591839f4c2e76acdec1924c0e7b0c63bc787a996f6f7
7
+ data.tar.gz: 3de6ee644c94c10a4570d370fb4a64609faa6bb940debc785a5bb1394a163243e475c944ea9df704707974f84aff2991b9a6384eb3533d56c4122cafd7e85714
data/.travis.yml CHANGED
@@ -1,9 +1,7 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 2.0.0-p648
4
3
  - 2.1.8
5
4
  - 2.2.3
6
- - 2.3.1
7
5
  - 2.3.3
8
6
  - 2.4.0
9
7
  - jruby-9.1.5.0
@@ -18,4 +16,4 @@ deploy:
18
16
  gem: opto
19
17
  on:
20
18
  tags: true
21
- rvm: 2.3.1
19
+ rvm: 2.4.0
data/lib/opto.rb CHANGED
@@ -2,11 +2,6 @@ require "opto/version"
2
2
  require 'opto/extensions/snake_case'
3
3
  require 'opto/extensions/hash_string_or_symbol_key'
4
4
 
5
- if RUBY_VERSION < '2.1'
6
- using Opto::Extension::SnakeCase
7
- using Opto::Extension::HashStringOrSymbolKey
8
- end
9
-
10
5
  require "opto/option"
11
6
  require "opto/group"
12
7
 
@@ -15,6 +10,9 @@ require 'yaml'
15
10
  # An option parser/validator/resolver
16
11
  #
17
12
  module Opto
13
+ using Opto::Extension::SnakeCase
14
+ using Opto::Extension::HashStringOrSymbolKey
15
+
18
16
  # Initialize a new Opto::Option (when input is hash) or an Opto::Group (when input is an array of hashes)
19
17
  def self.new(opts)
20
18
  case opts
@@ -0,0 +1,15 @@
1
+ module Opto
2
+ module Extension
3
+ # Refines String to have .snakecase method that turns
4
+ # StringLikeThis into a string_like_this
5
+ module SymbolizeKeys
6
+ refine Hash do
7
+ def symbolize_keys
8
+ each_with_object(dup.clear) do |(key, value), hash|
9
+ hash[(key.to_sym rescue key)] = value
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
data/lib/opto/group.rb CHANGED
@@ -2,11 +2,6 @@ require 'opto/option'
2
2
  require 'opto/extensions/snake_case'
3
3
  require 'opto/extensions/hash_string_or_symbol_key'
4
4
 
5
- if RUBY_VERSION < '2.1'
6
- using Opto::Extension::SnakeCase
7
- using Opto::Extension::HashStringOrSymbolKey
8
- end
9
-
10
5
  module Opto
11
6
  # A group of Opto::Option instances. Members of Groups can see their relatives
12
7
  # and their values. Such as `option.value_of('another_option')`
@@ -14,7 +9,7 @@ module Opto
14
9
  # Most Array instance methods are delegated, such as .map, .each, .find etc.
15
10
  class Group
16
11
 
17
- using Opto::Extension::HashStringOrSymbolKey unless RUBY_VERSION < '2.1'
12
+ using Opto::Extension::HashStringOrSymbolKey
18
13
 
19
14
  attr_reader :options, :defaults
20
15
 
data/lib/opto/option.rb CHANGED
@@ -4,21 +4,14 @@ require_relative 'setter'
4
4
  require_relative 'extensions/snake_case'
5
5
  require_relative 'extensions/hash_string_or_symbol_key'
6
6
 
7
- if RUBY_VERSION < '2.1'
8
- using Opto::Extension::SnakeCase
9
- using Opto::Extension::HashStringOrSymbolKey
10
- end
11
-
12
7
  module Opto
13
8
  # What is an option? It's like a variable that has a value, which can be validated or
14
9
  # manipulated on creation. The value can be resolved from a number of origins, such as
15
10
  # an environment variable or random string generator.
16
11
  class Option
17
12
 
18
- unless RUBY_VERSION < '2.1'
19
- using Opto::Extension::SnakeCase
20
- using Opto::Extension::HashStringOrSymbolKey
21
- end
13
+ using Opto::Extension::SnakeCase
14
+ using Opto::Extension::HashStringOrSymbolKey
22
15
 
23
16
  attr_accessor :type
24
17
  attr_accessor :name
data/lib/opto/resolver.rb CHANGED
@@ -1,11 +1,6 @@
1
1
  require 'opto/extensions/snake_case'
2
2
  require 'opto/extensions/hash_string_or_symbol_key'
3
3
 
4
- if RUBY_VERSION < '2.1'
5
- using Opto::Extension::SnakeCase
6
- using Opto::Extension::HashStringOrSymbolKey
7
- end
8
-
9
4
  module Opto
10
5
  # Base for resolvers.
11
6
  #
@@ -15,7 +10,7 @@ module Opto
15
10
  # RandomString, which can generate random strings of defined length.
16
11
  class Resolver
17
12
 
18
- using Opto::Extension::SnakeCase unless RUBY_VERSION < '2.1'
13
+ using Opto::Extension::SnakeCase
19
14
 
20
15
  attr_accessor :hint
21
16
  attr_accessor :option
@@ -1,11 +1,6 @@
1
1
  require 'opto/extensions/snake_case'
2
2
  require 'opto/extensions/hash_string_or_symbol_key'
3
3
 
4
- if RUBY_VERSION < '2.1'
5
- using Opto::Extension::SnakeCase
6
- using Opto::Extension::HashStringOrSymbolKey
7
- end
8
-
9
4
  module Opto
10
5
  module Resolvers
11
6
  # Allows setting the value conditionally based on other variables
@@ -34,7 +29,7 @@ module Opto
34
29
  # If you don't define an else, a null will be returned when no conditions match.
35
30
  class Condition < Opto::Resolver
36
31
 
37
- using Opto::Extension::HashStringOrSymbolKey unless RUBY_VERSION < '2.1'
32
+ using Opto::Extension::HashStringOrSymbolKey
38
33
 
39
34
  class HashCond
40
35
  attr_reader :condition
@@ -1,17 +1,12 @@
1
1
  require 'opto/extensions/snake_case'
2
2
  require 'opto/extensions/hash_string_or_symbol_key'
3
3
 
4
- if RUBY_VERSION < '2.1'
5
- using Opto::Extension::SnakeCase
6
- using Opto::Extension::HashStringOrSymbolKey
7
- end
8
-
9
4
  module Opto
10
5
  module Resolvers
11
6
  # Geneerate a new random number. Requires :min and :max in hint to define range.
12
7
  class Evaluate < Opto::Resolver
13
8
 
14
- using Opto::Extension::HashStringOrSymbolKey unless RUBY_VERSION < '2.1'
9
+ using Opto::Extension::HashStringOrSymbolKey
15
10
 
16
11
  def resolve
17
12
  raise TypeError, "String required" unless hint.kind_of?(String)
@@ -1,11 +1,6 @@
1
1
  require 'opto/extensions/snake_case'
2
2
  require 'opto/extensions/hash_string_or_symbol_key'
3
3
 
4
- if RUBY_VERSION < '2.1'
5
- using Opto::Extension::SnakeCase
6
- using Opto::Extension::HashStringOrSymbolKey
7
- end
8
-
9
4
  module Opto
10
5
  module Resolvers
11
6
  # Interpolates values from other options into a template string.
@@ -15,7 +10,7 @@ module Opto
15
10
  # interpolate: mysql://admin:$mysql_admin_pass@mysql:1234/$mysql_db_name
16
11
  class Interpolate < Opto::Resolver
17
12
 
18
- using Opto::Extension::HashStringOrSymbolKey unless RUBY_VERSION < '2.1'
13
+ using Opto::Extension::HashStringOrSymbolKey
19
14
 
20
15
  def resolve
21
16
  raise TypeError, "String expected" unless hint.kind_of?(String)
@@ -1,17 +1,12 @@
1
1
  require 'opto/extensions/snake_case'
2
2
  require 'opto/extensions/hash_string_or_symbol_key'
3
3
 
4
- if RUBY_VERSION < '2.1'
5
- using Opto::Extension::SnakeCase
6
- using Opto::Extension::HashStringOrSymbolKey
7
- end
8
-
9
4
  module Opto
10
5
  module Resolvers
11
6
  # Geneerate a new random number. Requires :min and :max in hint to define range.
12
7
  class RandomNumber < Opto::Resolver
13
8
 
14
- using Opto::Extension::HashStringOrSymbolKey unless RUBY_VERSION < '2.1'
9
+ using Opto::Extension::HashStringOrSymbolKey
15
10
 
16
11
  def resolve
17
12
  raise ArgumentError, "Range not set" if hint.nil?
@@ -1,31 +1,26 @@
1
1
  require 'opto/extensions/snake_case'
2
2
  require 'opto/extensions/hash_string_or_symbol_key'
3
3
 
4
- if RUBY_VERSION < '2.1'
5
- using Opto::Extension::SnakeCase
6
- using Opto::Extension::HashStringOrSymbolKey
7
- end
8
-
9
4
  module Opto
10
5
  module Resolvers
11
6
  # Generates a random string.
12
7
  #
13
- # Requires at least :length.
14
- # Also accepts :charset which can be one of:
15
- # - numbers (0-9),
16
- # - letters (a-z + A-Z),
17
- # - downcase (a-z),
8
+ # Requires at least :length.
9
+ # Also accepts :charset which can be one of:
10
+ # - numbers (0-9),
11
+ # - letters (a-z + A-Z),
12
+ # - downcase (a-z),
18
13
  # - upcase (A-Z),
19
- # - alphanumeric (0-9 + a-z + A-Z),
20
- # - hex (0-9 + a-f),
21
- # - hex_upcase (0-9 + A-F),
14
+ # - alphanumeric (0-9 + a-z + A-Z),
15
+ # - hex (0-9 + a-f),
16
+ # - hex_upcase (0-9 + A-F),
22
17
  # - base64 (base64 charset (length has to be divisible by four when using base64)),
23
18
  #- ascii_printable (all printable ascii chars)
24
19
  # - or a set of characters, for example:
25
20
  # { length: 8, charset: '01' } Will generate something like: 01001100
26
21
  class RandomString < Opto::Resolver
27
22
 
28
- using Opto::Extension::HashStringOrSymbolKey unless RUBY_VERSION < '2.1'
23
+ using Opto::Extension::HashStringOrSymbolKey
29
24
 
30
25
  def charset(name)
31
26
  case name.to_s
@@ -56,7 +51,7 @@ module Opto
56
51
  end
57
52
 
58
53
  def resolve
59
- if hint.kind_of?(Hash)
54
+ if hint.kind_of?(Hash)
60
55
  if hint[:length].nil?
61
56
  raise ArgumentError, "Invalid settings for random string. Required: length, optional: charset. Charsets : numbers, letters, alphanumeric, hex, base64, ascii_printable and X-Y range."
62
57
  end
data/lib/opto/setter.rb CHANGED
@@ -1,11 +1,6 @@
1
1
  require 'opto/extensions/snake_case'
2
2
  require 'opto/extensions/hash_string_or_symbol_key'
3
3
 
4
- if RUBY_VERSION < '2.1'
5
- using Opto::Extension::SnakeCase
6
- using Opto::Extension::HashStringOrSymbolKey
7
- end
8
-
9
4
  module Opto
10
5
  # Base for Setters.
11
6
  #
@@ -15,7 +10,7 @@ module Opto
15
10
  # RandomString, which can generate random strings of defined length.
16
11
  class Setter
17
12
 
18
- using Opto::Extension::SnakeCase unless RUBY_VERSION < '2.1'
13
+ using Opto::Extension::SnakeCase
19
14
 
20
15
  attr_accessor :hint
21
16
  attr_accessor :option
@@ -1,11 +1,6 @@
1
1
  require 'opto/extensions/snake_case'
2
2
  require 'opto/extensions/hash_string_or_symbol_key'
3
3
 
4
- if RUBY_VERSION < '2.1'
5
- using Opto::Extension::SnakeCase
6
- using Opto::Extension::HashStringOrSymbolKey
7
- end
8
-
9
4
  module Opto
10
5
  module Setters
11
6
  # Set a value to environment.
@@ -15,7 +10,7 @@ module Opto
15
10
  # Everything will be converted to strings unless hint is a hash with :options. (also include :name in that case)
16
11
  class Env < Opto::Setter
17
12
 
18
- using Opto::Extension::HashStringOrSymbolKey unless RUBY_VERSION < '2.1'
13
+ using Opto::Extension::HashStringOrSymbolKey
19
14
 
20
15
  attr_accessor :env_name, :dont_stringify
21
16
 
data/lib/opto/type.rb CHANGED
@@ -1,25 +1,20 @@
1
1
  require 'opto/extensions/snake_case'
2
2
  require 'opto/extensions/hash_string_or_symbol_key'
3
-
4
- if RUBY_VERSION < '2.1'
5
- using Opto::Extension::SnakeCase
6
- using Opto::Extension::HashStringOrSymbolKey
7
- end
3
+ require 'opto/extensions/symbolize_keys'
8
4
 
9
5
  module Opto
10
6
  # Defines a type handler. Used as a base from which to inherit in the type handlers.
11
7
  class Type
8
+ using Opto::Extension::HashStringOrSymbolKey
9
+ using Opto::Extension::SnakeCase
10
+ using Opto::Extension::SymbolizeKeys
11
+
12
12
  GLOBAL_OPTIONS = {
13
13
  required: true
14
14
  }
15
15
 
16
16
  attr_accessor :options
17
17
 
18
- unless RUBY_VERSION < '2.1'
19
- using Opto::Extension::SnakeCase
20
- using Opto::Extension::HashStringOrSymbolKey
21
- end
22
-
23
18
  class << self
24
19
  def inherited(where)
25
20
  types[where.type] = where
@@ -55,9 +50,7 @@ module Opto
55
50
  # end
56
51
  def validator(name, &block)
57
52
  raise TypeError, "Block required" unless block_given?
58
- define_method("validate_#{name}", &block)
59
- validators << "validate_#{name}".to_sym
60
- # RUBY_VERSION >= 2.1 would allow validators << define_method("validate_#{name}", block)
53
+ validators << define_method("validate_#{name}", &block)
61
54
  end
62
55
 
63
56
  def sanitizers
@@ -75,8 +68,7 @@ module Opto
75
68
  # end
76
69
  def sanitizer(name, &block)
77
70
  raise TypeError, "Block required" unless block_given?
78
- define_method("sanitize_#{name}", &block)
79
- sanitizers << "sanitize_#{name}".to_sym
71
+ sanitizers << define_method("sanitize_#{name}", &block)
80
72
  end
81
73
  end
82
74
 
@@ -93,7 +85,7 @@ module Opto
93
85
  end
94
86
 
95
87
  def initialize(options = {})
96
- @options = Type::GLOBAL_OPTIONS.merge(self.class.const_defined?(:OPTIONS) ? self.class.const_get(:OPTIONS) : {}).merge(options)
88
+ @options = Type::GLOBAL_OPTIONS.merge(self.class.const_defined?(:OPTIONS) ? self.class.const_get(:OPTIONS) : {}).merge(options.symbolize_keys)
97
89
  end
98
90
 
99
91
  def type
@@ -128,7 +120,7 @@ module Opto
128
120
  def validate(value)
129
121
  errors.clear
130
122
  if value.nil?
131
- errors[:presence] = "Required value missing" if required?
123
+ errors[:presence] = "Required value missing" if required?
132
124
  else
133
125
  (Type.validators + self.class.validators).each do |validator|
134
126
  begin
@@ -1,8 +1,4 @@
1
1
  require_relative '../type'
2
- if RUBY_VERSION < '2.1'
3
- using Opto::Extension::SnakeCase
4
- using Opto::Extension::HashStringOrSymbolKey
5
- end
6
2
 
7
3
  module Opto
8
4
  module Types
@@ -17,7 +13,7 @@ module Opto
17
13
  # - compact: when true, removes nils and blanks
18
14
  # - count: when true, the output is the count of items in the array
19
15
  class Array < Opto::Type
20
- using Opto::Extension::HashStringOrSymbolKey unless RUBY_VERSION < '2.1'
16
+ using Opto::Extension::HashStringOrSymbolKey
21
17
 
22
18
 
23
19
  OPTIONS = {
@@ -2,11 +2,6 @@ require_relative '../type'
2
2
  require 'opto/extensions/snake_case'
3
3
  require 'opto/extensions/hash_string_or_symbol_key'
4
4
 
5
- if RUBY_VERSION < '2.1'
6
- using Opto::Extension::SnakeCase
7
- using Opto::Extension::HashStringOrSymbolKey
8
- end
9
-
10
5
  module Opto
11
6
  module Types
12
7
  # A boolean value.
@@ -19,7 +14,7 @@ module Opto
19
14
  # :true says "true" by default when outputting a string
20
15
  # :false says "false" by default when outputting a string
21
16
  class Boolean < Opto::Type
22
- using Opto::Extension::HashStringOrSymbolKey unless RUBY_VERSION < '2.1'
17
+ using Opto::Extension::HashStringOrSymbolKey
23
18
 
24
19
  OPTIONS = {
25
20
  truthy: ['true', 'yes', '1', 'on', 'enabled', 'enable'],
@@ -2,11 +2,6 @@ require_relative '../type'
2
2
  require 'opto/extensions/snake_case'
3
3
  require 'opto/extensions/hash_string_or_symbol_key'
4
4
 
5
- if RUBY_VERSION < '2.1'
6
- using Opto::Extension::SnakeCase
7
- using Opto::Extension::HashStringOrSymbolKey
8
- end
9
-
10
5
  module Opto
11
6
  module Types
12
7
  # A list of possible values
@@ -39,7 +34,7 @@ module Opto
39
34
  # description: A friendly furry creature with a tail, says 'woof'
40
35
  # )
41
36
  class Enum < Opto::Type
42
- using Opto::Extension::HashStringOrSymbolKey unless RUBY_VERSION < '2.1'
37
+ using Opto::Extension::HashStringOrSymbolKey
43
38
 
44
39
  OPTIONS = {
45
40
  options: [],
@@ -2,11 +2,6 @@ require_relative '../type'
2
2
  require 'opto/extensions/snake_case'
3
3
  require 'opto/extensions/hash_string_or_symbol_key'
4
4
 
5
- if RUBY_VERSION < '2.1'
6
- using Opto::Extension::SnakeCase
7
- using Opto::Extension::HashStringOrSymbolKey
8
- end
9
-
10
5
  module Opto
11
6
  module Types
12
7
  # A number.
@@ -16,7 +11,7 @@ module Opto
16
11
  # :max - maximum allowed value
17
12
  # :nil_is_zero : set to true if you want to turn a null value into 0
18
13
  class Integer < Opto::Type
19
- using Opto::Extension::HashStringOrSymbolKey unless RUBY_VERSION < '2.1'
14
+ using Opto::Extension::HashStringOrSymbolKey
20
15
 
21
16
  OPTIONS = {
22
17
  min: 0,
@@ -3,11 +3,6 @@ require 'base64'
3
3
  require 'opto/extensions/snake_case'
4
4
  require 'opto/extensions/hash_string_or_symbol_key'
5
5
 
6
- if RUBY_VERSION < '2.1'
7
- using Opto::Extension::SnakeCase
8
- using Opto::Extension::HashStringOrSymbolKey
9
- end
10
-
11
6
  module Opto
12
7
  module Types
13
8
  # A string
@@ -25,7 +20,7 @@ module Opto
25
20
  # - capitalize: set to true to upcase the first letter
26
21
  # - hexdigest: valid options: md5, sha1, sha256, sha384, sha512 and nil/false. generate an md5/sha1/etc hexdigest from the value.
27
22
  class String < Opto::Type
28
- using Opto::Extension::HashStringOrSymbolKey unless RUBY_VERSION < '2.1'
23
+ using Opto::Extension::HashStringOrSymbolKey
29
24
 
30
25
  TRANSFORMATIONS = [ :upcase, :downcase, :strip, :chomp, :capitalize ]
31
26
 
@@ -3,11 +3,6 @@ require 'uri'
3
3
  require 'opto/extensions/snake_case'
4
4
  require 'opto/extensions/hash_string_or_symbol_key'
5
5
 
6
- if RUBY_VERSION < '2.1'
7
- using Opto::Extension::SnakeCase
8
- using Opto::Extension::HashStringOrSymbolKey
9
- end
10
-
11
6
  module Opto
12
7
  module Types
13
8
  # An uri/url.
@@ -15,7 +10,7 @@ module Opto
15
10
  # Options:
16
11
  # schemes: an array of allowed schemes, such as ['http', 'https', 'file']
17
12
  class Uri < Opto::Type
18
- using Opto::Extension::HashStringOrSymbolKey unless RUBY_VERSION < '2.1'
13
+ using Opto::Extension::HashStringOrSymbolKey
19
14
 
20
15
  OPTIONS = {
21
16
  schemes: [ 'http', 'https' ]
data/lib/opto/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Opto
2
- VERSION = "1.7.0"
2
+ VERSION = "1.8.0"
3
3
  end
data/opto.gemspec CHANGED
@@ -18,6 +18,8 @@ Gem::Specification.new do |spec|
18
18
  spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
19
19
  spec.require_paths = ["lib"]
20
20
 
21
+ spec.required_ruby_version = ">= 2.1.0"
22
+
21
23
  spec.add_development_dependency "bundler", "~> 1.12"
22
24
  spec.add_development_dependency "rake", "~> 10.0"
23
25
  spec.add_development_dependency "rspec", "~> 3.0"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: opto
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.7.0
4
+ version: 1.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kimmo Lehto
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-12-30 00:00:00.000000000 Z
11
+ date: 2017-01-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -69,6 +69,7 @@ files:
69
69
  - lib/opto.rb
70
70
  - lib/opto/extensions/hash_string_or_symbol_key.rb
71
71
  - lib/opto/extensions/snake_case.rb
72
+ - lib/opto/extensions/symbolize_keys.rb
72
73
  - lib/opto/group.rb
73
74
  - lib/opto/option.rb
74
75
  - lib/opto/resolver.rb
@@ -104,7 +105,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
104
105
  requirements:
105
106
  - - ">="
106
107
  - !ruby/object:Gem::Version
107
- version: '0'
108
+ version: 2.1.0
108
109
  required_rubygems_version: !ruby/object:Gem::Requirement
109
110
  requirements:
110
111
  - - ">="