dorian-arguments 0.2.0 → 0.3.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 +30 -26
  4. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3842736e3a26ecd94df067996bdc259b7538ea017c7e4779789f31e6a49adf70
4
- data.tar.gz: c1f363f50efb215e6b209b445c178872178dc5b93aae96cf07bd0547f8c938c0
3
+ metadata.gz: e6e446f6aaac5736fad5f8f34cf06f47807106bfcf2c2a6bc9f4c947a5b30b5d
4
+ data.tar.gz: 3afa1cea746fb8a29c4178d476848e9ea26275d6cee249700effd91c2934091a
5
5
  SHA512:
6
- metadata.gz: 0bf3bf93d7445af8e4d53fc1b4d22e08fe6d9ce3d7bf57088ee367abcef21f87b305d30d13fa8dae787fe81abd4e8e3583fcdc1565d94b06ca9bcd382a6d756f
7
- data.tar.gz: a65fb51190305fd70ccaf392c2967ce9fffc28cd6071d1d4efb709b57182351771d253aa823b9f94064862d5ee97850a39073acd91bbfd3ae221f3b5948e9961
6
+ metadata.gz: aca7cbf836751411ecabf3496a0ec0419367d82293cac10e5d95193b011229a4fe3531d307722d2edfdc6333690038f4258c57579ba1b78bebd80a12b9962941
7
+ data.tar.gz: b75f7bab12e64f7727727163c11cbc3eb12e6a1e8e24e6b6fef49ec22f14ac721a47838815c3164d2a36a5505991220c080baa64924d69a35890519c63d098b9
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.3.0
@@ -1,8 +1,10 @@
1
1
  require "dorian/to_struct"
2
2
  require "bigdecimal"
3
+ require "bigdecimal/util"
3
4
  require "active_support"
4
5
  require "active_support/core_ext/string/inflections"
5
6
  require "active_support/core_ext/array/wrap"
7
+ require "active_support/core_ext/enumerable"
6
8
 
7
9
  class Dorian
8
10
  class Arguments
@@ -81,21 +83,11 @@ class Dorian
81
83
  end
82
84
  .reject(&:nil?)
83
85
 
84
- if type == BOOLEAN
85
- values = values.map { |value| BOOLEANS.fetch(value.downcase) }
86
- end
87
-
88
- values = values.map { |value| value.to_i } if type == INTEGER
89
-
90
- if type == DECIMAL || type == NUMBER
91
- values = values.map { |value| BigDecimal(value) }
92
- end
93
-
94
- values = values.first if values.size < 2
95
- values || BOOLEANS.fetch(DEFAULTS.fetch(type)) if type == BOOLEAN
96
-
97
86
  indexes.sort.reverse.uniq.each { |index| arguments.delete_at(index) }
98
87
 
88
+ values = [DEFAULTS.fetch(type)] if values.empty?
89
+ values = values.map { |value| cast(type, value) }
90
+ values = values.first unless values.many?
99
91
  options[key] = values
100
92
  end
101
93
 
@@ -103,12 +95,19 @@ class Dorian
103
95
 
104
96
  arguments -= files
105
97
 
106
- {
107
- arguments:,
108
- options:,
109
- files:,
110
- help:
111
- }.to_deep_struct
98
+ { arguments:, options:, files:, help: }.to_deep_struct
99
+ end
100
+
101
+ def cast(type, value)
102
+ if type == BOOLEAN
103
+ BOOLEANS.fetch(value.downcase)
104
+ elsif type == INTEGER
105
+ value.to_i
106
+ elsif type == DECIMAL || type == NUMBER
107
+ value.to_d
108
+ else
109
+ value
110
+ end
112
111
  end
113
112
 
114
113
  def help
@@ -119,11 +118,14 @@ class Dorian
119
118
  definition.each do |key, value|
120
119
  type, default, aliases = normalize(value)
121
120
 
122
- keys_message = ([key] + aliases).map(&:to_s).map(&:parameterize).map do |key|
123
- key.size == 1 ? "-#{key}" : "--#{key}"
124
- end.join("|")
121
+ keys_message =
122
+ ([key] + aliases)
123
+ .map(&:to_s)
124
+ .map(&:parameterize)
125
+ .map { |key| key.size == 1 ? "-#{key}" : "--#{key}" }
126
+ .join("|")
125
127
 
126
- message += " #{keys_message} #{type.upcase}, default: #{default}\n"
128
+ message += " #{keys_message} #{type.upcase}, default: #{cast(type, default).inspect}\n"
127
129
  end
128
130
 
129
131
  message
@@ -133,9 +135,11 @@ class Dorian
133
135
  if value.is_a?(Hash)
134
136
  type = value[:type]&.to_s || DEFAULT_TYPE
135
137
  default = value[:default]&.to_s || DEFAULTS.fetch(type)
136
- aliases = Array.wrap(
137
- value[:alias]&.to_s || value[:aliases]&.map(&:to_s) || DEFAULT_ALIASES
138
- )
138
+ aliases =
139
+ Array.wrap(
140
+ value[:alias]&.to_s || value[:aliases]&.map(&:to_s) ||
141
+ DEFAULT_ALIASES
142
+ )
139
143
  else
140
144
  type = value.to_s
141
145
  default = DEFAULTS.fetch(type)
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: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dorian Marié