simple_params 1.4.5 → 1.5.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,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- YzIyNzJjMTNjYjNiYWVjYjkwYWNhYzZjOTYyNTY5NWU1NDk3MDAwMg==
4
+ YzgyNzMyMTQzMDg3OGQ0ZjBmMDY1ZjZmMDUyNTNmMjY4OTgzMzU0OA==
5
5
  data.tar.gz: !binary |-
6
- MWZlZGFlMGI2YzMzYzI2MzQ2YzRmMTAwNTJjOTFmYWNjYTkyNTNmMA==
6
+ OGQ3NTZmYzU0YzQ5MGE5NzE2ZDFlYmFkYWI1NmY4OTU2ZmE3ODRlNw==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- OTM5OTlkMDI2MjBlNGRhMzM0MGQzNGIxNjc2NzI0ZjljMDY1M2FkYjQ1MmNm
10
- NjdmMjgyOGVkODAzMmNlZTUzN2MwZmJiNzVkMGNjOTZmMjVkOWM1OTI0MTcw
11
- NDE5NTZkMmIwZDMwYTY5YzQ0MzA0NjMyYThhOWNmNzQ1NDZjYTk=
9
+ MGMwNmYxZjdkNzVjYjNmYjVkMjIzNGFkODBkNDNjYmJhZjgyM2IyMDg4ZDAx
10
+ NTQ5MTUxYmE1N2I2NzMxNWVmYzNkNTY4OGQ1YjMyMGU5OWUwOGJmMzYzYTNj
11
+ NTRiZjFhYjYzODBmNjVmNTNiYjJjOWJiZTIxMmY4OTI5N2M5OGI=
12
12
  data.tar.gz: !binary |-
13
- NDNjOWU2OTFmZGQ3ZjEwYTJmOTY1NGJmYjU5ODU0NTVkNGNhYTY4MmE2YWI1
14
- YmI5ZjQ5ZDU0NzJkNDg2MzQzM2RlMDc0NWE5MzM3NzcxOGU3YjIyZTI3M2Rl
15
- NTBmMDYyNzg1ZGIyNDAyZTRmZDkxZTFmNmFmOTU3ZGJkOGIzMGE=
13
+ NTljMTFlOTI0MGRmODdlOWNmOTgzZDczMzRhMGZiOGE5ODBjZDJhMWQ5YTU0
14
+ NDllYzNiM2Q5NjVjZjI1NWJmMjIxMWY1OTI2NzNkYWY1NzVhMmU3ZmFlNDJl
15
+ YjJiMTE0MGEzMDVlYzg2Y2M5NGMxZTk2ZDQyYjU2ZjUyMTIwNzY=
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- simple_params (1.4.4)
4
+ simple_params (1.4.5)
5
5
  activemodel (>= 3.0, < 5.0)
6
6
  shoulda-matchers (~> 2.8)
7
7
  virtus (>= 1.0.0)
@@ -39,20 +39,6 @@ module SimpleParams
39
39
  end
40
40
  end
41
41
 
42
- def assign_parameter_attributes(pairs)
43
- @p6i ||= pairs["6i"]
44
- @p5i ||= pairs["5i"]
45
- @p4i ||= pairs["4i"]
46
- @p3i ||= pairs["3i"]
47
- @p2i ||= pairs["2i"]
48
- @p1i ||= pairs["1i"]
49
- if all_multiparams_present?
50
- self.value = parse_multiparams
51
- end
52
- rescue ArgumentError
53
- self.value = nil
54
- end
55
-
56
42
  private
57
43
  def raw_default
58
44
  if @default.is_a?(Proc)
@@ -61,25 +47,5 @@ module SimpleParams
61
47
  @default
62
48
  end
63
49
  end
64
-
65
- def all_multiparams_present?
66
- if @type == Date
67
- [@p1i, @p2i, @p3i].all? { |p| !p.nil? }
68
- elsif (@type == DateTime) || (@type == Time)
69
- [@p1i, @p2i, @p3i, @p4i, @p5i, @p6i].all? { |p| !p.nil? }
70
- else
71
- true
72
- end
73
- end
74
-
75
- def parse_multiparams
76
- if @type == Date
77
- Date.new(@p1i.to_i, @p2i.to_i, @p3i.to_i)
78
- elsif @type == DateTime
79
- DateTime.new(@p1i.to_i, @p2i.to_i, @p3i.to_i, @p4i.to_i, @p5i.to_i, @p6i.to_i)
80
- elsif @type == Time
81
- Time.new(@p1i.to_i, @p2i.to_i, @p3i.to_i, @p4i.to_i, @p5i.to_i, @p6i.to_i)
82
- end
83
- end
84
50
  end
85
51
  end
@@ -2,12 +2,6 @@ module SimpleParams
2
2
  module HasAttributes
3
3
  extend ActiveSupport::Concern
4
4
  included do
5
- def define_attributes(params)
6
- self.class.defined_attributes.each_pair do |key, opts|
7
- send("#{key}_attribute=", Attribute.new(self, key, opts))
8
- end
9
- end
10
-
11
5
  def attributes
12
6
  (defined_attributes.keys + nested_classes.keys).flatten
13
7
  end
@@ -38,30 +32,6 @@ module SimpleParams
38
32
  attribute = send("#{name}_attribute")
39
33
  attribute.send("value=", val)
40
34
  end
41
-
42
- if [Date, 'Date', :date].include?(opts[:type])
43
- define_date_helper_methods(name)
44
- elsif [DateTime, 'DateTime', :datetime, Time, 'Time', :time].include?(opts[:type])
45
- define_datetime_helper_methods(name)
46
- end
47
- end
48
-
49
- def add_validations(name, opts = {})
50
- validations = opts[:validations] || {}
51
- has_default = opts.has_key?(:default) # checking has_key? because :default may be nil
52
- optional = opts[:optional]
53
- if !validations.empty?
54
- if optional || has_default
55
- validations.merge!(allow_nil: true)
56
- else
57
- validations.merge!(presence: true)
58
- end
59
- else
60
- if !optional && !has_default
61
- validations.merge!(presence: true)
62
- end
63
- end
64
- validates name, validations unless validations.empty?
65
35
  end
66
36
  end
67
37
  end
@@ -32,9 +32,9 @@ module SimpleParams
32
32
  if nested_class.is_a?(Array)
33
33
  # Have to map? & THEN all?, or else it won't
34
34
  # necessarily call valid? on every object
35
- nested_class.map(&:valid?).all?
35
+ nested_class.map { |klass| klass.nil? || klass.valid? }.all?
36
36
  else
37
- nested_class.valid?
37
+ nested_class.nil? || nested_class.valid?
38
38
  end
39
39
  end
40
40
  end
@@ -34,20 +34,12 @@ module SimpleParams
34
34
 
35
35
  def clear
36
36
  super
37
- @nested_classes.map do |attribute, klass|
38
- run_or_mapped_run(klass) do |k|
39
- k.errors.clear
40
- end
41
- end
37
+ nested_instances.each { |i| i.errors.clear }
42
38
  end
43
39
 
44
40
  def empty?
45
41
  super &&
46
- @nested_classes.all? do |attribute, klass|
47
- run_or_mapped_run(klass) do |k|
48
- k.errors.empty?
49
- end
50
- end
42
+ nested_instances.all? { |i| i.errors.empty? }
51
43
  end
52
44
  alias_method :blank?, :empty?
53
45
 
@@ -63,20 +55,15 @@ module SimpleParams
63
55
 
64
56
  def values
65
57
  messages.values +
66
- @nested_classes.map do |key, klass|
67
- run_or_mapped_run(klass) { |k| k.errors.values }
68
- end
58
+ nested_instances.map { |i| i.errors.values }
69
59
  end
70
60
 
71
61
  def full_messages
72
62
  parent_messages = map { |attribute, message| full_message(attribute, message) }
73
- nested_messages = @nested_classes.map do |attribute, klass|
74
- run_or_mapped_run(klass) do |k|
75
- unless k.errors.full_messages.nil?
76
- k.errors.full_messages.map { |message| "#{attribute} " + message }
77
- end
78
- end
63
+ nested_messages = nested_instances.map do |i|
64
+ i.errors.full_messages.map { |message| "#{i.parent_attribute_name} " + message }
79
65
  end
66
+
80
67
  (parent_messages + nested_messages).flatten
81
68
  end
82
69
 
@@ -93,11 +80,12 @@ module SimpleParams
93
80
  msgs.merge!(attribute.to_sym => nested_msgs)
94
81
  end
95
82
  end
83
+
96
84
  msgs
97
85
  end
98
86
 
99
87
  def to_s(full_messages = false)
100
- array = to_a
88
+ array = to_a.compact
101
89
  array.join(', ')
102
90
  end
103
91
 
@@ -135,7 +123,7 @@ module SimpleParams
135
123
  end
136
124
 
137
125
  def empty_messages?(msgs)
138
- msgs.nil? || msgs.empty? || (msgs.is_a?(Array) && msgs.all?(&:empty?))
126
+ msgs.nil? || msgs.empty? || (msgs.is_a?(Array) && msgs.all? { |m| m.nil? || m.empty? })
139
127
  end
140
128
 
141
129
  def run_or_mapped_run(object, &block)
@@ -149,5 +137,21 @@ module SimpleParams
149
137
  def symbolize_nested(nested)
150
138
  nested.inject({}) { |memo,(k,v) | memo[k.to_sym] = v; memo }
151
139
  end
140
+
141
+
142
+ def nested_instances
143
+ array =[]
144
+ @nested_classes.each do |key, instance|
145
+ if instance.is_a?(Array)
146
+ array << instance
147
+ array = array.flatten.compact
148
+ else
149
+ unless instance.nil?
150
+ array << instance
151
+ end
152
+ end
153
+ end
154
+ array
155
+ end
152
156
  end
153
157
  end
@@ -0,0 +1,34 @@
1
+ require "active_model"
2
+
3
+ module SimpleParams
4
+ class HashBuilder
5
+ def initialize(params)
6
+ @params = params
7
+ end
8
+
9
+ # TODO: This still needs specs around it, as well as a SIGNIFICANT refactor
10
+ def build
11
+ hash = {}
12
+ attributes = @params.attributes
13
+ attributes.each do |attribute|
14
+ raw_attribute = @params.send(attribute)
15
+ if raw_attribute.nil?
16
+ hash[attribute] = nil
17
+ elsif raw_attribute.is_a?(SimpleParams::Params)
18
+ hash[attribute] = @params.send(attribute).to_hash
19
+ elsif raw_attribute.is_a?(Array)
20
+ attribute_array = []
21
+ raw_attribute.each do |r_attr|
22
+ unless r_attr.nil?
23
+ attribute_array << r_attr.to_hash
24
+ end
25
+ end
26
+ hash[attribute] = attribute_array
27
+ else
28
+ hash[attribute] = @params.send(attribute)
29
+ end
30
+ end
31
+ hash
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,52 @@
1
+ module SimpleParams
2
+ class InitializationHash < ::Hash
3
+ include SimpleParams::HashHelpers
4
+
5
+ DATETIME_INDICATORS = ['(6i)', '(5i)', '(4i)', '(3i)', '(2i)', '(1i)']
6
+
7
+ attr_reader :original_params
8
+
9
+ def initialize(inputs={})
10
+ @original_params = hash_to_symbolized_hash(inputs)
11
+ assign_parameters
12
+ end
13
+
14
+ private
15
+ def assign_parameters
16
+ original_params.each_pair do |key, value|
17
+ date_time_found = false
18
+ DATETIME_INDICATORS.each do |indicator|
19
+ if key.to_s.include?(indicator)
20
+ date_time_found = true
21
+ key_name = key.to_s.partition(indicator).first
22
+ assign_datetime_params(key_name)
23
+ end
24
+ end
25
+ unless date_time_found
26
+ assign_param(key, value)
27
+ end
28
+ end
29
+ end
30
+
31
+ def assign_datetime_params(key)
32
+ p6i = original_params[:"#{key}(6i)"]
33
+ p5i = original_params[:"#{key}(5i)"]
34
+ p4i = original_params[:"#{key}(4i)"]
35
+ p3i = original_params[:"#{key}(3i)"]
36
+ p2i = original_params[:"#{key}(2i)"]
37
+ p1i = original_params[:"#{key}(1i)"]
38
+ value = if [p1i, p2i, p3i].all? && [p4i, p5i, p6i].none?
39
+ Date.new(p1i.to_i, p2i.to_i, p3i.to_i)
40
+ elsif [p6i, p5i, p4i, p3i, p1i, p1i].all?
41
+ Time.new(p1i.to_i, p2i.to_i, p3i.to_i, p4i.to_i, p5i.to_i, p6i.to_i)
42
+ end
43
+ self[key.to_sym] = value
44
+ rescue ArgumentError
45
+ self[key.to_sym] = nil
46
+ end
47
+
48
+ def assign_param(key, value)
49
+ self[key.to_sym] = value
50
+ end
51
+ end
52
+ end
@@ -17,71 +17,76 @@ module SimpleParams
17
17
  !!options[:with_ids]
18
18
  end
19
19
 
20
+ def optional?
21
+ !!options[:optional]
22
+ end
23
+
20
24
  def define_new_hash_class(parent, name, options, &block)
21
- options = options.merge(type: :hash)
22
- define_new_class(parent, name, options, &block)
25
+ define_new_class(parent, name, options.merge(type: :hash), &block)
23
26
  end
24
27
 
25
28
  def define_new_array_class(parent, name, options, &block)
26
- options = options.merge(type: :array)
27
- define_new_class(parent, name, options, &block)
29
+ define_new_class(parent, name, options.merge(type: :array), &block)
30
+ end
31
+
32
+ def build(params, parent, name)
33
+ if params.is_a?(Array)
34
+ params.map { |p| build_instance(p, parent, name) }.compact
35
+ elsif with_ids?
36
+ params.each_pair.map { |key, val| build_instance({key => val}, parent, name) }.compact
37
+ else
38
+ build_instance(params, parent, name)
39
+ end
28
40
  end
29
41
 
30
42
  private
31
43
  def define_new_class(parent, name, options, &block)
32
- klass_name = name.to_s.split('_').collect(&:capitalize).join
33
- Class.new(self).tap do |klass|
34
- parent.send(:remove_const, klass_name) if parent.const_defined?(klass_name)
35
- parent.const_set(klass_name, klass)
36
- klass.instance_eval <<-DEF
37
- def parent_class
38
- #{parent}
39
- end
40
- DEF
41
- extend ActiveModel::Naming
42
- klass.class_eval(&block)
43
- klass.class_eval("self.options = #{options}")
44
- if klass.parent_class.using_rails_helpers?
45
- klass.instance_eval("with_rails_helpers")
46
- end
47
-
48
- # define a _destroy param (Boolean, default: false)
49
- if klass.using_rails_helpers?
50
- klass.send(:define_attribute, :_destroy, {type: :boolean, default: false})
51
- end
52
- end
44
+ NestedParamsClassBuilder.new(parent).build(self, name, options, &block)
53
45
  end
54
- end
55
46
 
56
- def initialize(params={}, parent = nil)
57
- @parent = parent
58
- super(params)
47
+ def build_instance(params, parent, name)
48
+ instance = self.new(params, parent, name)
49
+ instance.destroyed? ? nil : instance
50
+ end
59
51
  end
60
52
 
61
- def id
62
- @id ||= nil
63
- end
53
+ attr_reader :parent, :id, :params, :parent_attribute_name
64
54
 
65
- def set_accessors(params={})
66
- if class_has_ids?
67
- @id = params.keys.first
68
- params = params.values.first || {}
69
- end
55
+ def initialize(params={}, parent, parent_attribute_name)
56
+ @parent = parent
57
+ @parent_attribute_name = parent_attribute_name.to_sym
58
+ @id = extract_id(params)
59
+ @params = extract_initialization_params(params)
60
+ super(@params)
61
+ end
70
62
 
71
- super(params)
63
+ def destroyed?
64
+ sym_params = symbolize_params(params)
65
+ [true, 1, "1", "true"].include?(sym_params[:_destroy])
72
66
  end
73
67
 
74
68
  private
75
- def class_has_ids?
69
+ def with_ids?
76
70
  self.class.with_ids?
77
71
  end
78
72
 
79
- def hash_class?
80
- self.class.hash?
73
+ def symbolize_params(params)
74
+ Hash[params.map{ |k, v| [k.to_sym, v] }]
81
75
  end
82
76
 
83
- def array_class?
84
- self.class.array?
77
+ def extract_id(params)
78
+ if with_ids?
79
+ id = params.keys[0]
80
+ id ? id.to_sym : nil
81
+ end
82
+ end
83
+
84
+ def extract_initialization_params(params)
85
+ if with_ids?
86
+ params.values[0] || {}
87
+ else
88
+ params || {}
89
+ end
85
90
  end
86
91
  end
87
92
  end
@@ -0,0 +1,32 @@
1
+ module SimpleParams
2
+ class NestedParamsClassBuilder
3
+ def initialize(parent)
4
+ @parent = parent
5
+ end
6
+
7
+ #TODO: Need to test this!
8
+ def build(nested_params, name, options, &block)
9
+ klass_name = name.to_s.split('_').collect(&:capitalize).join
10
+ Class.new(nested_params).tap do |klass|
11
+ @parent.send(:remove_const, klass_name) if @parent.const_defined?(klass_name)
12
+ @parent.const_set(klass_name, klass)
13
+ klass.instance_eval <<-DEF
14
+ def parent_class
15
+ #{@parent}
16
+ end
17
+ DEF
18
+ klass.class_eval('extend ActiveModel::Naming')
19
+ klass.class_eval(&block)
20
+ klass.class_eval("self.options = #{options}")
21
+ if klass.parent_class.using_rails_helpers?
22
+ klass.instance_eval("with_rails_helpers")
23
+ end
24
+
25
+ # define a _destroy param (Boolean, default: false)
26
+ if klass.using_rails_helpers?
27
+ klass.send(:define_attribute, :_destroy, {type: :boolean, default: false})
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -11,8 +11,6 @@ module SimpleParams
11
11
  include SimpleParams::Validations
12
12
  include SimpleParams::HasAttributes
13
13
  include SimpleParams::HasTypedParams
14
- include SimpleParams::HashHelpers
15
- include SimpleParams::DateTimeHelpers
16
14
  include SimpleParams::StrictParams
17
15
 
18
16
  class << self
@@ -31,6 +29,11 @@ module SimpleParams
31
29
  add_validations(name, opts)
32
30
  end
33
31
 
32
+ def add_validations(name, opts = {})
33
+ validation_string = ValidationBuilder.new(name, opts).validation_string
34
+ eval(validation_string)
35
+ end
36
+
34
37
  def nested_classes
35
38
  @nested_classes ||= {}
36
39
  end
@@ -72,44 +75,18 @@ module SimpleParams
72
75
  else
73
76
  # This logic basically sets the nested class to an instance of itself, unless
74
77
  # it is optional.
75
- klass_instance = klass.new({}, self)
76
- init_value = if opts[:optional]
77
- NilParams.define_nil_class(klass).new
78
+ init = if opts[:optional]
79
+ nil
78
80
  else
79
- klass_instance
81
+ klass_instance = klass.new({}, self, name)
80
82
  end
81
- init_value = klass.hash? ? init_value : [init_value]
83
+ init_value = klass.hash? ? init : [init]
82
84
  instance_variable_set("@#{name}", init_value)
83
85
  end
84
86
  end
85
87
 
86
88
  define_method("#{name}=") do |initializer|
87
- init_value = if initializer.is_a?(Array)
88
- initializer.map do |val|
89
- destroyed = if val.has_key?(:_destroy)
90
- val[:_destroy]
91
- elsif val.has_key?("_destroy")
92
- val["_destroy"]
93
- end
94
- unless [true, 1, "1", "true"].include?(destroyed)
95
- klass.new(val, self)
96
- end
97
- end.compact
98
- elsif klass.with_ids?
99
- initializer.each_pair.inject([]) do |array, (key, val)|
100
- destroyed = if val.has_key?(:_destroy)
101
- val[:_destroy]
102
- elsif val.has_key?("_destroy")
103
- val["_destroy"]
104
- end
105
- unless [true, 1, "1", "true"].include?(destroyed)
106
- array << klass.new({key => val}, self)
107
- end
108
- array
109
- end
110
- else
111
- klass.new(initializer, self)
112
- end
89
+ init_value = klass.build(initializer, self, name)
113
90
  instance_variable_set("@#{name}", init_value)
114
91
  end
115
92
  end
@@ -121,34 +98,23 @@ module SimpleParams
121
98
 
122
99
  def initialize(params={})
123
100
  set_strictness
124
-
125
- @original_params = hash_to_symbolized_hash(params)
101
+ params = InitializationHash.new(params)
102
+ @original_params = params.original_params
126
103
  define_attributes(@original_params)
127
104
 
128
105
  # Nested Classes
129
106
  @nested_classes = nested_classes.keys
130
-
131
107
  set_accessors(params)
132
108
  end
133
109
 
134
- def to_hash
135
- hash = {}
136
- attributes.each do |attribute|
137
- raw_attribute = send(attribute)
138
- if raw_attribute.is_a?(SimpleParams::Params)
139
- hash[attribute] = send(attribute).to_hash
140
- elsif raw_attribute.is_a?(Array)
141
- attribute_array = []
142
- raw_attribute.each do |r_attr|
143
- attribute_array << r_attr.to_hash
144
- end
145
- hash[attribute] = attribute_array
146
- else
147
- hash[attribute] = send(attribute)
148
- end
110
+ def define_attributes(params)
111
+ self.class.defined_attributes.each_pair do |key, opts|
112
+ send("#{key}_attribute=", Attribute.new(self, key, opts))
149
113
  end
114
+ end
150
115
 
151
- hash
116
+ def to_hash
117
+ HashBuilder.new(self).build
152
118
  end
153
119
 
154
120
  def errors
@@ -156,7 +122,6 @@ module SimpleParams
156
122
  @nested_classes.each do |param|
157
123
  nested_class_hash[param.to_sym] = send(param)
158
124
  end
159
-
160
125
  @errors ||= SimpleParams::Errors.new(self, nested_class_hash)
161
126
  end
162
127
 
@@ -0,0 +1,31 @@
1
+ module SimpleParams
2
+ class ValidationBuilder
3
+ def initialize(name, opts={})
4
+ @name = name
5
+ @opts = opts
6
+ end
7
+
8
+ def validation_string
9
+ validations = @opts[:validations] || {}
10
+ has_default = @opts.has_key?(:default) # checking has_key? because :default may be nil
11
+ optional = @opts[:optional]
12
+ if !validations.empty?
13
+ if optional || has_default
14
+ validations.merge!(allow_nil: true)
15
+ else
16
+ validations.merge!(presence: true)
17
+ end
18
+ else
19
+ if !optional && !has_default
20
+ validations.merge!(presence: true)
21
+ end
22
+ end
23
+
24
+ if validations.empty?
25
+ return ''
26
+ else
27
+ return "validates :#{@name.to_sym}, #{validations}"
28
+ end
29
+ end
30
+ end
31
+ end
@@ -1,3 +1,3 @@
1
1
  module SimpleParams
2
- VERSION = "1.4.5"
2
+ VERSION = "1.5.0"
3
3
  end
data/lib/simple_params.rb CHANGED
@@ -3,17 +3,19 @@ require 'simple_params/attribute'
3
3
  require 'simple_params/formatter'
4
4
  require 'simple_params/errors'
5
5
  require 'simple_params/concerns/has_attributes'
6
- require 'simple_params/concerns/date_time_helpers'
7
6
  require 'simple_params/concerns/hash_helpers'
8
7
  require 'simple_params/concerns/has_typed_params'
9
8
  require 'simple_params/concerns/rails_helpers'
10
9
  require 'simple_params/concerns/strict_params'
11
10
  require 'simple_params/concerns/validations'
12
11
  require 'simple_params/params'
12
+ require 'simple_params/initialization_hash'
13
+ require 'simple_params/hash_builder'
13
14
  require 'simple_params/nested_params'
14
- require 'simple_params/nil_params'
15
+ require 'simple_params/nested_params_class_builder'
15
16
  require 'simple_params/simple_params_error'
16
17
  require 'simple_params/type_mappings'
18
+ require 'simple_params/validation_builder'
17
19
  require 'simple_params/api_pie_doc'
18
20
  require 'simple_params/api_pie_doc/attribute_base'
19
21
  require 'simple_params/api_pie_doc/attribute'
@@ -34,6 +34,11 @@ class AcceptanceParams < SimpleParams::Params
34
34
  param :name
35
35
  end
36
36
 
37
+ nested_array :birds, optional: true, with_ids: true do
38
+ with_rails_helpers
39
+ param :name
40
+ end
41
+
37
42
  before_validation :set_current_time
38
43
 
39
44
  def set_current_time
@@ -117,7 +122,7 @@ describe SimpleParams::Params do
117
122
  state: "North Carolina",
118
123
  company: nil
119
124
  },
120
- phone: {},
125
+ phone: nil,
121
126
  dogs: [
122
127
  {
123
128
  name: "Spot",
@@ -130,6 +135,8 @@ describe SimpleParams::Params do
130
135
  name: nil,
131
136
  _destroy: false
132
137
  }
138
+ ],
139
+ birds: [
133
140
  ]
134
141
  })
135
142
  end
@@ -216,18 +223,19 @@ describe SimpleParams::Params do
216
223
  "current_time(2i)" => "10",
217
224
  "current_time(1i)" => "2015"
218
225
  )
219
- params.current_time.should eq(DateTime.new(2015, 10, 29, 9, 11, 56))
226
+ params.current_time.should eq(DateTime.new(2015, 10, 29, 9, 11, 56, '-04:00'))
220
227
  end
221
228
  end
222
229
 
223
230
  describe "attributes", attributes: true do
224
231
  it "returns array of attribute symbols" do
225
232
  params = AcceptanceParams.new
226
- params.attributes.should eq([:reference, :name, :date_of_birth, :current_time, :age, :color, :sibling_names, :address, :phone, :dogs, :cats])
233
+ params.attributes.should eq([:reference, :name, :date_of_birth, :current_time, :age, :color, :sibling_names, :address, :phone, :dogs, :cats, :birds])
227
234
  end
228
235
 
229
- it "returns array of attribute symbols for nested class" do
230
- params = AcceptanceParams::Address.new
236
+ it "returns array of attribute symbols for nested class", failing: true do
237
+ params = AcceptanceParams::Address.new({}, nil, "address")
238
+ params.parent_attribute_name.should eq(:address)
231
239
  params.attributes.should eq([:street, :city, :zip_code, :state, :company])
232
240
  end
233
241
 
@@ -343,6 +351,32 @@ describe SimpleParams::Params do
343
351
  params.should_not be_valid
344
352
  params.errors[:dogs][1][:age].should eq(["is not included in the list", "can't be blank"])
345
353
  end
354
+
355
+ # it "initializes birds as empty", failing: true do
356
+ # params = AcceptanceParams.new
357
+ # params.birds.should be_empty
358
+ # end
359
+
360
+ # it "allows absense of optional params", failing: true do
361
+ # params = AcceptanceParams.new(
362
+ # name: "test",
363
+ # address: {
364
+ # street: "1 Main St.",
365
+ # city: "Asheville",
366
+ # zip_code: "28806"
367
+ # },
368
+ # dogs: [
369
+ # { name: "spot", age: 13 }
370
+ # ],
371
+ # cats: {
372
+ # "0" => {
373
+ # name: "Purr"
374
+ # }
375
+ # },
376
+ # )
377
+ # params.birds.should be_empty
378
+ # params.should be_valid
379
+ # end
346
380
  end
347
381
 
348
382
  describe "#validate!" do
@@ -383,7 +417,9 @@ describe SimpleParams::Params do
383
417
  acceptance_params = AcceptanceParams.new(params)
384
418
  acceptance_params.valid?
385
419
  acceptance_params.should be_valid
420
+ acceptance_params.errors.should be_empty
386
421
  acceptance_params.should be_valid
422
+ acceptance_params.errors.should be_empty
387
423
  end
388
424
 
389
425
  it "is invalidated if validity changes after initial assignment" do
@@ -580,6 +616,10 @@ describe SimpleParams::Params do
580
616
  param :name, String, desc:'', required: true
581
617
  param :_destroy, Boolean, desc:'', required: false
582
618
  end
619
+ param :birds, Array, desc:'', required: false do
620
+ param :name, String, desc:'', required: true
621
+ param :_destroy, Boolean, desc:'', required: false
622
+ end
583
623
  API_PIE_DOCS
584
624
 
585
625
  expect(documentation).to be_a String
data/spec/errors_spec.rb CHANGED
@@ -44,6 +44,10 @@ describe SimpleParams::Errors do
44
44
  nil
45
45
  end
46
46
 
47
+ def parent_attribute_name
48
+ :dog
49
+ end
50
+
47
51
  def read_attribute_for_validation(attr)
48
52
  send(attr)
49
53
  end
@@ -71,6 +75,10 @@ describe SimpleParams::Errors do
71
75
  123
72
76
  end
73
77
 
78
+ def parent_attribute_name
79
+ :cats
80
+ end
81
+
74
82
  def read_attribute_for_validation(attr)
75
83
  send(attr)
76
84
  end
@@ -0,0 +1,71 @@
1
+ require 'spec_helper'
2
+
3
+ describe SimpleParams::InitializationHash do
4
+ let(:raw_params) do
5
+ {
6
+ "name" => "Emily",
7
+ "age" => "28",
8
+ "date_of_birth(3i)" => "30",
9
+ "date_of_birth(2i)" => "4",
10
+ "date_of_birth(1i)" => "1987",
11
+ "current_time(6i)" => "56",
12
+ "current_time(5i)" => "11",
13
+ "current_time(4i)" => "9",
14
+ "current_time(3i)" => "29",
15
+ "current_time(2i)" => "10",
16
+ "current_time(1i)" => "2015",
17
+ "invalid_time(6i)" => "56",
18
+ "invalid_time(5i)" => nil,
19
+ "invalid_time(4i)" => "9",
20
+ "invalid_time(3i)" => nil,
21
+ "invalid_time(2i)" => "10",
22
+ "invalid_time(1i)" => "2015"
23
+ }
24
+ end
25
+
26
+ let(:initialization_hash) do
27
+ described_class.new(raw_params)
28
+ end
29
+
30
+ it "has correct original_params" do
31
+ initialization_hash.original_params.should eq(
32
+ :name => "Emily",
33
+ :age => "28",
34
+ :"date_of_birth(3i)" => "30",
35
+ :"date_of_birth(2i)" => "4",
36
+ :"date_of_birth(1i)" => "1987",
37
+ :"current_time(6i)" => "56",
38
+ :"current_time(5i)" => "11",
39
+ :"current_time(4i)" => "9",
40
+ :"current_time(3i)" => "29",
41
+ :"current_time(2i)" => "10",
42
+ :"current_time(1i)" => "2015",
43
+ :"invalid_time(6i)" => "56",
44
+ :"invalid_time(5i)" => nil,
45
+ :"invalid_time(4i)" => "9",
46
+ :"invalid_time(3i)" => nil,
47
+ :"invalid_time(2i)" => "10",
48
+ :"invalid_time(1i)" => "2015"
49
+ )
50
+ end
51
+
52
+ it "has correct name" do
53
+ initialization_hash[:name].should eq("Emily")
54
+ end
55
+
56
+ it "has correct age" do
57
+ initialization_hash[:age].should eq("28")
58
+ end
59
+
60
+ it "has correct date_of_birth" do
61
+ initialization_hash[:date_of_birth].should eq(Date.new(1987, 4, 30))
62
+ end
63
+
64
+ it "has correct current_time" do
65
+ initialization_hash[:current_time].should eq(Time.new(2015, 10, 29, 9, 11, 56))
66
+ end
67
+
68
+ it "has nil invalid_time" do
69
+ initialization_hash[:invalid_time].should eq(nil)
70
+ end
71
+ end
@@ -102,7 +102,7 @@ describe SimpleParams::NestedParams do
102
102
  end
103
103
  end
104
104
 
105
- subject { defined_class.new(name: "Bill", age: 21) }
105
+ subject { defined_class.new({name: "Bill", age: 21}, nil, :my_special_params) }
106
106
 
107
107
  specify "name" do
108
108
  expect(subject.name).to eq "Bill"
@@ -125,7 +125,7 @@ describe SimpleParams::NestedParams do
125
125
  end
126
126
  end
127
127
 
128
- subject { defined_class.new("132" => { name: "Bill", age: 21 }) }
128
+ subject { defined_class.new({ "132" => { name: "Bill", age: 21 } }, nil, :my_special_params) }
129
129
 
130
130
  specify "name" do
131
131
  expect(subject.name).to eq "Bill"
@@ -136,7 +136,7 @@ describe SimpleParams::NestedParams do
136
136
  end
137
137
 
138
138
  specify "id" do
139
- expect(subject.id).to eq "132"
139
+ expect(subject.id).to eq :"132"
140
140
  end
141
141
  end
142
142
  end
@@ -150,7 +150,7 @@ describe SimpleParams::NestedParams do
150
150
  end
151
151
  end
152
152
 
153
- subject { defined_class.new(name: "Bill", age: 21) }
153
+ subject { defined_class.new({name: "Bill", age: 21}, nil, :my_special_params) }
154
154
 
155
155
  specify "name" do
156
156
  expect(subject.name).to eq "Bill"
@@ -173,7 +173,7 @@ describe SimpleParams::NestedParams do
173
173
  end
174
174
  end
175
175
 
176
- subject { defined_class.new("132" => { name: "Bill", age: 21 }) }
176
+ subject { defined_class.new({ "132" => { name: "Bill", age: 21 } }, nil, :my_special_params) }
177
177
 
178
178
  specify "name" do
179
179
  expect(subject.name).to eq "Bill"
@@ -184,7 +184,7 @@ describe SimpleParams::NestedParams do
184
184
  end
185
185
 
186
186
  specify "id" do
187
- expect(subject.id).to eq "132"
187
+ expect(subject.id).to eq :"132"
188
188
  end
189
189
  end
190
190
  end
@@ -49,7 +49,7 @@ describe SimpleParams::Params do
49
49
  end
50
50
 
51
51
  specify "setting datetime" do
52
- expect(params.current_time).to eq DateTime.new(2009, 3, 4, 11, 58, 59)
52
+ expect(params.current_time).to eq DateTime.new(2009, 3, 4, 11, 58, 59, '-05:00')
53
53
  end
54
54
 
55
55
  specify "sets address" do
@@ -0,0 +1,59 @@
1
+ require 'spec_helper'
2
+
3
+ describe SimpleParams::ValidationBuilder do
4
+ let(:name) { "my_attribute" }
5
+
6
+ context "with blank opts" do
7
+ let(:builder) { described_class.new(name) }
8
+
9
+ it "has correct validation string" do
10
+ builder.validation_string.should eq(
11
+ 'validates :my_attribute, {:presence=>true}'
12
+ )
13
+ end
14
+ end
15
+
16
+ context "with optional" do
17
+ let(:opts) do
18
+ {
19
+ optional: true
20
+ }
21
+ end
22
+
23
+ let(:builder) { described_class.new(name, opts) }
24
+
25
+ it "has correct validation string" do
26
+ builder.validation_string.should eq('')
27
+ end
28
+ end
29
+
30
+ context "with default" do
31
+ let(:opts) do
32
+ {
33
+ default: Proc.new { Time.now }
34
+ }
35
+ end
36
+
37
+ let(:builder) { described_class.new(name, opts) }
38
+
39
+ it "has correct validation string" do
40
+ builder.validation_string.should eq('')
41
+ end
42
+ end
43
+
44
+ context "with other validations" do
45
+ let(:opts) do
46
+ {
47
+ validations: { presence: true, length: { in: [0..20]} }
48
+ }
49
+ end
50
+
51
+ let(:builder) { described_class.new(name, opts) }
52
+
53
+ it "has correct validation string" do
54
+ builder.validation_string.should eq(
55
+ 'validates :my_attribute, {:presence=>true, :length=>{:in=>[0..20]}}'
56
+ )
57
+ end
58
+ end
59
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_params
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.5
4
+ version: 1.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - brycesenz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-09-07 00:00:00.000000000 Z
11
+ date: 2015-12-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -133,7 +133,6 @@ files:
133
133
  - lib/simple_params/api_pie_doc/attribute_base.rb
134
134
  - lib/simple_params/api_pie_doc/nested_attribute.rb
135
135
  - lib/simple_params/attribute.rb
136
- - lib/simple_params/concerns/date_time_helpers.rb
137
136
  - lib/simple_params/concerns/has_attributes.rb
138
137
  - lib/simple_params/concerns/has_typed_params.rb
139
138
  - lib/simple_params/concerns/hash_helpers.rb
@@ -142,11 +141,14 @@ files:
142
141
  - lib/simple_params/concerns/validations.rb
143
142
  - lib/simple_params/errors.rb
144
143
  - lib/simple_params/formatter.rb
144
+ - lib/simple_params/hash_builder.rb
145
+ - lib/simple_params/initialization_hash.rb
145
146
  - lib/simple_params/nested_params.rb
146
- - lib/simple_params/nil_params.rb
147
+ - lib/simple_params/nested_params_class_builder.rb
147
148
  - lib/simple_params/params.rb
148
149
  - lib/simple_params/simple_params_error.rb
149
150
  - lib/simple_params/type_mappings.rb
151
+ - lib/simple_params/validation_builder.rb
150
152
  - lib/simple_params/validation_matchers/coercion_matcher.rb
151
153
  - lib/simple_params/validation_matchers/format_matcher.rb
152
154
  - lib/simple_params/validation_matchers/nested_array_matcher.rb
@@ -165,13 +167,14 @@ files:
165
167
  - spec/fixtures/dummy_params.rb
166
168
  - spec/fixtures/validator_params.rb
167
169
  - spec/formatter_spec.rb
170
+ - spec/initialization_hash_spec.rb
168
171
  - spec/multiple_classes_spec.rb
169
172
  - spec/nested_params_spec.rb
170
- - spec/nil_params_spec.rb
171
173
  - spec/params_spec.rb
172
174
  - spec/rails_integration_spec.rb
173
175
  - spec/spec_helper.rb
174
176
  - spec/support/shared_examples/base_attribute.rb
177
+ - spec/validation_builder_spec.rb
175
178
  - spec/validation_matchers/coercion_matcher_spec.rb
176
179
  - spec/validation_matchers/format_matcher_spec.rb
177
180
  - spec/validation_matchers/nested_array_matcher_spec.rb
@@ -212,13 +215,14 @@ test_files:
212
215
  - spec/fixtures/dummy_params.rb
213
216
  - spec/fixtures/validator_params.rb
214
217
  - spec/formatter_spec.rb
218
+ - spec/initialization_hash_spec.rb
215
219
  - spec/multiple_classes_spec.rb
216
220
  - spec/nested_params_spec.rb
217
- - spec/nil_params_spec.rb
218
221
  - spec/params_spec.rb
219
222
  - spec/rails_integration_spec.rb
220
223
  - spec/spec_helper.rb
221
224
  - spec/support/shared_examples/base_attribute.rb
225
+ - spec/validation_builder_spec.rb
222
226
  - spec/validation_matchers/coercion_matcher_spec.rb
223
227
  - spec/validation_matchers/format_matcher_spec.rb
224
228
  - spec/validation_matchers/nested_array_matcher_spec.rb
@@ -1,59 +0,0 @@
1
- module SimpleParams
2
- module DateTimeHelpers
3
- extend ActiveSupport::Concern
4
-
5
- included do
6
- end
7
-
8
- module ClassMethods
9
- def define_date_helper_methods(name)
10
- define_method("#{name}(3i)=") do |day|
11
- attribute = send("#{name}_attribute")
12
- attribute.assign_parameter_attributes("3i" => day)
13
- end
14
-
15
- define_method("#{name}(2i)=") do |month|
16
- attribute = send("#{name}_attribute")
17
- attribute.assign_parameter_attributes("2i" => month)
18
- end
19
-
20
- define_method("#{name}(1i)=") do |year|
21
- attribute = send("#{name}_attribute")
22
- attribute.assign_parameter_attributes("1i" => year)
23
- end
24
- end
25
-
26
- def define_datetime_helper_methods(name)
27
- define_method("#{name}(6i)=") do |sec|
28
- attribute = send("#{name}_attribute")
29
- attribute.assign_parameter_attributes("6i" => sec)
30
- end
31
-
32
- define_method("#{name}(5i)=") do |minute|
33
- attribute = send("#{name}_attribute")
34
- attribute.assign_parameter_attributes("5i" => minute)
35
- end
36
-
37
- define_method("#{name}(4i)=") do |hour|
38
- attribute = send("#{name}_attribute")
39
- attribute.assign_parameter_attributes("4i" => hour)
40
- end
41
-
42
- define_method("#{name}(3i)=") do |day|
43
- attribute = send("#{name}_attribute")
44
- attribute.assign_parameter_attributes("3i" => day)
45
- end
46
-
47
- define_method("#{name}(2i)=") do |month|
48
- attribute = send("#{name}_attribute")
49
- attribute.assign_parameter_attributes("2i" => month)
50
- end
51
-
52
- define_method("#{name}(1i)=") do |year|
53
- attribute = send("#{name}_attribute")
54
- attribute.assign_parameter_attributes("1i" => year)
55
- end
56
- end
57
- end
58
- end
59
- end
@@ -1,65 +0,0 @@
1
- module SimpleParams
2
- class NilParams < NestedParams
3
- class << self
4
- def define_nil_class(parent)
5
- define_new_class(parent, :nil_params, {}) do
6
- end
7
- end
8
-
9
- def nested_classes
10
- if respond_to?(:parent_class) && parent_class.present?
11
- parent_class.nested_classes
12
- else
13
- {}
14
- end
15
- end
16
- end
17
-
18
- def initialize(params = {})
19
- self.class.options ||= {}
20
- @mocked_object = if parent_class
21
- parent_class.new
22
- else
23
- nil
24
- end
25
- super(params)
26
- end
27
-
28
- def valid?
29
- true
30
- end
31
-
32
- def to_hash
33
- {}
34
- end
35
-
36
- def errors
37
- Errors.new(self)
38
- end
39
-
40
- def method_missing(method_name, *arguments, &block)
41
- if @mocked_object.present?
42
- @mocked_object.send(method_name, *arguments, &block)
43
- else
44
- super(method_name, *arguments, &block)
45
- end
46
- end
47
-
48
- def respond_to?(method_name, include_private = false)
49
- if @mocked_object.present?
50
- @mocked_object.respond_to?(:method) || super
51
- else
52
- super
53
- end
54
- end
55
-
56
- private
57
- def parent_class
58
- if self.class.respond_to?(:parent_class)
59
- self.class.parent_class
60
- else
61
- nil
62
- end
63
- end
64
- end
65
- end
@@ -1,135 +0,0 @@
1
- require 'spec_helper'
2
-
3
- class DummyPersonParams < SimpleParams::Params
4
- param :name
5
- param :age, type: :integer
6
- nested_hash :address do
7
- param :street
8
- param :city
9
- end
10
- nested_array :phones do
11
- param :phone_number
12
- end
13
- end
14
-
15
- class DummyDogParams < SimpleParams::Params
16
- param :name
17
- param :age, type: :integer
18
- nested_hash :owners_address do
19
- param :street
20
- param :city
21
- end
22
- nested_array :puppies do
23
- param :name
24
- param :age
25
- end
26
- end
27
-
28
- describe SimpleParams::NilParams do
29
- describe "class methods", class_methods: true do
30
- describe "define_nil_class" do
31
- let(:defined_class) do
32
- described_class.define_nil_class(DummyPersonParams)
33
- end
34
-
35
- it "has correct name" do
36
- defined_class.name.should eq("DummyPersonParams::NilParams")
37
- end
38
-
39
- it "has correct parent class" do
40
- defined_class.parent_class.should eq(DummyPersonParams)
41
- end
42
-
43
- it "has correct nested_classes" do
44
- defined_class.nested_classes.should eq({
45
- address: DummyPersonParams::Address,
46
- phones: DummyPersonParams::Phones
47
- })
48
- end
49
- end
50
- end
51
-
52
- describe "public methods", public_methods: true do
53
- context "with no parent_class" do
54
- let!(:instance) { described_class.new({})}
55
-
56
- describe "#valid" do
57
- it "is valid" do
58
- instance.should be_valid
59
- end
60
- end
61
-
62
- describe "#errors" do
63
- it "has no errors" do
64
- instance.errors.should be_empty
65
- end
66
-
67
- it "can clear errors" do
68
- instance.valid?
69
- instance.errors.clear.should eq([])
70
- end
71
-
72
- it "has no error messages" do
73
- instance.valid?
74
- instance.errors.messages.should be_empty
75
- end
76
- end
77
-
78
- describe "#to_hash" do
79
- it "is empty" do
80
- instance.to_hash.should be_empty
81
- end
82
- end
83
-
84
- describe "#mocked_params accessors" do
85
- it "does not respond to random accessors" do
86
- instance.should_not respond_to(:name)
87
- instance.should_not respond_to(:age)
88
- end
89
- end
90
- end
91
-
92
- context "with parent class" do
93
- let!(:instance) { described_class.define_nil_class(DummyPersonParams).new({})}
94
-
95
- describe "#valid" do
96
- it "is valid" do
97
- instance.should be_valid
98
- end
99
- end
100
-
101
- describe "#errors" do
102
- it "has no errors" do
103
- instance.errors.should be_empty
104
- end
105
-
106
- it "can clear errors" do
107
- instance.valid?
108
- instance.errors.clear.should eq([])
109
- end
110
-
111
- it "has no error messages" do
112
- instance.valid?
113
- instance.errors.messages.should be_empty
114
- end
115
- end
116
-
117
- describe "#to_hash" do
118
- it "is empty" do
119
- instance.to_hash.should be_empty
120
- end
121
- end
122
-
123
- describe "#mocked_params accessors" do
124
- it "responds to parent class instance's accessors" do
125
- instance.name.should be_nil
126
- instance.age.should be_nil
127
- instance.name = "Amy"
128
- instance.age = 33
129
- instance.name.should eq("Amy")
130
- instance.age.should eq(33)
131
- end
132
- end
133
- end
134
- end
135
- end