attributor 5.2.0 → 5.6

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.
Files changed (53) hide show
  1. checksums.yaml +5 -5
  2. data/.travis.yml +4 -3
  3. data/CHANGELOG.md +146 -140
  4. data/attributor.gemspec +4 -5
  5. data/lib/attributor.rb +16 -2
  6. data/lib/attributor/attribute.rb +42 -9
  7. data/lib/attributor/extras/field_selector.rb +4 -0
  8. data/lib/attributor/families/numeric.rb +19 -6
  9. data/lib/attributor/families/temporal.rb +16 -9
  10. data/lib/attributor/hash_dsl_compiler.rb +6 -6
  11. data/lib/attributor/type.rb +27 -4
  12. data/lib/attributor/types/bigdecimal.rb +7 -2
  13. data/lib/attributor/types/boolean.rb +7 -2
  14. data/lib/attributor/types/class.rb +2 -2
  15. data/lib/attributor/types/collection.rb +24 -5
  16. data/lib/attributor/types/container.rb +3 -3
  17. data/lib/attributor/types/csv.rb +5 -1
  18. data/lib/attributor/types/date.rb +9 -3
  19. data/lib/attributor/types/date_time.rb +8 -2
  20. data/lib/attributor/types/float.rb +4 -3
  21. data/lib/attributor/types/hash.rb +82 -18
  22. data/lib/attributor/types/integer.rb +7 -1
  23. data/lib/attributor/types/model.rb +2 -2
  24. data/lib/attributor/types/object.rb +5 -0
  25. data/lib/attributor/types/polymorphic.rb +3 -2
  26. data/lib/attributor/types/string.rb +20 -1
  27. data/lib/attributor/types/struct.rb +1 -1
  28. data/lib/attributor/types/symbol.rb +5 -0
  29. data/lib/attributor/types/tempfile.rb +4 -0
  30. data/lib/attributor/types/time.rb +7 -3
  31. data/lib/attributor/types/uri.rb +9 -1
  32. data/lib/attributor/version.rb +1 -1
  33. data/spec/attribute_spec.rb +42 -7
  34. data/spec/dsl_compiler_spec.rb +7 -7
  35. data/spec/extras/field_selector/field_selector_spec.rb +9 -0
  36. data/spec/hash_dsl_compiler_spec.rb +2 -2
  37. data/spec/support/integers.rb +7 -0
  38. data/spec/type_spec.rb +1 -1
  39. data/spec/types/bigdecimal_spec.rb +8 -0
  40. data/spec/types/boolean_spec.rb +10 -0
  41. data/spec/types/class_spec.rb +0 -1
  42. data/spec/types/collection_spec.rb +16 -0
  43. data/spec/types/date_spec.rb +9 -0
  44. data/spec/types/date_time_spec.rb +9 -0
  45. data/spec/types/float_spec.rb +8 -0
  46. data/spec/types/hash_spec.rb +127 -9
  47. data/spec/types/integer_spec.rb +10 -1
  48. data/spec/types/model_spec.rb +14 -3
  49. data/spec/types/string_spec.rb +10 -0
  50. data/spec/types/temporal_spec.rb +5 -1
  51. data/spec/types/time_spec.rb +9 -0
  52. data/spec/types/uri_spec.rb +9 -0
  53. metadata +21 -34
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: f5472909b262b07126ca30d760aadb3f5bc0d362
4
- data.tar.gz: 9e66f91088af8389b60d088dd4d7599acfd74d9a
2
+ SHA256:
3
+ metadata.gz: 95e7a4d30473d5e7e9dbd37f0e028b9b5dd5b17cf0ceb89593f0aed60dab20de
4
+ data.tar.gz: 59cc3458cd723c43733ea6a128fb4032c8aa8c7cd2ac41ea620c31842da35db6
5
5
  SHA512:
6
- metadata.gz: a7c963afb8460167b7714ae4dd4ae74f273ef703b11059e354f33f2356adfba3e9433dad3bc4497612beacfcaf67c727249a6e0143b5e45a0219b95e9c61ace0
7
- data.tar.gz: ef1e2720295ee7456b08cd2c003e044f686958cb4e6ef3738f617d042c1ed2da0f1180e1cc5e11d0b97e0b74046eeead54f4a5b8f6bd843d4e092ab6662baa2b
6
+ metadata.gz: 10da45417633b8f159db72d33a81101f25bd90cf8282e3a10e80d3cfb92de90639642dcf51d12d8f8ce4237acd1e2e4e562c97173480b1525cb4914f237d8ce4
7
+ data.tar.gz: e5c2ebc2dd2a214ffb4254332e56366bd825f1dbc93499d26476ed73226dc0e698be75918dab0a5d9853550ba8c88c83ba02afeeae22885254f828d2ac52040e
@@ -1,11 +1,12 @@
1
1
  sudo: false
2
2
  language: ruby
3
3
  rvm:
4
- - 2.2.5
5
- - 2.3.1
4
+ - 2.4
5
+ - 2.5
6
+ - 2.6
7
+ - 2.7
6
8
  script:
7
9
  - bundle exec rspec spec
8
- - bundle exec rubocop --format=clang
9
10
  branches:
10
11
  only:
11
12
  - master
@@ -2,46 +2,61 @@
2
2
 
3
3
  ## next
4
4
 
5
- ## 5.2 (12/09/2017)
5
+ ## 5.6 (11/02/2020)
6
+
7
+ - Small fixes for dumping JSON-schema default values if they're Proc's or dumpable objects
8
+
9
+ ## 5.5 (21/08/2020)
10
+
11
+ - JSON-schema support. Enhanced all of the types to suppport describing themselves as JSON-schema format (`.as_json_schema`). This description includes a few `x-*` extensions to capture some information that Attributor supports, that JSON-schema does not.
12
+
13
+ ## 5.3 (24/04/2020)
6
14
 
7
- * Fixed describing `Hash` with no keys defined, to still use a given example (no example outputted before this)
8
- * Fix bug that would occur when defining an attribute carrying a reference object, for which the reference type didn't have `attributes` (for example a Collection).
9
- * Allows an attribute to override the reference object through its options of its parent (even when its containing object already has one defined).
10
- * Built code to generate examples in a smarter way when complex conditional requirements are specified.
15
+ - Fixed deprecation warnings in Ruby 2.7
11
16
 
17
+ ## 5.2.1 (25/08/2019)
18
+
19
+ - Propertly pass options down when dumping an Model
20
+
21
+ ## 5.2 (12/09/2017)
22
+
23
+ - Fixed describing `Hash` with no keys defined, to still use a given example (no example outputted before this)
24
+ - Fix bug that would occur when defining an attribute carrying a reference object, for which the reference type didn't have `attributes` (for example a Collection).
25
+ - Allows an attribute to override the reference object through its options of its parent (even when its containing object already has one defined).
26
+ - Built code to generate examples in a smarter way when complex conditional requirements are specified.
12
27
 
13
28
  ## 5.1
14
29
 
15
- * Added `Polymorphic` type. See [polymorphics.rb](spec/support/polymorphics.rb) for example usage.
30
+ - Added `Polymorphic` type. See [polymorphics.rb](spec/support/polymorphics.rb) for example usage.
16
31
 
17
32
  ## 5.0.2
18
33
 
19
- * Introduce the `Dumpable` (empty) module as an interface to indicate that instances of types that include it
20
- will respond to the `.dump` method, as a way to convert their internal substructure to primitive Ruby objects.
21
- * Currently the only two directly dumpable types are Collection and Hash (with the caveat that there are several others that derive from them..i.e., CSV, Model, etc...)
22
- * The rest of types have `native_types` that are already Ruby primitive Objects.
23
- * Fixed Hash and Model requirements to treat nil values as missing keys (to be compatible with the `required: true` option on an attribute).
34
+ - Introduce the `Dumpable` (empty) module as an interface to indicate that instances of types that include it
35
+ will respond to the `.dump` method, as a way to convert their internal substructure to primitive Ruby objects.
36
+ - Currently the only two directly dumpable types are Collection and Hash (with the caveat that there are several others that derive from them..i.e., CSV, Model, etc...)
37
+ - The rest of types have `native_types` that are already Ruby primitive Objects.
38
+ - Fixed Hash and Model requirements to treat nil values as missing keys (to be compatible with the `required: true` option on an attribute).
24
39
 
25
40
  ## 5.0.1
26
41
 
27
- * Fix bug that made Struct/Models skip validation of requirements using the `requires` DSL
42
+ - Fix bug that made Struct/Models skip validation of requirements using the `requires` DSL
28
43
 
29
44
  ## 5.0
30
45
 
31
- * Changed `FieldSelector` sub-attribute selection to use `{}` instead of `()`.
32
-
46
+ - Changed `FieldSelector` sub-attribute selection to use `{}` instead of `()`.
33
47
 
34
48
  ## 4.2.0
35
49
 
36
- * Added an "anonymous" DSL for base `Attributor::Type` which is reported in its `.describe` call.
37
- * This is a simple documentation bit, that might help the clients to document the type properly (i.e. treat it as if the type was anonymously defined whenever is used, rather than reachable by id/name from anywhere)
50
+ - Added an "anonymous" DSL for base `Attributor::Type` which is reported in its `.describe` call.
38
51
 
39
- * Built advanced attribute requirements for `Struct`,`Model` and `Hash` types. Those requirements allow you to define things like:
40
- * A list of attributes that are required (equivalent to defining the required: true bit at each of the attributes)
41
- * At most (n) attributes from a group can be passed in
42
- * At least (n) attributes from a group are required
43
- * Exactly (n) attributes from a group are required
44
- * Example:
52
+ - This is a simple documentation bit, that might help the clients to document the type properly (i.e. treat it as if the type was anonymously defined whenever is used, rather than reachable by id/name from anywhere)
53
+
54
+ - Built advanced attribute requirements for `Struct`,`Model` and `Hash` types. Those requirements allow you to define things like:
55
+ - A list of attributes that are required (equivalent to defining the required: true bit at each of the attributes)
56
+ - At most (n) attributes from a group can be passed in
57
+ - At least (n) attributes from a group are required
58
+ - Exactly (n) attributes from a group are required
59
+ - Example:
45
60
  ```
46
61
  requires ‘id’, ‘name’
47
62
  requires.all ‘id’, ‘name’ # Equivalent to above
@@ -50,7 +65,7 @@ will respond to the `.dump` method, as a way to convert their internal substruct
50
65
  requires.at_least(1).of ‘rock’, ‘pop’
51
66
  requires.exactly(2).of ‘one’, ‘two’, ’three’
52
67
  ```
53
- * Same example expressed inside a block if so desired
68
+ - Same example expressed inside a block if so desired
54
69
  ```
55
70
  requires do
56
71
  all 'id', 'name
@@ -62,167 +77,158 @@ will respond to the `.dump` method, as a way to convert their internal substruct
62
77
 
63
78
  ## 4.1.0
64
79
 
65
- * Added a `Class` type (useful to avoid demodulization coercions etc...)
66
- * Added `Attributor::FieldSelector` type for parsing hierarchical field
80
+ - Added a `Class` type (useful to avoid demodulization coercions etc...)
81
+ - Added `Attributor::FieldSelector` type for parsing hierarchical field
67
82
  selection hashes from a string. This is similar to the partial `fields`
68
83
  parameter in Google APIs, or the `fields` parameter in the Facebook's Graph
69
84
  API.
70
- * For example: the string `'one,two(a,b)'` would select two top-level fields
71
- named 'one' and 'two', retrieving the entire contents of 'one', and only
72
- the 'a' and 'b' sub-fields for 'two'. The type will parse the above string
73
- into the hash: `{one: true, two: {a: true, b: true}}`.
74
- * This type is not automatically required by Attributor. To require it use:
75
- `require 'attributor/extras/field_selector'.
76
- * This type also depends upon the 'parslet' gem.
85
+ - For example: the string `'one,two(a,b)'` would select two top-level fields
86
+ named 'one' and 'two', retrieving the entire contents of 'one', and only
87
+ the 'a' and 'b' sub-fields for 'two'. The type will parse the above string
88
+ into the hash: `{one: true, two: {a: true, b: true}}`.
89
+ - This type is not automatically required by Attributor. To require it use:
90
+ `require 'attributor/extras/field_selector'.
91
+ - This type also depends upon the 'parslet' gem.
77
92
 
78
93
  ## 4.0.1
79
94
 
80
- * `Attribute#check_option!` now calls `load` on any provided value.
81
-
95
+ - `Attribute#check_option!` now calls `load` on any provided value.
82
96
 
83
97
  ## 4.0.0
84
98
 
85
- * Changed the expectation of the value for an `:example` option of an attribute:
86
- * Before, passing an array of values would indicate that those were a few possible examples for it.
87
- * Now, any value (except the already existing special regexp or a proc) for an example will need to be of a native type (or coercible to it). This means that an attribute of type `Collection` can take an array example (and be taken as the whole thing)
88
- * If anybody wants to provide multiple examples for an attribute they can write a proc, and make it return the different ones.
99
+ - Changed the expectation of the value for an `:example` option of an attribute:
100
+ - Before, passing an array of values would indicate that those were a few possible examples for it.
101
+ - Now, any value (except the already existing special regexp or a proc) for an example will need to be of a native type (or coercible to it). This means that an attribute of type `Collection` can take an array example (and be taken as the whole thing)
102
+ - If anybody wants to provide multiple examples for an attribute they can write a proc, and make it return the different ones.
89
103
 
90
104
  ## 3.0.1
91
105
 
92
- * Fixed bug with example Hashes where `[]` with a key not in the hash would throw a `NoMethodError`.
93
- * Fixed bug in `Hash#get` for Hashes without predefined keys. It would throw an error if given a key not present in the hash's contents.
94
-
106
+ - Fixed bug with example Hashes where `[]` with a key not in the hash would throw a `NoMethodError`.
107
+ - Fixed bug in `Hash#get` for Hashes without predefined keys. It would throw an error if given a key not present in the hash's contents.
95
108
 
96
109
  ## 3.0.0
97
110
 
98
- * Small enhancements on `describe` for types
99
- * avoid creating empty `:attributes` key for `Model`
100
- * ensure embedding `key_type` in `Hash` using `shallow` mode
101
- * Added `Hash#delete`.
102
- * Changed the schema for describing `Hash` to use `attributes` instead of `keys`
103
- * It makes more sense, and it is compatible with Model and Structs too.
104
- * Undefine JRuby package helper methods in `Model` (org, java...)
105
- * Added support to `Collection.load` for any value that responds to `to_a`
106
- * Fixed `Collection.validate` to complain when value object is not a valida type
107
- * Fixed bug where defining an attribute that references a `Collection` would not properly support defining sub-attributes in a provided block.
108
- * Enhanced the type/attribute `describe` methods of types so that they generate an example if an `example` argument is passed in.
109
- * Complex (sub-structured) types will not output examples, only 'leaf' ones.
110
- * Improved handling of exceptions during attribute definitions for `Hash`/`Model` that would previously leave the set of attributes in an undefined state. Now, any attempts to use the type will throw an `InvalidDefinition` exception and include the original exception. (#127)
111
- * Removed `undef :empty?` from `Model`
112
- * Made `Collection` a subclass of Array, and `load` create new instances of it.
113
- * Built in proper loading and validation of any `Attribute#example` when the `:example` option is used.
114
-
111
+ - Small enhancements on `describe` for types
112
+ - avoid creating empty `:attributes` key for `Model`
113
+ - ensure embedding `key_type` in `Hash` using `shallow` mode
114
+ - Added `Hash#delete`.
115
+ - Changed the schema for describing `Hash` to use `attributes` instead of `keys`
116
+ - It makes more sense, and it is compatible with Model and Structs too.
117
+ - Undefine JRuby package helper methods in `Model` (org, java...)
118
+ - Added support to `Collection.load` for any value that responds to `to_a`
119
+ - Fixed `Collection.validate` to complain when value object is not a valida type
120
+ - Fixed bug where defining an attribute that references a `Collection` would not properly support defining sub-attributes in a provided block.
121
+ - Enhanced the type/attribute `describe` methods of types so that they generate an example if an `example` argument is passed in.
122
+ - Complex (sub-structured) types will not output examples, only 'leaf' ones.
123
+ - Improved handling of exceptions during attribute definitions for `Hash`/`Model` that would previously leave the set of attributes in an undefined state. Now, any attempts to use the type will throw an `InvalidDefinition` exception and include the original exception. (#127)
124
+ - Removed `undef :empty?` from `Model`
125
+ - Made `Collection` a subclass of Array, and `load` create new instances of it.
126
+ - Built in proper loading and validation of any `Attribute#example` when the `:example` option is used.
115
127
 
116
128
  ## 2.6.1
117
129
 
118
- * Add the `:custom_data` option for attributes. This is a hash that is passed through to `describe` - Attributor does no processing or handling of this option.
119
- * Added `Type.family` which returns a more-generic "family name". It's defined for all built-in types, and is included in `Type.describe`.
120
- * Cleanup and bug fixes around example generation for `Model`, `Struct` and `Hash`.
121
- * Avoid creating method accessors for true `Hash` types (only `[]` accessors)
122
- * Fix common hash methods created for example instances (to play well with lazy attributes)
123
- * Avoid storing the `Hash#insensitive_map` unless insensitivity enabled
130
+ - Add the `:custom_data` option for attributes. This is a hash that is passed through to `describe` - Attributor does no processing or handling of this option.
131
+ - Added `Type.family` which returns a more-generic "family name". It's defined for all built-in types, and is included in `Type.describe`.
132
+ - Cleanup and bug fixes around example generation for `Model`, `Struct` and `Hash`.
133
+ - Avoid creating method accessors for true `Hash` types (only `[]` accessors)
134
+ - Fix common hash methods created for example instances (to play well with lazy attributes)
135
+ - Avoid storing the `Hash#insensitive_map` unless insensitivity enabled
124
136
 
125
137
  ## 2.6.0
126
138
 
127
- * Fixed bug in `example_mixin` where lazy_attributes were not evaluated.
128
- * Fixed bug in `Hash` where the class would refuse to load from another `Attributor::Hash` when there were no keys defined and they were seemingly compatible.
129
- * Fixed a `Hash.dump` bug where nil attribute values would transitively be `dumpe`d therefore causing a nil dereference.
130
- * Hardened the `dump`ing of types to support nil values.
131
- * Fix `attribute.example` to actually accept native types (that are not only Strings)
132
- * Fixed bug where `Hash#get` would insert a nil value if asked for a key that was not present in the hash.
133
- * Fixed bug in `Hash.from_hash` where it would add nil values for keys that are defined on the type but not present in the input.
134
- * Added `Hash#merge` that works with two identically-typed hashes
135
- * Added `Hash#each_pair` for better duck-type compatibility with ::Hash.
136
-
139
+ - Fixed bug in `example_mixin` where lazy_attributes were not evaluated.
140
+ - Fixed bug in `Hash` where the class would refuse to load from another `Attributor::Hash` when there were no keys defined and they were seemingly compatible.
141
+ - Fixed a `Hash.dump` bug where nil attribute values would transitively be `dumpe`d therefore causing a nil dereference.
142
+ - Hardened the `dump`ing of types to support nil values.
143
+ - Fix `attribute.example` to actually accept native types (that are not only Strings)
144
+ - Fixed bug where `Hash#get` would insert a nil value if asked for a key that was not present in the hash.
145
+ - Fixed bug in `Hash.from_hash` where it would add nil values for keys that are defined on the type but not present in the input.
146
+ - Added `Hash#merge` that works with two identically-typed hashes
147
+ - Added `Hash#each_pair` for better duck-type compatibility with ::Hash.
137
148
 
138
149
  ## 2.5.0
139
150
 
140
- * Partial support for defining `:default` values through Procs.
141
- * Note: this is only "partially" supported the `parent` argument of the Proc will NOT contain the correct attribute parent yet. It will contain a fake class, that will loudly complain about any attempt to use any of its methods.
142
- * Fixed `Model.example` to properly handle the case when no attributes are defined on the class.
143
- * `Model#dump` now issues a warning if its contents have keys for attributes not present on the class. The unknown contents are not dumped.
144
- * `Hash.load` now supports loading any value that responds to `to_hash`.
145
- * `Time`, `DateTime`, and `Date` now all return ISO 8601 formatted values from `.dump` (via calling `iso8601` on the value).
146
- * Added `Type.id`, a unique value based on the type's class name.
147
-
151
+ - Partial support for defining `:default` values through Procs.
152
+ - Note: this is only "partially" supported the `parent` argument of the Proc will NOT contain the correct attribute parent yet. It will contain a fake class, that will loudly complain about any attempt to use any of its methods.
153
+ - Fixed `Model.example` to properly handle the case when no attributes are defined on the class.
154
+ - `Model#dump` now issues a warning if its contents have keys for attributes not present on the class. The unknown contents are not dumped.
155
+ - `Hash.load` now supports loading any value that responds to `to_hash`.
156
+ - `Time`, `DateTime`, and `Date` now all return ISO 8601 formatted values from `.dump` (via calling `iso8601` on the value).
157
+ - Added `Type.id`, a unique value based on the type's class name.
148
158
 
149
159
  ## 2.4.0
150
160
 
151
- * `Model` is now a subclass of `Hash`.
152
- * The interface for `Model` instances is almost entirely unchanged, except for the addition of `Hash`-like methods (i.e., you can now do `some_model[:key]` to access attributes).
153
- * This fixes numerous incompatabilities between models and hashes, as well as confusing differences between the behavior when loading a model vs a hash.
154
- * `String.load` now raises `IncompatibleTypeError` for `Enumerable` values.
155
- * Added `Symbol` type, use with caution as it will automatically call `#to_sym` on anything loaded.
161
+ - `Model` is now a subclass of `Hash`.
162
+ - The interface for `Model` instances is almost entirely unchanged, except for the addition of `Hash`-like methods (i.e., you can now do `some_model[:key]` to access attributes).
163
+ - This fixes numerous incompatabilities between models and hashes, as well as confusing differences between the behavior when loading a model vs a hash.
164
+ - `String.load` now raises `IncompatibleTypeError` for `Enumerable` values.
165
+ - Added `Symbol` type, use with caution as it will automatically call `#to_sym` on anything loaded.
156
166
 
157
167
  ## 2.3.0
158
168
 
159
- * Added `recurse` option to `Type.load` that is used by `Model` and `Hash` to force the loading of values (specifically, so that default values are assigned) even if the loaded value is `nil`.
160
- * Fix `Attributor::CSV` to dump `String` values and generate `String` examples.
161
- * Default values of `false` now work correctly.
162
- * Added `BigDecimal`, `Date` and `Time` types
163
- * `DateTime.load` now raises `CoercionError` (instead of returning `nil`) if given values that can not coerced properly.
164
- * `Hash.dump` now first calls `Hash.load`, and correctly uses defined value types for dumping.
165
- * Added `Hash#get`, for retrieving keys using the same logic the `case_insensitive_load` and `allow_extra` with defined `extra` key.
166
-
169
+ - Added `recurse` option to `Type.load` that is used by `Model` and `Hash` to force the loading of values (specifically, so that default values are assigned) even if the loaded value is `nil`.
170
+ - Fix `Attributor::CSV` to dump `String` values and generate `String` examples.
171
+ - Default values of `false` now work correctly.
172
+ - Added `BigDecimal`, `Date` and `Time` types
173
+ - `DateTime.load` now raises `CoercionError` (instead of returning `nil`) if given values that can not coerced properly.
174
+ - `Hash.dump` now first calls `Hash.load`, and correctly uses defined value types for dumping.
175
+ - Added `Hash#get`, for retrieving keys using the same logic the `case_insensitive_load` and `allow_extra` with defined `extra` key.
167
176
 
168
177
  ## 2.2.1
169
178
 
170
- * Dumping attributes will now load the values if they're not in the native type.
171
- * `Model.valid_type?` now accepts hashes.
172
- * `Hash`:
173
- * Added `:has_key?` to delegation
174
-
179
+ - Dumping attributes will now load the values if they're not in the native type.
180
+ - `Model.valid_type?` now accepts hashes.
181
+ - `Hash`:
182
+ - Added `:has_key?` to delegation
175
183
 
176
184
  ## 2.2.0
177
185
 
178
- * Fix example generation for Hash and Collection to handle a non-Array context parameter.
179
- * Hash:
180
- * Added additional options:
181
- * `:case_insensitive_load` for string-keyed hashes. This allows loading hashes with keys that do not exactly match the case defined in the hash.
182
- * Added `:allow_extras` option to allow handling of undefined keys when loading.
183
- * Added `Hash#set` to encapsulate the above options and attribute loading.
184
- * Added `extra` command in the `keys` DSL, which lets you define a key (whose value should be a Hash), to group any unspecified keys during load.
185
-
186
+ - Fix example generation for Hash and Collection to handle a non-Array context parameter.
187
+ - Hash:
188
+ - Added additional options:
189
+ - `:case_insensitive_load` for string-keyed hashes. This allows loading hashes with keys that do not exactly match the case defined in the hash.
190
+ - Added `:allow_extras` option to allow handling of undefined keys when loading.
191
+ - Added `Hash#set` to encapsulate the above options and attribute loading.
192
+ - Added `extra` command in the `keys` DSL, which lets you define a key (whose value should be a Hash), to group any unspecified keys during load.
186
193
 
187
194
  ## 2.1.0
188
195
 
189
- * Structs now inherit type-level options from their reference.
190
- * Add Collection subclasses for CSVs and Ids
191
- * CSV type for Collection of values serialized as comma-separated strings.
192
- * Ids type. A helper for creating CSVs with members matching a given a type's :identity option.
193
- * Allow instances of Models to be initialized with initial data.
194
- * Supported formats for the data are equivalent to the loading formats (i.e. ruby Hash, a JSON string or another instance of the same model type).
195
- * Improved context reporting in errors
196
- * Added contextual information while loading and dumping attributes.
197
- * `load` takes a new `context` argument (defaulting to a system-wide root) in the form of an array of parent segments.
198
- * `validate` takes a `context` argument that (instead of a string) is now an array of parent segments.
199
- * `dump` takes a `context:` option parameter of the same type
200
- * Enhanced error messages to report the correct context scope.
201
- * Make Attribute assignments in models to report a special context (not the attributor root)
202
- * Instead of reporting "$." as the context , when doing model.field_name=value, they'll now report "assignment.of(field_name)" instead
203
- * Truncate the length of values when reporting loading errors when they're long (i.e. >500 chars)
204
- * `Model.attributes` may now be called more than once to set add or replace attributes. The exact behavior depends upon the types of the attributes being added or replaced. See [model_spec.rb](spec/types/model_spec.rb) for examples.
205
- * Greately enhanced Hash type with individual key specification (rather than
196
+ - Structs now inherit type-level options from their reference.
197
+ - Add Collection subclasses for CSVs and Ids
198
+ - CSV type for Collection of values serialized as comma-separated strings.
199
+ - Ids type. A helper for creating CSVs with members matching a given a type's :identity option.
200
+ - Allow instances of Models to be initialized with initial data.
201
+ - Supported formats for the data are equivalent to the loading formats (i.e. ruby Hash, a JSON string or another instance of the same model type).
202
+ - Improved context reporting in errors
203
+ - Added contextual information while loading and dumping attributes.
204
+ - `load` takes a new `context` argument (defaulting to a system-wide root) in the form of an array of parent segments.
205
+ - `validate` takes a `context` argument that (instead of a string) is now an array of parent segments.
206
+ - `dump` takes a `context:` option parameter of the same type
207
+ - Enhanced error messages to report the correct context scope.
208
+ - Make Attribute assignments in models to report a special context (not the attributor root)
209
+ - Instead of reporting "\$." as the context , when doing model.field_name=value, they'll now report "assignment.of(field_name)" instead
210
+ - Truncate the length of values when reporting loading errors when they're long (i.e. >500 chars)
211
+ - `Model.attributes` may now be called more than once to set add or replace attributes. The exact behavior depends upon the types of the attributes being added or replaced. See [model_spec.rb](spec/types/model_spec.rb) for examples.
212
+ - Greately enhanced Hash type with individual key specification (rather than
206
213
  simply defining the types of keys)
207
- * Loaded Hash types now return instances of the class rather than a simple Ruby Hash.
208
- * Introduced a new FileUpload type. This can be easily used in Web servers to map incoming multipart file uploads.
209
- * Introduced a new Tempfile type.
210
-
214
+ - Loaded Hash types now return instances of the class rather than a simple Ruby Hash.
215
+ - Introduced a new FileUpload type. This can be easily used in Web servers to map incoming multipart file uploads.
216
+ - Introduced a new Tempfile type.
211
217
 
212
218
  ## 2.0.0
213
219
 
214
- * Added new exception subtypes (load methods return more precise errors now)
215
- * Changed ```Attributor::Model``` to be a class instead of module.
216
- * Improved handling of ```Attributor::Model``` examples:
217
- * Support creating examples with specific values. i.e.:
220
+ - Added new exception subtypes (load methods return more precise errors now)
221
+ - Changed `Attributor::Model` to be a class instead of module.
222
+ - Improved handling of `Attributor::Model` examples:
223
+ - Support creating examples with specific values. i.e.:
218
224
  ```ruby
219
225
  person = Person.example(name: "Bob")
220
226
  person.name # => "Bob"
221
227
  ```
222
- * Example values are now lazily initialized when used.
223
- * Terminate sub-attribute generation after ```Attributor::Model::MAX_EXAMPLE_DEPTH``` levels to prevent infinite generation.
224
- * Added additional options for Attribute :example values:
225
- * explicit nil values
226
- * procs that take 2 arguments now receive the context as the second argument.
227
- * Circular references are now detected and handled in validation and dumping.
228
- * Fixed bug with Model attribute accessors when using false values.
228
+ - Example values are now lazily initialized when used.
229
+ - Terminate sub-attribute generation after `Attributor::Model::MAX_EXAMPLE_DEPTH` levels to prevent infinite generation.
230
+ - Added additional options for Attribute :example values:
231
+ - explicit nil values
232
+ - procs that take 2 arguments now receive the context as the second argument.
233
+ - Circular references are now detected and handled in validation and dumping.
234
+ - Fixed bug with Model attribute accessors when using false values.
@@ -25,19 +25,18 @@ Gem::Specification.new do |spec|
25
25
 
26
26
  spec.add_development_dependency 'rspec', '~> 3'
27
27
  spec.add_development_dependency 'rspec-its'
28
- spec.add_development_dependency 'rspec-collection_matchers', '~> 1'
28
+ spec.add_development_dependency 'rspec-collection_matchers'
29
29
  spec.add_development_dependency('yard')
30
- spec.add_development_dependency('backports', ['~> 3'])
31
30
  spec.add_development_dependency('yardstick', ['~> 0'])
32
31
  spec.add_development_dependency('bundler', ['>= 0'])
33
32
  spec.add_development_dependency('rake-notes', ['~> 0'])
34
33
  spec.add_development_dependency('coveralls')
35
34
  spec.add_development_dependency('guard', ['~> 2'])
36
35
  spec.add_development_dependency('guard-rspec', ['~> 4'])
37
- spec.add_development_dependency('pry', ['~> 0'])
36
+ spec.add_development_dependency('pry')
38
37
  if RUBY_PLATFORM !~ /java/
39
- spec.add_development_dependency('pry-byebug', ['~> 1'])
40
- spec.add_development_dependency('pry-stack_explorer', ['~> 0'])
38
+ spec.add_development_dependency('pry-byebug')
39
+ spec.add_development_dependency('pry-stack_explorer')
41
40
  end
42
41
  spec.add_development_dependency 'fuubar'
43
42
  spec.add_development_dependency 'rubocop'
@@ -2,7 +2,7 @@ require 'json'
2
2
  require 'randexp'
3
3
 
4
4
  require 'hashie'
5
-
5
+ require 'active_support/concern'
6
6
  require 'digest/sha1'
7
7
 
8
8
  module Attributor
@@ -29,7 +29,7 @@ module Attributor
29
29
  def self.resolve_type(attr_type, options = {}, constructor_block = nil)
30
30
  klass = self.find_type(attr_type)
31
31
 
32
- return klass.construct(constructor_block, options) if klass.constructable?
32
+ return klass.construct(constructor_block, **options) if klass.constructable?
33
33
  raise AttributorException, "Type: #{attr_type} does not support anonymous generation" if constructor_block
34
34
 
35
35
  klass
@@ -73,6 +73,20 @@ module Attributor
73
73
  inspection
74
74
  end
75
75
 
76
+ def self.recursive_to_h(val)
77
+ if val.is_a? Array
78
+ val.map { |v| recursive_to_h(v) }
79
+ elsif val.nil?
80
+ nil
81
+ elsif val.respond_to?(:to_h)
82
+ val.to_h.each_with_object({}) do |(name, inner_val), hash|
83
+ hash[name] = recursive_to_h(inner_val)
84
+ end
85
+ else
86
+ val
87
+ end
88
+ end
89
+
76
90
  MODULE_PREFIX = 'Attributor::'.freeze
77
91
  MODULE_PREFIX_REGEX = ::Regexp.new(MODULE_PREFIX)
78
92