cerulean 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/cerulean.rb +41 -19
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ff333f5e197758c6b4c0614f27179ca77824c62a
4
- data.tar.gz: 598cb45f6432f5cb299ad14caad290cfed03a4c0
3
+ metadata.gz: 8a55c2f55e2a8e0b5706ecc63c45195b3694ebe3
4
+ data.tar.gz: caeff050b3016337f6e3d27c25815f2a53f7eecb
5
5
  SHA512:
6
- metadata.gz: a3a70d26556c3f14880db10ad313b3a90240db82699c840a9e9b2e574cb2c110f1162b821853a270f44de7cff41e7153f8436a35fe9affd515a77aee5dc33499
7
- data.tar.gz: dd80aced8e677fce697dd9533465a7597806751306b490464c9a95ca4b66c10a1ed25f73c023b6fecba31133dcd4d0c38bcc67487476dd68097eb2293db18971
6
+ metadata.gz: cffe40593fc289fefc08e3ae36afd19c11d52eb171a40478e263285e6c9ce4b21c9750babf297ba9bb39f1219d1fd7152a34b85cc7f1db65fb1b7b33b984d2e6
7
+ data.tar.gz: 11d1b53ea9435d18df564290b3639694ddf4c92817f7db332ffa1949828ccb6cc86472048725b07fbb9bbdfeda7de5eb4ec633f0b617ad5e7db189405a6b6253
data/lib/cerulean.rb CHANGED
@@ -80,22 +80,40 @@ module Cerulean
80
80
  @meta ||= self.class.class_variable_get(:@@meta)[action_name.to_sym]
81
81
  end
82
82
 
83
+ def parse_integer(val)
84
+ Integer(val) rescue nil
85
+ end
86
+
87
+ def parse_float(val)
88
+ Float(val) rescue nil
89
+ end
90
+
91
+ def parse_boolean(val)
92
+ case val.to_s.downcase
93
+ when 'true'
94
+ true
95
+ when 'false'
96
+ false
97
+ else
98
+ nil
99
+ end
100
+ end
101
+
83
102
  def validate_param_type(val, type)
84
- case type.to_s
85
- when 'Integer'
86
- Integer(val) rescue false
87
- when 'Float'
88
- Float(val) rescue false
89
- when 'Boolean'
90
- %w(true false).include?(val.to_s.downcase)
91
- when 'Array[Integer]'
92
- !val.any? { |el| !Integer(el) rescue true }
93
- when 'Array[Float]'
94
- !val.any? { |el| !Float(el) rescue true }
95
- when 'Array[Boolean]'
96
- !val.any? { |el| %w(true false).include?(el.downcase) }
103
+ if val.is_a?(Array)
104
+ parsed = val.map { |el| validate_param_type(el, type[0].to_s) }
105
+ parsed.all? ? parsed : nil
97
106
  else
98
- true
107
+ case type.to_s
108
+ when 'Integer'
109
+ parse_integer(val)
110
+ when 'Float'
111
+ parse_float(val)
112
+ when 'Boolean'
113
+ parse_boolean(val)
114
+ when 'String'
115
+ val
116
+ end
99
117
  end
100
118
  end
101
119
 
@@ -112,27 +130,31 @@ module Cerulean
112
130
  @declared = {}
113
131
  meta[:params].each do |param, opts|
114
132
  if params.has_key?(param.to_s)
115
- raise "#{param} is not a #{opts[:type]}" unless validate_param_type(params[param.to_s], opts[:type])
133
+ p = validate_param_type(params[param.to_s], opts[:type])
134
+
135
+ if p.nil?
136
+ raise "#{param} is not a #{opts[:type]}"
137
+ end
116
138
 
117
139
  if opts.has_key?(:min)
118
- if Float(params[param]) < opts[:min]
140
+ if p < opts[:min]
119
141
  raise "#{param} must be greater than or equal to #{opts[:min]}"
120
142
  end
121
143
  end
122
144
 
123
145
  if opts.has_key?(:max)
124
- if Float(params[param]) > opts[:max]
146
+ if p > opts[:max]
125
147
  raise "#{param} must be less than or equal to #{opts[:max]}"
126
148
  end
127
149
  end
128
150
 
129
151
  if opts.has_key?(:values)
130
- unless opts[:values].map { |val| val.to_s }.include?(params[param])
152
+ unless opts[:values].map { |val| val.to_s }.include?(p.to_s)
131
153
  raise "#{param} must be one of #{opts[:values]}"
132
154
  end
133
155
  end
134
156
 
135
- @declared[param] = params[param]
157
+ @declared[param] = p
136
158
  else
137
159
  params[param] = opts[:default] if opts.has_key?(:default)
138
160
  raise "#{param} is required" if opts[:required]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cerulean
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kyle Kestell
@@ -37,7 +37,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
37
37
  version: '0'
38
38
  requirements: []
39
39
  rubyforge_project:
40
- rubygems_version: 2.4.8
40
+ rubygems_version: 2.4.5.1
41
41
  signing_key:
42
42
  specification_version: 4
43
43
  summary: Cerulean