typed_model 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +15 -0
- data/.rspec +3 -0
- data/.travis.yml +7 -0
- data/Gemfile +6 -0
- data/LICENSE +21 -0
- data/LICENSE.txt +21 -0
- data/README.md +110 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/typed_model.rb +5 -0
- data/lib/typed_model/attribute_definition.rb +60 -0
- data/lib/typed_model/errors.rb +34 -0
- data/lib/typed_model/map_of.rb +33 -0
- data/lib/typed_model/model_base.rb +82 -0
- data/lib/typed_model/model_validations.rb +79 -0
- data/lib/typed_model/seq_of.rb +25 -0
- data/lib/typed_model/type_def.rb +112 -0
- data/lib/typed_model/types.rb +78 -0
- data/lib/typed_model/validator.rb +56 -0
- data/lib/typed_model/validators.rb +52 -0
- data/lib/typed_model/version.rb +3 -0
- data/typed_model.gemspec +42 -0
- metadata +115 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 7b1bfb6a568683689e6333d52789ba54cecb6582f107c44398f86001a90168cd
|
4
|
+
data.tar.gz: 220086f68dbb3033a6e9e91f562fe59c0709c7694d2ca79494b8452f722bd647
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6461a2b3a7e02a68bffeb6e2d330b17b562be0e1598ce35fe514f1a4c3fde0e1980e61fa23cacda63eedac2f40d2c802b46954172be4d7e9b11a8e2c370f068b
|
7
|
+
data.tar.gz: c939e41dd3c7e727f311e5ae9bede19ce4e1e9e1e1d9d2d790ddb055566982d1c735e573dcca198fb2ced894a0a79f08d9b1b169874113ebb328c7dbcdf106c1
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2019 Lenny Marks
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 TODO: Write your name
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
# TypedModel
|
2
|
+
|
3
|
+
A Ruby library for defining data schemas via classes with
|
4
|
+
typed fields and built in hydration and serialization
|
5
|
+
|
6
|
+
* data mapping (data -> object -> data)
|
7
|
+
* declared types
|
8
|
+
* declared validations
|
9
|
+
* Nested models
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
gem 'typed_model'
|
17
|
+
```
|
18
|
+
|
19
|
+
And then execute:
|
20
|
+
|
21
|
+
$ bundle
|
22
|
+
|
23
|
+
Or install it yourself as:
|
24
|
+
|
25
|
+
$ gem install typed_model
|
26
|
+
|
27
|
+
## Usage
|
28
|
+
|
29
|
+
Employee
|
30
|
+
include ModelBase
|
31
|
+
|
32
|
+
attribute :a_boolean, type: :boolean
|
33
|
+
attribute :an_integer, type: :integer
|
34
|
+
attribute :a_timestamp, type: :timestamp
|
35
|
+
attribute :widget1, type: :map
|
36
|
+
attribute :address, type: a_c
|
37
|
+
attribute :addresses, seq_of: a_c
|
38
|
+
attribute :colors, type: :seq
|
39
|
+
end
|
40
|
+
|
41
|
+
### Sequences
|
42
|
+
|
43
|
+
# non-empty/nil sequence
|
44
|
+
attribute :a_seq, type: :seq, :validations [:required]
|
45
|
+
|
46
|
+
attribute :a_seq, seq_of: :string
|
47
|
+
|
48
|
+
attribute :a_seq, seq_of: Address
|
49
|
+
|
50
|
+
# sequence of string -> integer maps
|
51
|
+
attribute :a_Seq, seq_of: [:string, :integer]
|
52
|
+
|
53
|
+
attribute :a_seq, seq_of: { type: Adress, :validations [:some_validation] }
|
54
|
+
|
55
|
+
### Maps
|
56
|
+
|
57
|
+
# string -> string
|
58
|
+
attribute :a_map, :map_of [:string, :integer]
|
59
|
+
|
60
|
+
attribute :a_map, type: :map, validations: [:required]
|
61
|
+
|
62
|
+
# map of maps
|
63
|
+
attribute :map_of_maps, :map_of [:string, [:string, :integer]]
|
64
|
+
|
65
|
+
# map of maps
|
66
|
+
attribute :a_map, :map_of [{type: :string, validators: [:some_check]},
|
67
|
+
{type: Address, validators: [:required]},
|
68
|
+
:validators [:required]
|
69
|
+
|
70
|
+
### Validations
|
71
|
+
|
72
|
+
Primitive types (e.g. :string, :integer, :timestamp, :boolean, etc) are
|
73
|
+
automatically validated.
|
74
|
+
|
75
|
+
##### Custom validations
|
76
|
+
|
77
|
+
Via :keyword or `Validator instance`
|
78
|
+
|
79
|
+
class Employee
|
80
|
+
...
|
81
|
+
attribute :name, validations: [:my_validation]
|
82
|
+
|
83
|
+
def assert_my_validation(attr)
|
84
|
+
add_error(attr, 'some error')
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
|
89
|
+
validator = Validator.new(:foo) do
|
90
|
+
['some error']
|
91
|
+
end
|
92
|
+
|
93
|
+
class Employee
|
94
|
+
...
|
95
|
+
attribute :name, validations: [validator]
|
96
|
+
|
97
|
+
|
98
|
+
## Development
|
99
|
+
|
100
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
101
|
+
|
102
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
103
|
+
|
104
|
+
## Contributing
|
105
|
+
|
106
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/typed_model.
|
107
|
+
|
108
|
+
## License
|
109
|
+
|
110
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "typed_model"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/lib/typed_model.rb
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'typed_model/types'
|
2
|
+
require 'typed_model/validators'
|
3
|
+
require 'typed_model/type_def'
|
4
|
+
|
5
|
+
module TypedModel
|
6
|
+
class AttributeDefinition
|
7
|
+
attr_reader :name, :spec, :model_validations, :mapping_key
|
8
|
+
|
9
|
+
def initialize(name:, type: nil, seq_of: nil, map_of: nil, validations: [], mapping_key: nil)
|
10
|
+
@name, @mapping_key = name, mapping_key
|
11
|
+
@model_validations = []
|
12
|
+
spec_validations = []
|
13
|
+
validations.each do |v|
|
14
|
+
if Validators.recognized?(v) || v.respond_to?(:validate)
|
15
|
+
spec_validations << v
|
16
|
+
else
|
17
|
+
model_validations << v
|
18
|
+
end
|
19
|
+
end
|
20
|
+
@spec = TypeDef.build(type: type, seq_of: seq_of, map_of: map_of, validations: spec_validations)
|
21
|
+
end
|
22
|
+
|
23
|
+
def attr_type
|
24
|
+
spec.value_type
|
25
|
+
end
|
26
|
+
|
27
|
+
def mapping_key
|
28
|
+
@mapping_key || name
|
29
|
+
end
|
30
|
+
|
31
|
+
def validate(model)
|
32
|
+
value = attr_value(model)
|
33
|
+
spec.validate(value, model.errors, name)
|
34
|
+
model_validations.each do |v|
|
35
|
+
method_name = "assert_#{v}"
|
36
|
+
if model.respond_to?(method_name)
|
37
|
+
model.send(method_name, name)
|
38
|
+
else
|
39
|
+
raise "Unrecognized validation '#{v}'"
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def to_data(model)
|
45
|
+
value = attr_value(model)
|
46
|
+
spec.to_data(value) unless value.nil?
|
47
|
+
end
|
48
|
+
|
49
|
+
def typecast_value(v)
|
50
|
+
spec.typecast_value(v)
|
51
|
+
end
|
52
|
+
|
53
|
+
def attr_value(model)
|
54
|
+
model.send(name)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
|
60
|
+
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module TypedModel
|
2
|
+
class Errors
|
3
|
+
def initialize
|
4
|
+
@errors = Hash.new do |h, k|
|
5
|
+
h[k] = []
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
def add(key, msg)
|
10
|
+
@errors[key] << msg
|
11
|
+
end
|
12
|
+
|
13
|
+
def merge!(errors, prefix)
|
14
|
+
errors.each_error do |k, msg|
|
15
|
+
add("#{prefix}/#{k}", msg)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def each_error(&blk)
|
20
|
+
@errors.each do |(k, msgs)|
|
21
|
+
msgs.each { |m| blk.call(k, m) }
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def [](k)
|
26
|
+
@errors[k]
|
27
|
+
end
|
28
|
+
|
29
|
+
def empty?
|
30
|
+
@errors.empty?
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module TypedModel
|
2
|
+
class MapOf
|
3
|
+
attr_reader :key_spec, :value_spec
|
4
|
+
|
5
|
+
def initialize(key_spec, val_spec)
|
6
|
+
@key_spec = key_spec
|
7
|
+
@value_spec = val_spec
|
8
|
+
end
|
9
|
+
|
10
|
+
def typecast_value(values)
|
11
|
+
unless values.nil?
|
12
|
+
values.each_with_object({}) do |(k, v), h|
|
13
|
+
h[key_spec.typecast_value(k)] = value_spec.typecast_value(v)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def to_data(value)
|
19
|
+
value.each_with_object({}) do |(k, v), h|
|
20
|
+
h[key_spec.to_data(k)] = value_spec.to_data(v)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def validate(value, errors, key_prefix)
|
25
|
+
unless value.nil?
|
26
|
+
value.each_pair do |k, v|
|
27
|
+
key_spec.validate(k, errors, "#{key_prefix}/keys/#{k}")
|
28
|
+
value_spec.validate(v, errors, "#{key_prefix}/#{k}")
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
require 'typed_model/model_validations'
|
2
|
+
require 'typed_model/attribute_definition'
|
3
|
+
|
4
|
+
module TypedModel
|
5
|
+
|
6
|
+
# Introduce a ModelBase class for declaring structure
|
7
|
+
# - data mapping (data -> object -> data)
|
8
|
+
# - declared types
|
9
|
+
# - declared validations
|
10
|
+
# - Nested models
|
11
|
+
#
|
12
|
+
# e.g.
|
13
|
+
# class Address
|
14
|
+
# include ModelBase
|
15
|
+
# attribute :street, type: string, validations: [:required]
|
16
|
+
# end
|
17
|
+
#
|
18
|
+
# class Employee
|
19
|
+
# include ModelBase
|
20
|
+
# attribute :name, type: :string, validations: [:required]
|
21
|
+
# attribute :primary_address, type: Address, validations: [:required]
|
22
|
+
# attribute :alternate_addresses, seq_of: Address
|
23
|
+
# end
|
24
|
+
module ModelBase
|
25
|
+
def self.included(base)
|
26
|
+
super
|
27
|
+
base.class_eval do
|
28
|
+
include ModelValidations
|
29
|
+
|
30
|
+
before_validation :validate_declared_attributes
|
31
|
+
|
32
|
+
class << self
|
33
|
+
def attribute(name, opts = {})
|
34
|
+
@declared_attributes ||= {}
|
35
|
+
attribute_def = AttributeDefinition.new(opts.merge(name: name))
|
36
|
+
@declared_attributes[name.to_sym] = attribute_def
|
37
|
+
@declared_attributes[attribute_def.mapping_key.to_sym] = attribute_def
|
38
|
+
attr_reader name
|
39
|
+
define_method "#{name}=" do |v|
|
40
|
+
instance_variable_set("@#{name}", attribute_def.typecast_value(v))
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def declared_attributes
|
45
|
+
ancestors.reverse.each_with_object({}) do |c, attributes|
|
46
|
+
attributes.merge!(c.instance_variable_get(:@declared_attributes) || {})
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def initialize(values = {})
|
54
|
+
self.attributes = values
|
55
|
+
end
|
56
|
+
|
57
|
+
def attributes=(values = {})
|
58
|
+
return if values.nil?
|
59
|
+
|
60
|
+
values.each_pair do |k, v|
|
61
|
+
if (attr_def = self.class.declared_attributes[k.to_sym])
|
62
|
+
setter = "#{attr_def.name}="
|
63
|
+
send(setter, v) if respond_to?(setter)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def validate_declared_attributes
|
69
|
+
self.class.declared_attributes.each do |(_, attr)|
|
70
|
+
attr.validate(self)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
def to_data
|
75
|
+
self.class.declared_attributes.each_with_object({}) do |(_, attr), h|
|
76
|
+
data = attr.to_data(self)
|
77
|
+
h[attr.mapping_key] = data unless data.nil?
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
@@ -0,0 +1,79 @@
|
|
1
|
+
require 'time'
|
2
|
+
require 'set'
|
3
|
+
require 'typed_model/errors'
|
4
|
+
|
5
|
+
module TypedModel
|
6
|
+
|
7
|
+
# Supplies a simple Errors facility with common validations
|
8
|
+
#
|
9
|
+
# Implementations that mix in this module are expected to plug in validations
|
10
|
+
# by overriding the `validate` method and/or adding `before_validation` hooks.
|
11
|
+
#
|
12
|
+
# Calling `valid?` will return true or false and leave errors accessible
|
13
|
+
# via `errors` which returns a Map<String,Array> of messages keyed by attribute.
|
14
|
+
#
|
15
|
+
module ModelValidations
|
16
|
+
attr_reader :errors
|
17
|
+
|
18
|
+
def self.included(base)
|
19
|
+
super
|
20
|
+
base.class_eval do
|
21
|
+
class << self
|
22
|
+
def before_validation(fname)
|
23
|
+
(@before_validation_callbacks ||= []) << fname
|
24
|
+
end
|
25
|
+
|
26
|
+
def before_validation_callbacks
|
27
|
+
ancestors.reverse.each_with_object([]) do |c, callbacks|
|
28
|
+
callbacks.concat(c.instance_variable_get(:@before_validation_callbacks) || [])
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def add_error(key, msg)
|
36
|
+
errors.add(key, msg)
|
37
|
+
end
|
38
|
+
|
39
|
+
def assert_not_blank(field)
|
40
|
+
v = send(field)
|
41
|
+
add_error(field, :required) unless v.to_s.match(/\S+/)
|
42
|
+
end
|
43
|
+
|
44
|
+
def assert_timestamp(field)
|
45
|
+
v = send(field)
|
46
|
+
if !v.is_a?(Time)
|
47
|
+
s = v.to_s
|
48
|
+
if s.match(/\S/)
|
49
|
+
send("#{field}=", Time.parse(s))
|
50
|
+
end
|
51
|
+
end
|
52
|
+
rescue
|
53
|
+
add_error(field, :invalid)
|
54
|
+
end
|
55
|
+
|
56
|
+
def valid?
|
57
|
+
self.class.before_validation_callbacks.each do |fname|
|
58
|
+
send(fname)
|
59
|
+
end
|
60
|
+
validate
|
61
|
+
errors.empty?
|
62
|
+
end
|
63
|
+
|
64
|
+
def errors
|
65
|
+
@errors ||= Errors.new
|
66
|
+
end
|
67
|
+
|
68
|
+
def each_error(&blk)
|
69
|
+
errors.each_error(&blk)
|
70
|
+
end
|
71
|
+
|
72
|
+
protected
|
73
|
+
|
74
|
+
def validate
|
75
|
+
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module TypedModel
|
2
|
+
class SeqOf
|
3
|
+
attr_reader :spec
|
4
|
+
|
5
|
+
def initialize(spec)
|
6
|
+
@spec = spec
|
7
|
+
end
|
8
|
+
|
9
|
+
def typecast_value(values)
|
10
|
+
values.map { |v| spec.typecast_value(v) }
|
11
|
+
end
|
12
|
+
|
13
|
+
def to_data(values)
|
14
|
+
values.map { |v| spec.to_data(v) }
|
15
|
+
end
|
16
|
+
|
17
|
+
def validate(value, errors, key_prefix)
|
18
|
+
unless value.nil?
|
19
|
+
value.each_with_index do |v, index|
|
20
|
+
spec.validate(v, errors, "#{key_prefix}/#{index}")
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,112 @@
|
|
1
|
+
require 'typed_model/seq_of'
|
2
|
+
require 'typed_model/map_of'
|
3
|
+
require 'typed_model/types'
|
4
|
+
require 'typed_model/validator'
|
5
|
+
|
6
|
+
module TypedModel
|
7
|
+
class TypeDef
|
8
|
+
class << self
|
9
|
+
def build(args)
|
10
|
+
spec_map = args.respond_to?(:has_key?) ? args : { type: args }
|
11
|
+
|
12
|
+
t, seq_of, map_of, validations = spec_map.values_at(:type, :seq_of, :map_of, :validations)
|
13
|
+
|
14
|
+
validators = (validations || []).map do |v|
|
15
|
+
Validator.build(v)
|
16
|
+
end
|
17
|
+
|
18
|
+
spec_opts = { type: t, validators: validators }
|
19
|
+
|
20
|
+
if seq_of
|
21
|
+
seq_spec = build(seq_of)
|
22
|
+
new(spec_opts.merge(type: SeqOf.new(seq_spec)))
|
23
|
+
elsif map_of
|
24
|
+
key_spec_opts, val_spec_opts = map_of
|
25
|
+
if val_spec_opts.is_a?(Array)
|
26
|
+
val_spec_opts = { type: :map, map_of: val_spec_opts }
|
27
|
+
end
|
28
|
+
map_of = MapOf.new(build(key_spec_opts), build(val_spec_opts))
|
29
|
+
new(spec_opts.merge(type: map_of))
|
30
|
+
else
|
31
|
+
new(spec_opts)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
attr_accessor :value_type, :validators
|
37
|
+
|
38
|
+
def initialize(values)
|
39
|
+
@value_type = values[:type]
|
40
|
+
@validators = (values[:validators] || [])
|
41
|
+
end
|
42
|
+
|
43
|
+
def typecast_value(v)
|
44
|
+
if value_type
|
45
|
+
if value_type.respond_to?(:typecast_value)
|
46
|
+
value_type.typecast_value(v)
|
47
|
+
elsif value_type.is_a?(Symbol) && Types.recognized?(value_type)
|
48
|
+
Types.typecast(value_type, v)
|
49
|
+
elsif value_type.is_a?(Class)
|
50
|
+
instantiate_type(v)
|
51
|
+
else
|
52
|
+
raise "unrecognized type '#{value_type.inspect}'"
|
53
|
+
end
|
54
|
+
else
|
55
|
+
v
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def validate(value, errors, key_prefix)
|
60
|
+
validate_recognized_types(errors, key_prefix, value)
|
61
|
+
|
62
|
+
execute_validators(errors, key_prefix, value)
|
63
|
+
|
64
|
+
if value_type.respond_to?(:validate)
|
65
|
+
value_type.validate(value, errors, key_prefix)
|
66
|
+
end
|
67
|
+
|
68
|
+
if value.respond_to?(:valid?) && value.respond_to?(:errors) && !value.valid?
|
69
|
+
errors.merge!(value.errors, key_prefix)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def to_data(value)
|
74
|
+
if value_type.respond_to?(:to_data)
|
75
|
+
value_type.to_data(value)
|
76
|
+
elsif value.respond_to?(:to_data)
|
77
|
+
value.to_data
|
78
|
+
else
|
79
|
+
value
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
private
|
84
|
+
|
85
|
+
def execute_validators(errors, key_prefix, value)
|
86
|
+
validators.each_with_object(errors) do |v, errors|
|
87
|
+
(v.validate(value) || []).each do |s|
|
88
|
+
errors.add(key_prefix, s)
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
def validate_recognized_types(errors, key_prefix, value)
|
94
|
+
if value_type.is_a?(Symbol) && Types.recognized?(value_type)
|
95
|
+
if value != nil && Types.send(value_type, value).nil?
|
96
|
+
errors.add(key_prefix, :invalid)
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
def instantiate_type(v)
|
102
|
+
if v.is_a?(value_type)
|
103
|
+
v
|
104
|
+
else
|
105
|
+
m = value_type.respond_to?(:build) ? :build : :new
|
106
|
+
value_type.send(m, v)
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
|
@@ -0,0 +1,78 @@
|
|
1
|
+
module TypedModel
|
2
|
+
|
3
|
+
# Supported Types/Type coercion
|
4
|
+
#
|
5
|
+
# Type checking methods (e.g. `boolean`, `integer`, etc..) should return
|
6
|
+
# value of type if "reasonably" coercible, otherwise nil.
|
7
|
+
#
|
8
|
+
# "Reasonable" = reasonable valid over the wire data for type. E.g. String 'true'
|
9
|
+
# is reasonable for a declared :boolean. Likewise, boolean is reasonable
|
10
|
+
# for declared string, but Hash not so much.
|
11
|
+
#
|
12
|
+
#
|
13
|
+
class Types
|
14
|
+
PRIMITIVE_CLASSES = Set.new([String, TrueClass, FalseClass, Integer, Float, Fixnum])
|
15
|
+
|
16
|
+
class << self
|
17
|
+
def recognized?(t)
|
18
|
+
respond_to?(t)
|
19
|
+
end
|
20
|
+
|
21
|
+
def typecast(sym, value)
|
22
|
+
send(sym, value) || value
|
23
|
+
end
|
24
|
+
|
25
|
+
def timestamp(v)
|
26
|
+
if v.respond_to?(:monday?)
|
27
|
+
v
|
28
|
+
else
|
29
|
+
Time.parse(v)
|
30
|
+
end
|
31
|
+
rescue
|
32
|
+
nil
|
33
|
+
end
|
34
|
+
|
35
|
+
def boolean(v)
|
36
|
+
case v
|
37
|
+
when "true", true then
|
38
|
+
true
|
39
|
+
when "false", false then
|
40
|
+
false
|
41
|
+
else
|
42
|
+
nil
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def integer(v)
|
47
|
+
Integer(v)
|
48
|
+
rescue
|
49
|
+
nil
|
50
|
+
end
|
51
|
+
|
52
|
+
def map(v)
|
53
|
+
maplike?(v) ? v : nil
|
54
|
+
end
|
55
|
+
|
56
|
+
def seq(v)
|
57
|
+
return nil if v.nil?
|
58
|
+
return v if v.is_a?(Array)
|
59
|
+
return v.to_a if v.respond_to?(:to_a) && !maplike?(v)
|
60
|
+
nil
|
61
|
+
end
|
62
|
+
|
63
|
+
def string(v)
|
64
|
+
primitive?(v) ? v.to_s : nil
|
65
|
+
end
|
66
|
+
|
67
|
+
private
|
68
|
+
|
69
|
+
def maplike?(v)
|
70
|
+
v.respond_to?(:each_pair)
|
71
|
+
end
|
72
|
+
|
73
|
+
def primitive?(v)
|
74
|
+
PRIMITIVE_CLASSES.include?(v.class)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'typed_model/validators'
|
2
|
+
|
3
|
+
module TypedModel
|
4
|
+
class Validator
|
5
|
+
class << self
|
6
|
+
def build(arg)
|
7
|
+
if arg.respond_to?(:validate) && arg.respond_to?(:name)
|
8
|
+
return arg
|
9
|
+
end
|
10
|
+
if arg.is_a?(Symbol)
|
11
|
+
if Validators.recognized?(arg)
|
12
|
+
new_builtin_validator(arg)
|
13
|
+
else
|
14
|
+
raise "Unrecognized validation '#{arg}'"
|
15
|
+
end
|
16
|
+
else
|
17
|
+
raise "failed to create validator from '#{arg}'"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def from_primitive(t)
|
22
|
+
new(t) do |v|
|
23
|
+
errors = []
|
24
|
+
if v != nil && (msg = send("assert_#{t}", v))
|
25
|
+
errors << msg
|
26
|
+
end
|
27
|
+
errors
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def new_builtin_validator(sym)
|
34
|
+
new(sym) do |v|
|
35
|
+
errors = []
|
36
|
+
if (msg = Validators.validate(sym, v))
|
37
|
+
errors << msg
|
38
|
+
end
|
39
|
+
errors
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
attr_reader :name, :f
|
45
|
+
|
46
|
+
def initialize(name, &blk)
|
47
|
+
@name = name
|
48
|
+
@f = blk
|
49
|
+
end
|
50
|
+
|
51
|
+
def validate(value)
|
52
|
+
f.call(value)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'typed_model/types'
|
2
|
+
|
3
|
+
module TypedModel
|
4
|
+
module Validators
|
5
|
+
module_function
|
6
|
+
|
7
|
+
def recognized?(sym)
|
8
|
+
respond_to?("assert_#{sym}")
|
9
|
+
end
|
10
|
+
|
11
|
+
def validate(sym, value)
|
12
|
+
method_name = "assert_#{sym}"
|
13
|
+
unless respond_to?(method_name)
|
14
|
+
raise "Unrecognized validation '#{sym}'"
|
15
|
+
end
|
16
|
+
send(method_name, value)
|
17
|
+
end
|
18
|
+
|
19
|
+
def assert_timestamp(v)
|
20
|
+
if Types.timestamp(v).nil?
|
21
|
+
:invalid
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def assert_required(v)
|
26
|
+
if v.nil? || (v.respond_to?(:empty?) && v.empty?)
|
27
|
+
:required
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
alias assert_not_blank assert_required
|
32
|
+
|
33
|
+
def assert_not_nil(v)
|
34
|
+
if v.nil?
|
35
|
+
:required_not_nil
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def assert_string(v)
|
40
|
+
if Types.string(v).nil?
|
41
|
+
:invalid
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def assert_integer(v)
|
46
|
+
if Types.integer(v).nil?
|
47
|
+
:invalid
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
data/typed_model.gemspec
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "typed_model/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "typed_model"
|
8
|
+
spec.version = TypedModel::VERSION
|
9
|
+
spec.authors = ["Lenny Marks"]
|
10
|
+
spec.email = ["lenny@lennymarks.com"]
|
11
|
+
|
12
|
+
spec.summary = 'A Ruby library for defining data schemas via classes with typed fields and built in hydration and serialization'
|
13
|
+
spec.description = 'A Ruby library for defining data schemas via classes with typed fields and built in hydration and serialization'
|
14
|
+
spec.homepage = 'https://github.com/lenny/typed_model'
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
18
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
19
|
+
if spec.respond_to?(:metadata)
|
20
|
+
spec.metadata["allowed_push_host"] = 'https://rubygems.org'
|
21
|
+
|
22
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
23
|
+
spec.metadata["source_code_uri"] = 'https://github.com/lenny/typed_model'
|
24
|
+
spec.metadata["changelog_uri"] = "https://github.com/lenny/typed_model/blob/master/CHANGELOG.md"
|
25
|
+
else
|
26
|
+
raise "RubyGems 2.0 or newer is required to protect against " \
|
27
|
+
"public gem pushes."
|
28
|
+
end
|
29
|
+
|
30
|
+
# Specify which files should be added to the gem when it is released.
|
31
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
32
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
33
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
34
|
+
end
|
35
|
+
spec.bindir = "exe"
|
36
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
37
|
+
spec.require_paths = ["lib"]
|
38
|
+
|
39
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
40
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
41
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
42
|
+
end
|
metadata
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: typed_model
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Lenny Marks
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-08-10 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.16'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.16'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
description: A Ruby library for defining data schemas via classes with typed fields
|
56
|
+
and built in hydration and serialization
|
57
|
+
email:
|
58
|
+
- lenny@lennymarks.com
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- ".rspec"
|
65
|
+
- ".travis.yml"
|
66
|
+
- Gemfile
|
67
|
+
- LICENSE
|
68
|
+
- LICENSE.txt
|
69
|
+
- README.md
|
70
|
+
- Rakefile
|
71
|
+
- bin/console
|
72
|
+
- bin/setup
|
73
|
+
- lib/typed_model.rb
|
74
|
+
- lib/typed_model/attribute_definition.rb
|
75
|
+
- lib/typed_model/errors.rb
|
76
|
+
- lib/typed_model/map_of.rb
|
77
|
+
- lib/typed_model/model_base.rb
|
78
|
+
- lib/typed_model/model_validations.rb
|
79
|
+
- lib/typed_model/seq_of.rb
|
80
|
+
- lib/typed_model/type_def.rb
|
81
|
+
- lib/typed_model/types.rb
|
82
|
+
- lib/typed_model/validator.rb
|
83
|
+
- lib/typed_model/validators.rb
|
84
|
+
- lib/typed_model/version.rb
|
85
|
+
- typed_model.gemspec
|
86
|
+
homepage: https://github.com/lenny/typed_model
|
87
|
+
licenses:
|
88
|
+
- MIT
|
89
|
+
metadata:
|
90
|
+
allowed_push_host: https://rubygems.org
|
91
|
+
homepage_uri: https://github.com/lenny/typed_model
|
92
|
+
source_code_uri: https://github.com/lenny/typed_model
|
93
|
+
changelog_uri: https://github.com/lenny/typed_model/blob/master/CHANGELOG.md
|
94
|
+
post_install_message:
|
95
|
+
rdoc_options: []
|
96
|
+
require_paths:
|
97
|
+
- lib
|
98
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '0'
|
103
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
104
|
+
requirements:
|
105
|
+
- - ">="
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
requirements: []
|
109
|
+
rubyforge_project:
|
110
|
+
rubygems_version: 2.7.8
|
111
|
+
signing_key:
|
112
|
+
specification_version: 4
|
113
|
+
summary: A Ruby library for defining data schemas via classes with typed fields and
|
114
|
+
built in hydration and serialization
|
115
|
+
test_files: []
|