schemacop 2.3.2 → 2.4.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.
Files changed (69) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +15 -0
  3. data/README.md +190 -2
  4. data/RUBY_VERSION +1 -1
  5. data/Rakefile +6 -5
  6. data/VERSION +1 -1
  7. data/doc/Schemacop.html +30 -3
  8. data/doc/Schemacop/ArrayValidator.html +2 -2
  9. data/doc/Schemacop/BooleanValidator.html +1 -1
  10. data/doc/Schemacop/Caster.html +379 -0
  11. data/doc/Schemacop/Collector.html +178 -102
  12. data/doc/Schemacop/Exceptions.html +1 -1
  13. data/doc/Schemacop/Exceptions/InvalidSchemaError.html +1 -1
  14. data/doc/Schemacop/Exceptions/ValidationError.html +1 -1
  15. data/doc/Schemacop/FieldNode.html +17 -5
  16. data/doc/Schemacop/FloatValidator.html +1 -1
  17. data/doc/Schemacop/HashValidator.html +1 -1
  18. data/doc/Schemacop/IntegerValidator.html +1 -1
  19. data/doc/Schemacop/NilValidator.html +1 -1
  20. data/doc/Schemacop/Node.html +95 -83
  21. data/doc/Schemacop/NodeResolver.html +26 -10
  22. data/doc/Schemacop/NodeSupportingField.html +1 -1
  23. data/doc/Schemacop/NodeSupportingType.html +2 -4
  24. data/doc/Schemacop/NodeWithBlock.html +1 -1
  25. data/doc/Schemacop/NumberValidator.html +1 -1
  26. data/doc/Schemacop/ObjectValidator.html +1 -1
  27. data/doc/Schemacop/RootNode.html +1 -1
  28. data/doc/Schemacop/Schema.html +3 -3
  29. data/doc/Schemacop/StringValidator.html +1 -1
  30. data/doc/Schemacop/SymbolValidator.html +1 -1
  31. data/doc/ScopedEnv.html +1 -1
  32. data/doc/_index.html +8 -1
  33. data/doc/class_list.html +1 -1
  34. data/doc/file.README.html +184 -3
  35. data/doc/index.html +184 -3
  36. data/doc/method_list.html +93 -61
  37. data/doc/top-level-namespace.html +1 -1
  38. data/lib/schemacop.rb +14 -0
  39. data/lib/schemacop/caster.rb +38 -0
  40. data/lib/schemacop/collector.rb +34 -6
  41. data/lib/schemacop/field_node.rb +25 -3
  42. data/lib/schemacop/node.rb +10 -2
  43. data/lib/schemacop/node_resolver.rb +10 -2
  44. data/lib/schemacop/node_supporting_type.rb +21 -1
  45. data/lib/schemacop/schema.rb +2 -2
  46. data/lib/schemacop/validator/array_validator.rb +1 -1
  47. data/lib/schemacop/validator/float_validator.rb +1 -1
  48. data/lib/schemacop/validator/integer_validator.rb +1 -1
  49. data/schemacop.gemspec +14 -8
  50. data/test/casting_test.rb +90 -0
  51. data/test/custom_check_test.rb +14 -14
  52. data/test/custom_if_test.rb +12 -12
  53. data/test/defaults_test.rb +71 -0
  54. data/test/nil_dis_allow_test.rb +6 -6
  55. data/test/node_resolver_test.rb +26 -0
  56. data/test/short_forms_test.rb +73 -66
  57. data/test/test_helper.rb +7 -0
  58. data/test/types_test.rb +5 -5
  59. data/test/validator_array_test.rb +16 -16
  60. data/test/validator_boolean_test.rb +2 -2
  61. data/test/validator_float_test.rb +15 -15
  62. data/test/validator_hash_test.rb +5 -5
  63. data/test/validator_integer_test.rb +9 -9
  64. data/test/validator_nil_test.rb +1 -1
  65. data/test/validator_number_test.rb +19 -19
  66. data/test/validator_object_test.rb +18 -18
  67. data/test/validator_string_test.rb +12 -12
  68. data/test/validator_symbol_test.rb +2 -2
  69. metadata +42 -6
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f276c94be15a6c27b4c690e71cafe375db73b53984a700205a9b4a78024a31da
4
- data.tar.gz: 82dabde2bd0e6fc7fb06a5ebef3b4cf2065e718a0c27bb419960199b9898b145
3
+ metadata.gz: e82135ea68e1aa89e4fa729d481bcb6097705e01ba339b9692f7a69d2260f334
4
+ data.tar.gz: 26358b8124dfd17505830b7e5fb1446c37d34eb9d09533cc3b5f216d9fcccba3
5
5
  SHA512:
6
- metadata.gz: 6bb7bcb290cffe2bdf0089e98dc977a2d6d90458875cd89d84756cd3749bfd925fa5ff1987fa4b19ad1c89cee52944385d7e84ea8e8dea417db8c7cb007a4507
7
- data.tar.gz: 3d3122e09ff0aaed4605daec77228afbf38b02e604c0498bb8cd8c7a6bb5a8d17914378ba580f72d6e0155cef4dfa3b192689f7bf6a259d1efd8be82831ce746
6
+ metadata.gz: ba3a66136b5a4a756f08bf2f377621bbb047574f68e2e97f9a0d21680040330c44510e8c05b18f39da93d34a96d86a4342495d8bdcb2a0522336743a9cb19d9d
7
+ data.tar.gz: 8f0901419e3e22952ee289d47cc911f7304d5fa848456e1840255e2a5964a374ce48284927e804c530b22eb61408eb4b516dcbce8da46e969e16e504084469ef
@@ -10,6 +10,21 @@
10
10
  ### Changes
11
11
  -->
12
12
 
13
+ ## 2.4.0 (2019-10-28)
14
+
15
+ ### New features
16
+
17
+ * Add support for default values
18
+
19
+ * Add support for type casting
20
+
21
+ ### Bug fixes
22
+
23
+ * Change order of built-in validators so that `Integer` and `String` come
24
+ *before* `Number` which matches both.
25
+
26
+ ### Changes
27
+
13
28
  ## 2.3.2 (2019-09-26)
14
29
 
15
30
  ### New features
data/README.md CHANGED
@@ -124,12 +124,40 @@ We will see Type and Field lines in more detail below.
124
124
  ### `validate` vs `validate!` vs `valid?`
125
125
 
126
126
  The method `validate` will return a `Collector` object that contains all
127
- validation errors (if any), whereas `validate!` will accumulate all violations
128
- and finally throw an exception describing them.
127
+ validation errors (if any) as well as a deep copy of your data with applied
128
+ defaults and castings, whereas `validate!` will accumulate all violations
129
+ and finally throw an exception describing them or, if the validation was
130
+ successful, a deep-copy of your supplied data with defaults and castings
131
+ applied.
129
132
 
130
133
  For simply querying the validity of some data, use the methods `valid?` or
131
134
  `invalid?`.
132
135
 
136
+ Examples:
137
+
138
+ ```ruby
139
+ # validate! returns your modified data or throws a validation error
140
+ s = Schema.new do
141
+ req :foo, default: 42
142
+ end
143
+ s.validate!({}) # => { foo: 42 }
144
+
145
+ # validate returns a collector
146
+ s = Schema.new do
147
+ req :foo, default: 42
148
+ end
149
+
150
+ collector = s.validate({})
151
+ collector.valid? # true
152
+ collector.data # => { foo: 42 }
153
+
154
+ collector = s.validate({ foo: 'invalid' })
155
+ collector.valid? # false
156
+ collector.data # => nil
157
+ collector.exceptions # => Validation error
158
+ ```
159
+
160
+
133
161
  ## Schemacop's DSL
134
162
 
135
163
  In this section, we will ignore [short forms](#short-forms) and explicitly
@@ -489,6 +517,10 @@ Schema.new do
489
517
  end
490
518
  ```
491
519
 
520
+ Note that this does not allow you to specify any options for the hash itself.
521
+ You still need to specify `:hash` as a type if you want to pass any options to
522
+ the hash (i.e. a `default`).
523
+
492
524
  ### Shortform for subtypes
493
525
 
494
526
  In case of nested arrays, you can group all Type Lines to a single one.
@@ -547,6 +579,149 @@ of type Array with children of type Array with children of type Hash in which at
547
579
  least one of the Symbol keys `:food` and `:drink` (with any non-nil value type)
548
580
  is present.
549
581
 
582
+ ## Defaults
583
+
584
+ Starting from version 2.4.0, Schemacop allows you to define default values at
585
+ any point in your schema. If the validated data contains a nil value, it will be
586
+ substituted by the given default value.
587
+
588
+ Note that Schemacop never modifies the data you pass to it. If you want to
589
+ benefit from Schemacop-applied defaults, you need to access the cloned, modified
590
+ data returned by `validate` or `validate!`.
591
+
592
+ Applying defaults is done before validating the substructure and before any type
593
+ casting. The provided default will be validated same as user-supplied data, so
594
+ if your given default does not validate properly, a validation error is thrown.
595
+ Make sure your default values always match the underlying schema.
596
+
597
+ Defaults can be specified at any point:
598
+
599
+
600
+ ```ruby
601
+ # Basic usage
602
+ Schema.new do
603
+ type :string, default: 'Hello World'
604
+ end
605
+
606
+ # The default given for the first type will match
607
+ Schema.new do
608
+ type :string, default: 'Hello World' # This will always be applied of no value is supplied
609
+ type :integer, default: 42
610
+ end
611
+
612
+ # You can also pass entire hashes or arrays to your defaults
613
+ Schema.new do
614
+ req :foo, :hash, default: { foo: :bar } do
615
+ req :foo, :symbol
616
+ end
617
+ req :bar, :array, :integer, default: [1, 2, 3]
618
+ end
619
+
620
+ # Defaults must match the given schema. The following will fail.
621
+ Schema.new do
622
+ req :foo, default: { bar: :baz } do
623
+ req :foo
624
+ end
625
+ end
626
+ ```
627
+
628
+ ### Required data points
629
+
630
+ Note that any *required* validation is done before applying the defaults. If you
631
+ specify a `req` field, it must always be given, no matter if you have specified
632
+ a default or not. Therefore, specifying `req` fields do not make sense in
633
+ conjunction with defaults, as the default is always ignored.
634
+
635
+ ## Type casting
636
+
637
+ Starting from version 2.4.0, Schemacop allows you to specify type castings that
638
+ can alter the validated data. Consider the following:
639
+
640
+ ```ruby
641
+ s = Schema.new do
642
+ req :id, :integer, cast: [String]
643
+ end
644
+
645
+ data = s.validate!(id: '42')
646
+ data # => { id: 42 }
647
+ ```
648
+
649
+ Note that Schemacop never modifies the data you pass to it. If you want to
650
+ benefit from Schemacop-applied castings, you need to access the cloned, modified
651
+ data returned by `validate` or `validate!`.
652
+
653
+ ### Specifying type castings
654
+
655
+ Type castings can be specified using two forms: Either as a hash or as an array.
656
+ While using an array only allows you to specify the supported source types to be
657
+ casted, using a hash allows you to specify custom casting logic as blocks.
658
+
659
+ For hashes, the key must be a class and the value must be either `:default` for
660
+ using a built-in caster or a callable object (proc or lambda) that receives the
661
+ value and is supposed to cast it. If the value can't be casted, the proc must
662
+ fail with an exception. The exception message will then be contained in the
663
+ collected validation errors.
664
+
665
+ Example:
666
+
667
+ ```ruby
668
+ Schema.new do
669
+ # Pass array to `cast`. This enables casting from String or Float to Integer
670
+ # using the built-in casters.
671
+ req: id_1, :integer, cast: [String, Float]
672
+
673
+ # Pass hash to `cast`. This enables casting from Float to Integer using the
674
+ # built-in caster and from String to Integer using a custom callback.
675
+ req :id_2, :integer, cast: { Float => :default, String => proc { |s| Integer(s) }
676
+ end
677
+ ```
678
+
679
+ ### Built-in casters
680
+
681
+ Schemacop comes with the following casters:
682
+
683
+ - `String` to `Integer` and `Float`
684
+ - `Float` to `Integer`
685
+ - `Integer` to `Float`
686
+
687
+ Note that all built-in casters are precise, so the string `foo` will fail with
688
+ an error if casted to an Integer. When casting float values and strings
689
+ containing float values to integers, the decimal places will be discarded
690
+ however.
691
+
692
+ ### Execution order
693
+
694
+ The casting is done *before* the options `if` and `check` are evaluated.
695
+ Example:
696
+
697
+ ```ruby
698
+ s = Schema.new do
699
+ type :integer, if: proc { |i| i == 42 } # 1
700
+ type :integer, check: proc { |i| i < 3 } # 2
701
+ type :string
702
+ end
703
+
704
+ s.validate!('42') # 1 will match
705
+ s.validate!('2') # 2 will match
706
+ s.validate!('234') # 3 will match
707
+ s.validate!(5) # Will fail, as nothing matches
708
+ ```
709
+
710
+ ### Caveats
711
+
712
+ Casting only works with type definitions that only include one type. For
713
+ instance, the `Numeric` validator includes both `Integer` and `Float`, which
714
+ would made it unclear what to cast a string into:
715
+
716
+ ```ruby
717
+ # This does not work, as it is unclear whether to cast the String into an
718
+ # Integer or a Float.
719
+ type :number, cast: [String]
720
+ ```
721
+
722
+ The same also applies to booleans, as they compound both `TrueClass` and
723
+ `FalseClass`. This may be tackled in future releases.
724
+
550
725
  ## Exceptions
551
726
 
552
727
  Schemacop will throw one of the following checked exceptions:
@@ -572,6 +747,19 @@ Schemacop will throw one of the following checked exceptions:
572
747
 
573
748
  * Schemacop does not yet support string regex matching.
574
749
 
750
+ ## Development
751
+
752
+ To run tests:
753
+
754
+ * Check out the source
755
+
756
+ * Run `bundle install`
757
+
758
+ * Run `bundle exec rake test` to run all tests
759
+
760
+ * Run `bundle exec rake test TEST=test/unit/some/file.rb` to run a single test
761
+ file
762
+
575
763
  ## Contributors
576
764
 
577
765
  Thanks to [Rubocop](https://github.com/bbatsov/rubocop) for great inspiration
@@ -1 +1 @@
1
- ruby-2.3.1-p112
1
+ ruby-2.6.2-p47
data/Rakefile CHANGED
@@ -14,19 +14,20 @@ task :gemspec do
14
14
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
15
15
  spec.require_paths = ['lib']
16
16
 
17
+ # This lower bound for ActiveSupport is not necessarily true. Schemacop
18
+ # needs access to ActiveSupport::HashWithIndifferentAccess and expects
19
+ # behavior of that as in version 5 of ActiveSupport.
20
+ spec.add_dependency 'activesupport', '>= 4.0'
17
21
  spec.add_development_dependency 'bundler', '~> 1.3'
18
22
  spec.add_development_dependency 'rake'
19
23
  spec.add_development_dependency 'ci_reporter', '~> 2.0'
20
24
  spec.add_development_dependency 'ci_reporter_minitest'
21
25
  spec.add_development_dependency 'haml'
26
+ spec.add_development_dependency 'colorize'
22
27
  spec.add_development_dependency 'yard'
23
28
  spec.add_development_dependency 'rubocop', '0.35.1'
24
29
  spec.add_development_dependency 'redcarpet'
25
-
26
- # This lower bound for ActiveSupport is not necessarily true. Schemacop
27
- # needs access to ActiveSupport::HashWithIndifferentAccess and expects
28
- # behavior of that as in version 5 of ActiveSupport.
29
- spec.add_dependency 'activesupport', '>= 4.0'
30
+ spec.add_development_dependency 'pry'
30
31
  end
31
32
 
32
33
  File.open('schemacop.gemspec', 'w') { |f| f.write(gemspec.to_ruby.strip) }
data/VERSION CHANGED
@@ -1 +1 @@
1
- 2.3.2
1
+ 2.4.0
@@ -79,7 +79,7 @@
79
79
  <dl>
80
80
  <dt>Defined in:</dt>
81
81
  <dd>lib/schemacop.rb<span class="defines">,<br />
82
- lib/schemacop/node.rb,<br /> lib/schemacop/schema.rb,<br /> lib/schemacop/collector.rb,<br /> lib/schemacop/root_node.rb,<br /> lib/schemacop/field_node.rb,<br /> lib/schemacop/node_resolver.rb,<br /> lib/schemacop/node_with_block.rb,<br /> lib/schemacop/node_supporting_type.rb,<br /> lib/schemacop/node_supporting_field.rb,<br /> lib/schemacop/validator/nil_validator.rb,<br /> lib/schemacop/validator/hash_validator.rb,<br /> lib/schemacop/validator/array_validator.rb,<br /> lib/schemacop/validator/float_validator.rb,<br /> lib/schemacop/validator/number_validator.rb,<br /> lib/schemacop/validator/object_validator.rb,<br /> lib/schemacop/validator/string_validator.rb,<br /> lib/schemacop/validator/symbol_validator.rb,<br /> lib/schemacop/validator/boolean_validator.rb,<br /> lib/schemacop/validator/integer_validator.rb</span>
82
+ lib/schemacop/node.rb,<br /> lib/schemacop/caster.rb,<br /> lib/schemacop/schema.rb,<br /> lib/schemacop/collector.rb,<br /> lib/schemacop/root_node.rb,<br /> lib/schemacop/field_node.rb,<br /> lib/schemacop/node_resolver.rb,<br /> lib/schemacop/node_with_block.rb,<br /> lib/schemacop/node_supporting_type.rb,<br /> lib/schemacop/node_supporting_field.rb,<br /> lib/schemacop/validator/nil_validator.rb,<br /> lib/schemacop/validator/hash_validator.rb,<br /> lib/schemacop/validator/array_validator.rb,<br /> lib/schemacop/validator/float_validator.rb,<br /> lib/schemacop/validator/number_validator.rb,<br /> lib/schemacop/validator/object_validator.rb,<br /> lib/schemacop/validator/string_validator.rb,<br /> lib/schemacop/validator/symbol_validator.rb,<br /> lib/schemacop/validator/boolean_validator.rb,<br /> lib/schemacop/validator/integer_validator.rb</span>
83
83
  </dd>
84
84
  </dl>
85
85
 
@@ -93,11 +93,38 @@
93
93
 
94
94
 
95
95
 
96
- <strong class="classes">Classes:</strong> <span class='object_link'><a href="Schemacop/ArrayValidator.html" title="Schemacop::ArrayValidator (class)">ArrayValidator</a></span>, <span class='object_link'><a href="Schemacop/BooleanValidator.html" title="Schemacop::BooleanValidator (class)">BooleanValidator</a></span>, <span class='object_link'><a href="Schemacop/Collector.html" title="Schemacop::Collector (class)">Collector</a></span>, <span class='object_link'><a href="Schemacop/FieldNode.html" title="Schemacop::FieldNode (class)">FieldNode</a></span>, <span class='object_link'><a href="Schemacop/FloatValidator.html" title="Schemacop::FloatValidator (class)">FloatValidator</a></span>, <span class='object_link'><a href="Schemacop/HashValidator.html" title="Schemacop::HashValidator (class)">HashValidator</a></span>, <span class='object_link'><a href="Schemacop/IntegerValidator.html" title="Schemacop::IntegerValidator (class)">IntegerValidator</a></span>, <span class='object_link'><a href="Schemacop/NilValidator.html" title="Schemacop::NilValidator (class)">NilValidator</a></span>, <span class='object_link'><a href="Schemacop/Node.html" title="Schemacop::Node (class)">Node</a></span>, <span class='object_link'><a href="Schemacop/NodeResolver.html" title="Schemacop::NodeResolver (class)">NodeResolver</a></span>, <span class='object_link'><a href="Schemacop/NodeSupportingField.html" title="Schemacop::NodeSupportingField (class)">NodeSupportingField</a></span>, <span class='object_link'><a href="Schemacop/NodeSupportingType.html" title="Schemacop::NodeSupportingType (class)">NodeSupportingType</a></span>, <span class='object_link'><a href="Schemacop/NodeWithBlock.html" title="Schemacop::NodeWithBlock (class)">NodeWithBlock</a></span>, <span class='object_link'><a href="Schemacop/NumberValidator.html" title="Schemacop::NumberValidator (class)">NumberValidator</a></span>, <span class='object_link'><a href="Schemacop/ObjectValidator.html" title="Schemacop::ObjectValidator (class)">ObjectValidator</a></span>, <span class='object_link'><a href="Schemacop/RootNode.html" title="Schemacop::RootNode (class)">RootNode</a></span>, <span class='object_link'><a href="Schemacop/Schema.html" title="Schemacop::Schema (class)">Schema</a></span>, <span class='object_link'><a href="Schemacop/StringValidator.html" title="Schemacop::StringValidator (class)">StringValidator</a></span>, <span class='object_link'><a href="Schemacop/SymbolValidator.html" title="Schemacop::SymbolValidator (class)">SymbolValidator</a></span>
96
+ <strong class="classes">Classes:</strong> <span class='object_link'><a href="Schemacop/ArrayValidator.html" title="Schemacop::ArrayValidator (class)">ArrayValidator</a></span>, <span class='object_link'><a href="Schemacop/BooleanValidator.html" title="Schemacop::BooleanValidator (class)">BooleanValidator</a></span>, <span class='object_link'><a href="Schemacop/Caster.html" title="Schemacop::Caster (class)">Caster</a></span>, <span class='object_link'><a href="Schemacop/Collector.html" title="Schemacop::Collector (class)">Collector</a></span>, <span class='object_link'><a href="Schemacop/FieldNode.html" title="Schemacop::FieldNode (class)">FieldNode</a></span>, <span class='object_link'><a href="Schemacop/FloatValidator.html" title="Schemacop::FloatValidator (class)">FloatValidator</a></span>, <span class='object_link'><a href="Schemacop/HashValidator.html" title="Schemacop::HashValidator (class)">HashValidator</a></span>, <span class='object_link'><a href="Schemacop/IntegerValidator.html" title="Schemacop::IntegerValidator (class)">IntegerValidator</a></span>, <span class='object_link'><a href="Schemacop/NilValidator.html" title="Schemacop::NilValidator (class)">NilValidator</a></span>, <span class='object_link'><a href="Schemacop/Node.html" title="Schemacop::Node (class)">Node</a></span>, <span class='object_link'><a href="Schemacop/NodeResolver.html" title="Schemacop::NodeResolver (class)">NodeResolver</a></span>, <span class='object_link'><a href="Schemacop/NodeSupportingField.html" title="Schemacop::NodeSupportingField (class)">NodeSupportingField</a></span>, <span class='object_link'><a href="Schemacop/NodeSupportingType.html" title="Schemacop::NodeSupportingType (class)">NodeSupportingType</a></span>, <span class='object_link'><a href="Schemacop/NodeWithBlock.html" title="Schemacop::NodeWithBlock (class)">NodeWithBlock</a></span>, <span class='object_link'><a href="Schemacop/NumberValidator.html" title="Schemacop::NumberValidator (class)">NumberValidator</a></span>, <span class='object_link'><a href="Schemacop/ObjectValidator.html" title="Schemacop::ObjectValidator (class)">ObjectValidator</a></span>, <span class='object_link'><a href="Schemacop/RootNode.html" title="Schemacop::RootNode (class)">RootNode</a></span>, <span class='object_link'><a href="Schemacop/Schema.html" title="Schemacop::Schema (class)">Schema</a></span>, <span class='object_link'><a href="Schemacop/StringValidator.html" title="Schemacop::StringValidator (class)">StringValidator</a></span>, <span class='object_link'><a href="Schemacop/SymbolValidator.html" title="Schemacop::SymbolValidator (class)">SymbolValidator</a></span>
97
97
 
98
98
 
99
99
  </p>
100
100
 
101
+
102
+ <h2>
103
+ Constant Summary
104
+ <small><a href="#" class="constants_summary_toggle">collapse</a></small>
105
+ </h2>
106
+
107
+ <dl class="constants">
108
+
109
+ <dt id="DEFAULT_CASTERS-constant" class="">DEFAULT_CASTERS =
110
+
111
+ </dt>
112
+ <dd><pre class="code"><span class='lbrace'>{</span>
113
+ <span class='const'>String</span> <span class='op'>=&gt;</span> <span class='lbrace'>{</span>
114
+ <span class='const'>Integer</span> <span class='op'>=&gt;</span> <span class='id identifier rubyid_proc'>proc</span> <span class='lbrace'>{</span> <span class='op'>|</span><span class='id identifier rubyid_s'>s</span><span class='op'>|</span> <span class='const'>Integer</span><span class='lparen'>(</span><span class='id identifier rubyid_s'>s</span><span class='rparen'>)</span> <span class='rbrace'>}</span><span class='comma'>,</span>
115
+ <span class='const'>Float</span> <span class='op'>=&gt;</span> <span class='id identifier rubyid_proc'>proc</span> <span class='lbrace'>{</span> <span class='op'>|</span><span class='id identifier rubyid_s'>s</span><span class='op'>|</span> <span class='const'>Float</span><span class='lparen'>(</span><span class='id identifier rubyid_s'>s</span><span class='rparen'>)</span> <span class='rbrace'>}</span>
116
+ <span class='rbrace'>}</span><span class='comma'>,</span>
117
+ <span class='const'>Float</span> <span class='op'>=&gt;</span> <span class='lbrace'>{</span>
118
+ <span class='const'>Integer</span> <span class='op'>=&gt;</span> <span class='id identifier rubyid_proc'>proc</span> <span class='lbrace'>{</span> <span class='op'>|</span><span class='id identifier rubyid_f'>f</span><span class='op'>|</span> <span class='const'>Integer</span><span class='lparen'>(</span><span class='id identifier rubyid_f'>f</span><span class='rparen'>)</span> <span class='rbrace'>}</span>
119
+ <span class='rbrace'>}</span><span class='comma'>,</span>
120
+ <span class='const'>Integer</span> <span class='op'>=&gt;</span> <span class='lbrace'>{</span>
121
+ <span class='const'>Float</span> <span class='op'>=&gt;</span> <span class='id identifier rubyid_proc'>proc</span> <span class='lbrace'>{</span> <span class='op'>|</span><span class='id identifier rubyid_f'>f</span><span class='op'>|</span> <span class='const'>Float</span><span class='lparen'>(</span><span class='id identifier rubyid_f'>f</span><span class='rparen'>)</span> <span class='rbrace'>}</span>
122
+ <span class='rbrace'>}</span>
123
+ <span class='rbrace'>}</span></pre></dd>
124
+
125
+ </dl>
126
+
127
+
101
128
 
102
129
 
103
130
 
@@ -109,7 +136,7 @@
109
136
  </div>
110
137
 
111
138
  <div id="footer">
112
- Generated on Thu Sep 26 13:19:46 2019 by
139
+ Generated on Mon Oct 28 16:21:59 2019 by
113
140
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
114
141
  0.9.20 (ruby-2.6.2).
115
142
  </div>
@@ -304,7 +304,7 @@
304
304
  <span class='id identifier rubyid_collector'>collector</span><span class='period'>.</span><span class='id identifier rubyid_error'>error</span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>Array must have less (&lt;=) than </span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_option'>option</span><span class='lparen'>(</span><span class='symbol'>:max</span><span class='rparen'>)</span><span class='embexpr_end'>}</span><span class='tstring_content'> elements.</span><span class='tstring_end'>&quot;</span></span>
305
305
  <span class='kw'>end</span>
306
306
  <span class='id identifier rubyid_data'>data</span><span class='period'>.</span><span class='id identifier rubyid_each_with_index'>each_with_index</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_entry'>entry</span><span class='comma'>,</span> <span class='id identifier rubyid_index'>index</span><span class='op'>|</span>
307
- <span class='id identifier rubyid_collector'>collector</span><span class='period'>.</span><span class='id identifier rubyid_path'>path</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>[</span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_index'>index</span><span class='embexpr_end'>}</span><span class='tstring_content'>]</span><span class='tstring_end'>&quot;</span></span><span class='rparen'>)</span> <span class='kw'>do</span>
307
+ <span class='id identifier rubyid_collector'>collector</span><span class='period'>.</span><span class='id identifier rubyid_path'>path</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>[</span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_index'>index</span><span class='embexpr_end'>}</span><span class='tstring_content'>]</span><span class='tstring_end'>&quot;</span></span><span class='comma'>,</span> <span class='id identifier rubyid_index'>index</span><span class='comma'>,</span> <span class='symbol'>:array</span><span class='rparen'>)</span> <span class='kw'>do</span>
308
308
  <span class='id identifier rubyid_validate_types'>validate_types</span><span class='lparen'>(</span><span class='id identifier rubyid_entry'>entry</span><span class='comma'>,</span> <span class='id identifier rubyid_collector'>collector</span><span class='rparen'>)</span>
309
309
  <span class='kw'>end</span>
310
310
  <span class='kw'>end</span>
@@ -319,7 +319,7 @@
319
319
  </div>
320
320
 
321
321
  <div id="footer">
322
- Generated on Thu Sep 26 13:19:47 2019 by
322
+ Generated on Mon Oct 28 16:21:59 2019 by
323
323
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
324
324
  0.9.20 (ruby-2.6.2).
325
325
  </div>
@@ -135,7 +135,7 @@
135
135
  </div>
136
136
 
137
137
  <div id="footer">
138
- Generated on Thu Sep 26 13:19:47 2019 by
138
+ Generated on Mon Oct 28 16:22:00 2019 by
139
139
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
140
140
  0.9.20 (ruby-2.6.2).
141
141
  </div>
@@ -0,0 +1,379 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <meta charset="utf-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>
7
+ Class: Schemacop::Caster
8
+
9
+ &mdash; Documentation by YARD 0.9.20
10
+
11
+ </title>
12
+
13
+ <link rel="stylesheet" href="../css/style.css" type="text/css" charset="utf-8" />
14
+
15
+ <link rel="stylesheet" href="../css/common.css" type="text/css" charset="utf-8" />
16
+
17
+ <script type="text/javascript" charset="utf-8">
18
+ pathId = "Schemacop::Caster";
19
+ relpath = '../';
20
+ </script>
21
+
22
+
23
+ <script type="text/javascript" charset="utf-8" src="../js/jquery.js"></script>
24
+
25
+ <script type="text/javascript" charset="utf-8" src="../js/app.js"></script>
26
+
27
+
28
+ </head>
29
+ <body>
30
+ <div class="nav_wrap">
31
+ <iframe id="nav" src="../class_list.html?1"></iframe>
32
+ <div id="resizer"></div>
33
+ </div>
34
+
35
+ <div id="main" tabindex="-1">
36
+ <div id="header">
37
+ <div id="menu">
38
+
39
+ <a href="../_index.html">Index (C)</a> &raquo;
40
+ <span class='title'><span class='object_link'><a href="../Schemacop.html" title="Schemacop (module)">Schemacop</a></span></span>
41
+ &raquo;
42
+ <span class="title">Caster</span>
43
+
44
+ </div>
45
+
46
+ <div id="search">
47
+
48
+ <a class="full_list_link" id="class_list_link"
49
+ href="../class_list.html">
50
+
51
+ <svg width="24" height="24">
52
+ <rect x="0" y="4" width="24" height="4" rx="1" ry="1"></rect>
53
+ <rect x="0" y="12" width="24" height="4" rx="1" ry="1"></rect>
54
+ <rect x="0" y="20" width="24" height="4" rx="1" ry="1"></rect>
55
+ </svg>
56
+ </a>
57
+
58
+ </div>
59
+ <div class="clear"></div>
60
+ </div>
61
+
62
+ <div id="content"><h1>Class: Schemacop::Caster
63
+
64
+
65
+
66
+ </h1>
67
+ <div class="box_info">
68
+
69
+ <dl>
70
+ <dt>Inherits:</dt>
71
+ <dd>
72
+ <span class="inheritName">Object</span>
73
+
74
+ <ul class="fullTree">
75
+ <li>Object</li>
76
+
77
+ <li class="next">Schemacop::Caster</li>
78
+
79
+ </ul>
80
+ <a href="#" class="inheritanceTree">show all</a>
81
+
82
+ </dd>
83
+ </dl>
84
+
85
+
86
+
87
+
88
+
89
+
90
+
91
+
92
+
93
+
94
+
95
+ <dl>
96
+ <dt>Defined in:</dt>
97
+ <dd>lib/schemacop/caster.rb</dd>
98
+ </dl>
99
+
100
+ </div>
101
+
102
+
103
+
104
+
105
+
106
+
107
+
108
+
109
+
110
+ <h2>
111
+ Instance Method Summary
112
+ <small><a href="#" class="summary_toggle">collapse</a></small>
113
+ </h2>
114
+
115
+ <ul class="summary">
116
+
117
+ <li class="public ">
118
+ <span class="summary_signature">
119
+
120
+ <a href="#cast-instance_method" title="#cast (instance method)">#<strong>cast</strong> &#x21d2; Object </a>
121
+
122
+
123
+
124
+ </span>
125
+
126
+
127
+
128
+
129
+
130
+
131
+
132
+
133
+
134
+ <span class="summary_desc"><div class='inline'></div></span>
135
+
136
+ </li>
137
+
138
+
139
+ <li class="public ">
140
+ <span class="summary_signature">
141
+
142
+ <a href="#castable%3F-instance_method" title="#castable? (instance method)">#<strong>castable?</strong> &#x21d2; Boolean </a>
143
+
144
+
145
+
146
+ </span>
147
+
148
+
149
+
150
+
151
+
152
+
153
+
154
+
155
+
156
+ <span class="summary_desc"><div class='inline'></div></span>
157
+
158
+ </li>
159
+
160
+
161
+ <li class="public ">
162
+ <span class="summary_signature">
163
+
164
+ <a href="#initialize-instance_method" title="#initialize (instance method)">#<strong>initialize</strong>(casts, data, target_type) &#x21d2; Caster </a>
165
+
166
+
167
+
168
+ </span>
169
+
170
+
171
+ <span class="note title constructor">constructor</span>
172
+
173
+
174
+
175
+
176
+
177
+
178
+
179
+
180
+ <span class="summary_desc"><div class='inline'><p>A new instance of Caster.</p>
181
+ </div></span>
182
+
183
+ </li>
184
+
185
+
186
+ </ul>
187
+
188
+
189
+ <div id="constructor_details" class="method_details_list">
190
+ <h2>Constructor Details</h2>
191
+
192
+ <div class="method_details first">
193
+ <h3 class="signature first" id="initialize-instance_method">
194
+
195
+ #<strong>initialize</strong>(casts, data, target_type) &#x21d2; <tt><span class='object_link'><a href="" title="Schemacop::Caster (class)">Caster</a></span></tt>
196
+
197
+
198
+
199
+
200
+
201
+ </h3><div class="docstring">
202
+ <div class="discussion">
203
+ <p>Returns a new instance of Caster</p>
204
+
205
+
206
+ </div>
207
+ </div>
208
+ <div class="tags">
209
+
210
+
211
+ </div><table class="source_code">
212
+ <tr>
213
+ <td>
214
+ <pre class="lines">
215
+
216
+
217
+ 3
218
+ 4
219
+ 5
220
+ 6
221
+ 7
222
+ 8
223
+ 9
224
+ 10
225
+ 11
226
+ 12
227
+ 13
228
+ 14
229
+ 15
230
+ 16
231
+ 17
232
+ 18
233
+ 19
234
+ 20
235
+ 21
236
+ 22
237
+ 23
238
+ 24</pre>
239
+ </td>
240
+ <td>
241
+ <pre class="code"><span class="info file"># File 'lib/schemacop/caster.rb', line 3</span>
242
+
243
+ <span class='kw'>def</span> <span class='id identifier rubyid_initialize'>initialize</span><span class='lparen'>(</span><span class='id identifier rubyid_casts'>casts</span><span class='comma'>,</span> <span class='id identifier rubyid_data'>data</span><span class='comma'>,</span> <span class='id identifier rubyid_target_type'>target_type</span><span class='rparen'>)</span>
244
+ <span class='ivar'>@casts</span> <span class='op'>=</span> <span class='id identifier rubyid_casts'>casts</span>
245
+ <span class='ivar'>@data</span> <span class='op'>=</span> <span class='id identifier rubyid_data'>data</span>
246
+ <span class='ivar'>@target_type</span> <span class='op'>=</span> <span class='id identifier rubyid_target_type'>target_type</span>
247
+ <span class='ivar'>@caster</span> <span class='op'>=</span> <span class='kw'>nil</span>
248
+
249
+ <span class='kw'>if</span> <span class='id identifier rubyid_casts'>casts</span><span class='period'>.</span><span class='id identifier rubyid_is_a?'>is_a?</span><span class='lparen'>(</span><span class='const'>Array</span><span class='rparen'>)</span>
250
+ <span class='id identifier rubyid_from_types'>from_types</span> <span class='op'>=</span> <span class='id identifier rubyid_casts'>casts</span>
251
+ <span class='kw'>elsif</span> <span class='id identifier rubyid_casts'>casts</span><span class='period'>.</span><span class='id identifier rubyid_is_a?'>is_a?</span><span class='lparen'>(</span><span class='const'>Hash</span><span class='rparen'>)</span>
252
+ <span class='id identifier rubyid_from_types'>from_types</span> <span class='op'>=</span> <span class='id identifier rubyid_casts'>casts</span><span class='period'>.</span><span class='id identifier rubyid_keys'>keys</span>
253
+ <span class='kw'>else</span>
254
+ <span class='id identifier rubyid_fail'>fail</span> <span class='const'><span class='object_link'><a href="Exceptions.html" title="Schemacop::Exceptions (module)">Exceptions</a></span></span><span class='op'>::</span><span class='const'><span class='object_link'><a href="Exceptions/InvalidSchemaError.html" title="Schemacop::Exceptions::InvalidSchemaError (class)">InvalidSchemaError</a></span></span><span class='comma'>,</span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>Option `cast` must be either an array or a hash.</span><span class='tstring_end'>&quot;</span></span>
255
+ <span class='kw'>end</span>
256
+
257
+ <span class='kw'>return</span> <span class='kw'>unless</span> <span class='id identifier rubyid_from_types'>from_types</span><span class='period'>.</span><span class='id identifier rubyid_include?'>include?</span><span class='lparen'>(</span><span class='id identifier rubyid_data'>data</span><span class='period'>.</span><span class='id identifier rubyid_class'>class</span><span class='rparen'>)</span>
258
+
259
+ <span class='kw'>if</span> <span class='lparen'>(</span><span class='id identifier rubyid_casts'>casts</span><span class='period'>.</span><span class='id identifier rubyid_is_a?'>is_a?</span><span class='lparen'>(</span><span class='const'>Array</span><span class='rparen'>)</span> <span class='op'>&amp;&amp;</span> <span class='id identifier rubyid_casts'>casts</span><span class='period'>.</span><span class='id identifier rubyid_include?'>include?</span><span class='lparen'>(</span><span class='id identifier rubyid_data'>data</span><span class='period'>.</span><span class='id identifier rubyid_class'>class</span><span class='rparen'>)</span><span class='rparen'>)</span> <span class='op'>||</span> <span class='id identifier rubyid_casts'>casts</span><span class='lbracket'>[</span><span class='id identifier rubyid_data'>data</span><span class='period'>.</span><span class='id identifier rubyid_class'>class</span><span class='rbracket'>]</span> <span class='op'>==</span> <span class='symbol'>:default</span>
260
+ <span class='ivar'>@caster</span> <span class='op'>=</span> <span class='const'><span class='object_link'><a href="../Schemacop.html#DEFAULT_CASTERS-constant" title="Schemacop::DEFAULT_CASTERS (constant)">DEFAULT_CASTERS</a></span></span><span class='lbracket'>[</span><span class='id identifier rubyid_data'>data</span><span class='period'>.</span><span class='id identifier rubyid_class'>class</span><span class='rbracket'>]</span><span class='lbracket'>[</span><span class='id identifier rubyid_target_type'>target_type</span><span class='rbracket'>]</span>
261
+ <span class='kw'>else</span>
262
+ <span class='ivar'>@caster</span> <span class='op'>=</span> <span class='id identifier rubyid_casts'>casts</span><span class='lbracket'>[</span><span class='id identifier rubyid_data'>data</span><span class='period'>.</span><span class='id identifier rubyid_class'>class</span><span class='rbracket'>]</span>
263
+ <span class='kw'>end</span>
264
+ <span class='kw'>end</span></pre>
265
+ </td>
266
+ </tr>
267
+ </table>
268
+ </div>
269
+
270
+ </div>
271
+
272
+
273
+ <div id="instance_method_details" class="method_details_list">
274
+ <h2>Instance Method Details</h2>
275
+
276
+
277
+ <div class="method_details first">
278
+ <h3 class="signature first" id="cast-instance_method">
279
+
280
+ #<strong>cast</strong> &#x21d2; <tt>Object</tt>
281
+
282
+
283
+
284
+
285
+
286
+ </h3><table class="source_code">
287
+ <tr>
288
+ <td>
289
+ <pre class="lines">
290
+
291
+
292
+ 30
293
+ 31
294
+ 32
295
+ 33
296
+ 34
297
+ 35
298
+ 36</pre>
299
+ </td>
300
+ <td>
301
+ <pre class="code"><span class="info file"># File 'lib/schemacop/caster.rb', line 30</span>
302
+
303
+ <span class='kw'>def</span> <span class='id identifier rubyid_cast'>cast</span>
304
+ <span class='id identifier rubyid_fail'>fail</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>Not castable.</span><span class='tstring_end'>&#39;</span></span> <span class='kw'>unless</span> <span class='id identifier rubyid_castable?'>castable?</span>
305
+ <span class='kw'>return</span> <span class='ivar'>@caster</span><span class='period'>.</span><span class='id identifier rubyid_call'>call</span><span class='lparen'>(</span><span class='ivar'>@data</span><span class='rparen'>)</span>
306
+ <span class='kw'>rescue</span> <span class='op'>=&gt;</span> <span class='id identifier rubyid_e'>e</span>
307
+ <span class='id identifier rubyid_fail'>fail</span> <span class='const'><span class='object_link'><a href="Exceptions.html" title="Schemacop::Exceptions (module)">Exceptions</a></span></span><span class='op'>::</span><span class='const'><span class='object_link'><a href="Exceptions/InvalidSchemaError.html" title="Schemacop::Exceptions::InvalidSchemaError (class)">InvalidSchemaError</a></span></span><span class='comma'>,</span>
308
+ <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>Could not cast value </span><span class='embexpr_beg'>#{</span><span class='ivar'>@value</span><span class='period'>.</span><span class='id identifier rubyid_inspect'>inspect</span><span class='embexpr_end'>}</span><span class='tstring_content'> to </span><span class='embexpr_beg'>#{</span><span class='ivar'>@target_type</span><span class='embexpr_end'>}</span><span class='tstring_content'>: </span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_e'>e</span><span class='period'>.</span><span class='id identifier rubyid_message'>message</span><span class='embexpr_end'>}</span><span class='tstring_content'>.</span><span class='tstring_end'>&quot;</span></span>
309
+ <span class='kw'>end</span></pre>
310
+ </td>
311
+ </tr>
312
+ </table>
313
+ </div>
314
+
315
+ <div class="method_details ">
316
+ <h3 class="signature " id="castable?-instance_method">
317
+
318
+ #<strong>castable?</strong> &#x21d2; <tt>Boolean</tt>
319
+
320
+
321
+
322
+
323
+
324
+ </h3><div class="docstring">
325
+ <div class="discussion">
326
+
327
+
328
+ </div>
329
+ </div>
330
+ <div class="tags">
331
+
332
+ <p class="tag_title">Returns:</p>
333
+ <ul class="return">
334
+
335
+ <li>
336
+
337
+
338
+ <span class='type'>(<tt>Boolean</tt>)</span>
339
+
340
+
341
+
342
+ </li>
343
+
344
+ </ul>
345
+
346
+ </div><table class="source_code">
347
+ <tr>
348
+ <td>
349
+ <pre class="lines">
350
+
351
+
352
+ 26
353
+ 27
354
+ 28</pre>
355
+ </td>
356
+ <td>
357
+ <pre class="code"><span class="info file"># File 'lib/schemacop/caster.rb', line 26</span>
358
+
359
+ <span class='kw'>def</span> <span class='id identifier rubyid_castable?'>castable?</span>
360
+ <span class='op'>!</span><span class='ivar'>@caster</span><span class='period'>.</span><span class='id identifier rubyid_nil?'>nil?</span>
361
+ <span class='kw'>end</span></pre>
362
+ </td>
363
+ </tr>
364
+ </table>
365
+ </div>
366
+
367
+ </div>
368
+
369
+ </div>
370
+
371
+ <div id="footer">
372
+ Generated on Mon Oct 28 16:21:59 2019 by
373
+ <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
374
+ 0.9.20 (ruby-2.6.2).
375
+ </div>
376
+
377
+ </div>
378
+ </body>
379
+ </html>