dorian-arguments 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/VERSION +1 -1
  3. data/lib/dorian/arguments.rb +28 -20
  4. metadata +1 -29
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e02f141d6bfdd389868a2971a9b0f36ed727fa78ab18b9b4715498ac55818d0b
4
- data.tar.gz: 83b5ca9c05d4089c553f928bf9f21d86a2d4465bb89d1a448b5261b3dee3c768
3
+ metadata.gz: f9954bfae6d2589acc531515399fd72719c17e59850945789eda577bbdbf76a1
4
+ data.tar.gz: a515bd1890f8bb0f69f7851b17a3e2e7c81a347ff3bdd0043d9de6f5978b375d
5
5
  SHA512:
6
- metadata.gz: 92b8cf10de0ea112586d59ee491ccbed78183c3457e60361c4dd9ca76fe7c2cc5400ce5cb209d2e683767d7b7f8ee7326473967b9c169a432d561253192b7449
7
- data.tar.gz: 165bf376d5bd39c13ca104d32b098bdad23153714ca9c486f3290ddd977b02088bf84f4bfafefe2c80710d55c4bcf94fa11cb8af5841b6dc43e0edff24a3174a
6
+ metadata.gz: f162d7069cce665ddafef2c864e4cfb97e367303624bcfa9fedd9539c0f1776292bb5b52073b0cd202e2a713b27ff18c3db53950f38008a4b54f81640db57cdf
7
+ data.tar.gz: e45aaa64ba83d4c43e7b1cc08115556ded6942de9a2d85b16d1bf2b106870703347922558d5e99afdedbe65d89c1c2dcb26890d9552a645d5e136fc32bb21f1e
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.0
1
+ 1.1.0
@@ -1,10 +1,5 @@
1
- require "dorian/to_struct"
2
1
  require "bigdecimal"
3
2
  require "bigdecimal/util"
4
- require "active_support"
5
- require "active_support/core_ext/string/inflections"
6
- require "active_support/core_ext/array/wrap"
7
- require "active_support/core_ext/enumerable"
8
3
 
9
4
  class Dorian
10
5
  class Arguments
@@ -22,13 +17,7 @@ class Dorian
22
17
 
23
18
  TYPES = [BOOLEAN, STRING, NUMBER, INTEGER, DECIMAL]
24
19
 
25
- DEFAULTS = {
26
- BOOLEAN => "false",
27
- STRING => "",
28
- NUMBER => "0",
29
- INTEGER => "0",
30
- DECIMAL => "0"
31
- }
20
+ DEFAULT = nil
32
21
 
33
22
  MATCHES = {
34
23
  BOOLEAN => /^(true|false)$/i,
@@ -56,7 +45,7 @@ class Dorian
56
45
  definition.each do |key, value|
57
46
  type, default, aliases = normalize(value)
58
47
 
59
- keys = ([key] + aliases).map(&:to_s).map(&:parameterize)
48
+ keys = ([key] + aliases).map(&:to_s).map { |key| parameterize(key) }
60
49
  keys = keys.map { |key| ["--#{key}", "-#{key}"] }.flatten
61
50
 
62
51
  indexes = []
@@ -87,7 +76,7 @@ class Dorian
87
76
 
88
77
  values = [default] if values.empty?
89
78
  values = values.map { |value| cast(type, value) }
90
- values = values.first unless values.many?
79
+ values = values.first unless many?(values)
91
80
  options[key] = values
92
81
  end
93
82
 
@@ -95,11 +84,17 @@ class Dorian
95
84
 
96
85
  arguments -= files
97
86
 
98
- { arguments:, options:, files:, help: }.to_deep_struct
87
+ options = Struct.new(*options.keys).new(*options.values)
88
+
89
+ Struct.new(:arguments, :options, :files, :help).new(
90
+ arguments, options, files, help
91
+ )
99
92
  end
100
93
 
101
94
  def cast(type, value)
102
- if type == BOOLEAN
95
+ if value.nil?
96
+ nil
97
+ elsif type == BOOLEAN
103
98
  BOOLEANS.fetch(value.downcase)
104
99
  elsif type == INTEGER
105
100
  value.to_i
@@ -121,7 +116,7 @@ class Dorian
121
116
  keys_message =
122
117
  ([key] + aliases)
123
118
  .map(&:to_s)
124
- .map(&:parameterize)
119
+ .map { |key| parameterize(key) }
125
120
  .map { |key| key.size == 1 ? "-#{key}" : "--#{key}" }
126
121
  .join("|")
127
122
 
@@ -132,18 +127,31 @@ class Dorian
132
127
  message
133
128
  end
134
129
 
130
+ def many?(array)
131
+ array.size > 1
132
+ end
133
+
134
+ def parameterize(string, separator: "-")
135
+ string
136
+ .to_s
137
+ .encode("ASCII", invalid: :replace, undef: :replace, replace: "")
138
+ .downcase
139
+ .gsub(/[^a-z0-9]+/, separator)
140
+ .gsub(/^#{separator}+|#{separator}+$/, "")
141
+ end
142
+
135
143
  def normalize(value)
136
144
  if value.is_a?(Hash)
137
145
  type = value[:type]&.to_s || DEFAULT_TYPE
138
- default = value[:default]&.to_s || DEFAULTS.fetch(type)
146
+ default = value[:default]&.to_s || DEFAULT
139
147
  aliases =
140
- Array.wrap(
148
+ Array(
141
149
  value[:alias]&.to_s || value[:aliases]&.map(&:to_s) ||
142
150
  DEFAULT_ALIASES
143
151
  )
144
152
  else
145
153
  type = value.to_s
146
- default = DEFAULTS.fetch(type)
154
+ default = DEFAULT
147
155
  aliases = DEFAULT_ALIASES
148
156
  end
149
157
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dorian-arguments
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dorian Marié
@@ -10,20 +10,6 @@ bindir: bin
10
10
  cert_chain: []
11
11
  date: 2024-08-24 00:00:00.000000000 Z
12
12
  dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: activesupport
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - ">="
18
- - !ruby/object:Gem::Version
19
- version: '0'
20
- type: :runtime
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - ">="
25
- - !ruby/object:Gem::Version
26
- version: '0'
27
13
  - !ruby/object:Gem::Dependency
28
14
  name: bigdecimal
29
15
  requirement: !ruby/object:Gem::Requirement
@@ -38,20 +24,6 @@ dependencies:
38
24
  - - ">="
39
25
  - !ruby/object:Gem::Version
40
26
  version: '0'
41
- - !ruby/object:Gem::Dependency
42
- name: dorian-to_struct
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - ">="
46
- - !ruby/object:Gem::Version
47
- version: '0'
48
- type: :runtime
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - ">="
53
- - !ruby/object:Gem::Version
54
- version: '0'
55
27
  description: parses arguments
56
28
  email: dorian@dorianmarie.com
57
29
  executables: