evt-schema 0.6.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c467a08693a41abecf0d00cece99cee5432a5c95
4
+ data.tar.gz: f8a24c5de56aa58868ccd13ee6719e089e9b7ce1
5
+ SHA512:
6
+ metadata.gz: 1ed970585e37072610ba4a6d2dc17362faeb3d867344c025bdbe06eb7adf96f26a0b90860355c51504926c52f08c7e8f010f7b50e9bf1dbf8f68ada62153a8d9
7
+ data.tar.gz: 65811f0afcd628e9fcb44e189fc86ad6cf0e24c8f2a4245c0d05049cb10604624a285cdbf30bae66bcacb5c76108c1bea6a211df86981779b6a52c14aa1eeeaa
@@ -0,0 +1,53 @@
1
+ module Schema
2
+ module Controls
3
+ module DataStructure
4
+ def self.example
5
+ example = Example.new
6
+ example.some_attribute = 'some value'
7
+ example
8
+ end
9
+
10
+ def self.ancestors
11
+ example.class.ancestors
12
+ end
13
+
14
+ def self.attributes
15
+ example.attributes
16
+ end
17
+
18
+ def self.hash
19
+ example.to_h
20
+ end
21
+
22
+ class Example
23
+ include ::Schema::DataStructure
24
+ attribute :some_attribute
25
+ end
26
+
27
+ module ExtraAttributes
28
+ def self.data
29
+ {
30
+ :some_attribute => 'some value',
31
+ :some_other_attribute => 'some other value'
32
+ }
33
+ end
34
+ end
35
+
36
+ module ConfigureDependencies
37
+ def self.example
38
+ Example.build
39
+ end
40
+
41
+ class Example
42
+ include ::Schema::DataStructure
43
+
44
+ attr_accessor :some_dependency
45
+
46
+ def configure
47
+ self.some_dependency = :set
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,126 @@
1
+ module Schema
2
+ module Controls
3
+ module Schema
4
+ def self.example
5
+ example = Example.new
6
+ example.some_attribute = 'some value'
7
+ example.some_other_attribute = 'some other value'
8
+ example
9
+ end
10
+
11
+ def self.other_example
12
+ example = OtherExample.new
13
+ example.some_attribute = 'some value'
14
+ example.some_other_attribute = 'some other value'
15
+ example
16
+ end
17
+
18
+ def self.ancestors
19
+ example.class.ancestors
20
+ end
21
+
22
+ def self.attributes
23
+ example.attributes
24
+ end
25
+
26
+ def self.hash
27
+ example.to_h
28
+ end
29
+
30
+ class Example
31
+ include ::Schema
32
+ attribute :some_attribute
33
+ attribute :some_other_attribute
34
+ end
35
+
36
+ class OtherExample < Example
37
+ end
38
+
39
+ module Equivalent
40
+ def self.example
41
+ example = Example.new
42
+ example.some_attribute = 'some value'
43
+ example.yet_another_attribute = 'some other value'
44
+ example
45
+ end
46
+
47
+ class Example
48
+ include ::Schema
49
+ attribute :some_attribute
50
+ attribute :yet_another_attribute
51
+ end
52
+ end
53
+
54
+ module DefaultValue
55
+ class Example
56
+ include ::Schema
57
+ attribute :some_attribute, default: 'some default value'
58
+ end
59
+
60
+ module Proc
61
+ class Example
62
+ include ::Schema
63
+ attribute :some_attribute, default: proc { 'some default value' }
64
+ end
65
+ end
66
+ end
67
+
68
+ module Typed
69
+ class SomeType
70
+ end
71
+
72
+ class SomeSubtype < SomeType
73
+ end
74
+
75
+ class Example
76
+ include ::Schema
77
+ attribute :some_attribute, SomeType
78
+ end
79
+
80
+ module Strict
81
+ class Example
82
+ include ::Schema
83
+ attribute :some_attribute, SomeType, strict: true
84
+ end
85
+ end
86
+ end
87
+
88
+ module Duplicate
89
+ def self.example
90
+ Example.new
91
+ end
92
+
93
+ class Example
94
+ include ::Schema
95
+
96
+ attribute :some_attribute, Numeric
97
+ attribute :some_attribute, String
98
+ end
99
+ end
100
+
101
+ module Validation
102
+ def self.example
103
+ Example.new
104
+ end
105
+
106
+ def self.errors
107
+ [
108
+ "some_attribute can't be nil",
109
+ "some_other_attribute can't be nil"
110
+ ]
111
+ end
112
+
113
+ class Example
114
+ include ::Schema
115
+
116
+ attribute :some_attribute
117
+ attribute :some_other_attribute
118
+
119
+ module Validator
120
+ extend ::Schema::Validation::NilAttributes
121
+ end
122
+ end
123
+ end
124
+ end
125
+ end
126
+ end
@@ -0,0 +1,2 @@
1
+ require 'schema/controls/schema'
2
+ require 'schema/controls/data_structure'
@@ -0,0 +1,36 @@
1
+ module Schema
2
+ module DataStructure
3
+ def self.included(cls)
4
+ cls.class_exec do
5
+ include Schema
6
+ extend Build
7
+ extend Virtual::Macro
8
+
9
+ virtual :configure
10
+ virtual :configure_dependencies do
11
+ configure
12
+ end
13
+ end
14
+ end
15
+
16
+ module Build
17
+ def build(data=nil, strict=nil)
18
+ data ||= {}
19
+ strict ||= false
20
+
21
+ new.tap do |instance|
22
+ set_attributes(instance, data, strict)
23
+ instance.configure_dependencies
24
+ end
25
+ end
26
+
27
+ def set_attributes(instance, data, strict)
28
+ begin
29
+ SetAttributes.(instance, data, strict: strict)
30
+ rescue SetAttributes::Attribute::Error => e
31
+ raise Schema::Error, e.message
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,26 @@
1
+ module Schema
2
+ module Assertions
3
+ def attributes_equal?(other, attributes=nil, print: nil)
4
+ attributes ||= self.class.attribute_names
5
+
6
+ print = true if print.nil?
7
+ print ||= false
8
+
9
+ equal = self.eql?(other, attributes, ignore_class: true)
10
+
11
+ if !equal && print
12
+ attributes = Array(attributes)
13
+
14
+ require 'pp'
15
+ puts "self: #{self.class.name}"
16
+ pp self.attributes.select { |k, v| attributes.include? k }
17
+ puts "other #{other.class.name}:"
18
+ pp other.attributes.select { |k, v| attributes.include? k }
19
+ puts "attributes:"
20
+ attributes.each { |a| puts a.inspect}
21
+ end
22
+
23
+ equal
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,16 @@
1
+ module Schema
2
+ class Attribute
3
+ class Error < Schema::Error; end
4
+ class TypeError < Schema::Attribute::Error; end
5
+
6
+ attr_reader :name
7
+ attr_reader :type
8
+ attr_reader :strict
9
+
10
+ def initialize(name, type, strict)
11
+ @name = name
12
+ @type = type
13
+ @strict = strict
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,138 @@
1
+ module Schema
2
+ class Error < RuntimeError; end
3
+
4
+ def self.included(cls)
5
+ cls.class_exec do
6
+ extend AttributeMacro
7
+ extend AttributeList
8
+ end
9
+ end
10
+
11
+ module AttributeMacro
12
+ def attribute_macro(attr_name, type=nil, strict: nil, default: nil)
13
+ if type.nil? && !strict.nil?
14
+ raise Schema::Attribute::Error, "The \"#{attr_name}\" attribute is declared with the \"strict\" option but a type is not specified"
15
+ end
16
+
17
+ strict ||= false
18
+ check = nil
19
+
20
+ unless type.nil?
21
+ check = proc do |val|
22
+ unless val.nil?
23
+ if strict
24
+ raise Schema::Attribute::TypeError, "#{val.inspect} is not an instance of #{type.name}" unless val.instance_of? type
25
+ else
26
+ raise Schema::Attribute::TypeError, "#{val.inspect} is not a kind of #{type.name}" unless val.is_a? type
27
+ end
28
+ end
29
+ end
30
+ end
31
+
32
+ initialize_value = nil
33
+ if default.is_a? Proc
34
+ initialize_value = default
35
+ elsif !default.nil?
36
+ initialize_value = proc { default }
37
+ end
38
+
39
+ ::Attribute::Define.(self, attr_name, :accessor, check: check, &initialize_value)
40
+
41
+ attribute = attributes.register(attr_name, type, strict)
42
+ attribute
43
+ end
44
+ alias :attribute :attribute_macro
45
+ end
46
+
47
+ module AttributeList
48
+ def attributes
49
+ @attributes ||= AttributeRegistry.new
50
+ end
51
+
52
+ def attribute_names
53
+ attributes.map { |attribute| attribute.name }
54
+ end
55
+ end
56
+
57
+ class AttributeRegistry
58
+ def entries
59
+ @entries ||= []
60
+ end
61
+
62
+ def length
63
+ entries.length
64
+ end
65
+ alias :count :length
66
+
67
+ def [](index)
68
+ entries[index]
69
+ end
70
+
71
+ def map(&action)
72
+ entries.map(&action)
73
+ end
74
+
75
+ def each_with_object(obj, &action)
76
+ entries.each_with_object(obj, &action)
77
+ end
78
+
79
+ def add(name, type, strict=nil)
80
+ strict ||= false
81
+ attribute = Schema::Attribute.new(name, type, strict)
82
+ entries << attribute
83
+ attribute
84
+ end
85
+
86
+ def register(name, type, strict=nil)
87
+ remove(name)
88
+ add(name, type, strict)
89
+ end
90
+
91
+ def remove(name)
92
+ entries.delete_if { |entry| entry.name == name }
93
+ end
94
+
95
+ def attribute(name)
96
+ entries.find { |entry| entry.name == name }
97
+ end
98
+
99
+ def attribute?(name)
100
+ !attribute(name).nil?
101
+ end
102
+ end
103
+
104
+ def attributes
105
+ self.class.attributes.each_with_object({}) do |attribute, attributes|
106
+ attributes[attribute.name] = public_send(attribute.name)
107
+ end
108
+ end
109
+ alias :to_h :attributes
110
+
111
+ def ==(other, attributes=nil, ignore_class: nil)
112
+ attributes ||= self.class.attribute_names
113
+ attributes = Array(attributes)
114
+
115
+ ignore_class = false if ignore_class.nil?
116
+
117
+ if !ignore_class
118
+ return false if self.class != other.class
119
+ end
120
+
121
+ attributes.each do |attribute|
122
+ if attribute.is_a? Hash
123
+ this_attribute, other_attribute = attribute.keys.first, attribute.values.first
124
+ else
125
+ this_attribute, other_attribute = attribute, attribute
126
+ end
127
+
128
+ return false if public_send(this_attribute) != other.public_send(other_attribute)
129
+ end
130
+
131
+ true
132
+ end
133
+ alias :eql? :==
134
+
135
+ def ===(other)
136
+ self.eql?(other, ignore_class: true)
137
+ end
138
+ end
@@ -0,0 +1,6 @@
1
+ module Schema
2
+ module Validation
3
+ class Error < RuntimeError
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,21 @@
1
+ module Schema
2
+ module Validation
3
+ module NilAttributes
4
+ def nil_attributes
5
+ Validator
6
+ end
7
+
8
+ module Validator
9
+ def self.call(schema, errors=nil)
10
+ errors ||= []
11
+ schema.class.attribute_names.each do |attribute|
12
+ if schema.public_send(attribute).nil?
13
+ errors << "#{attribute} can't be nil"
14
+ end
15
+ end
16
+ errors.empty?
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
data/lib/schema.rb ADDED
@@ -0,0 +1,11 @@
1
+ require 'attribute'
2
+ require 'set_attributes'
3
+ require 'validate'
4
+ require 'virtual'
5
+
6
+ require 'schema/schema'
7
+ require 'schema/schema/assertions'
8
+ require 'schema/schema/attribute'
9
+ require 'schema/data_structure'
10
+ require 'schema/validation/nil_attributes'
11
+ require 'schema/validation/error'
metadata ADDED
@@ -0,0 +1,123 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: evt-schema
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.6.0.2
5
+ platform: ruby
6
+ authors:
7
+ - The Eventide Project
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-12-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: evt-attribute
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: evt-set_attributes
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: evt-validate
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: evt-virtual
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: ntl-test_bench
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: " "
84
+ email: opensource@eventide-project.org
85
+ executables: []
86
+ extensions: []
87
+ extra_rdoc_files: []
88
+ files:
89
+ - lib/schema.rb
90
+ - lib/schema/controls.rb
91
+ - lib/schema/controls/data_structure.rb
92
+ - lib/schema/controls/schema.rb
93
+ - lib/schema/data_structure.rb
94
+ - lib/schema/schema.rb
95
+ - lib/schema/schema/assertions.rb
96
+ - lib/schema/schema/attribute.rb
97
+ - lib/schema/validation/error.rb
98
+ - lib/schema/validation/nil_attributes.rb
99
+ homepage: https://github.com/eventide-project/schema
100
+ licenses:
101
+ - MIT
102
+ metadata: {}
103
+ post_install_message:
104
+ rdoc_options: []
105
+ require_paths:
106
+ - lib
107
+ required_ruby_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: 2.3.3
112
+ required_rubygems_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ requirements: []
118
+ rubyforge_project:
119
+ rubygems_version: 2.5.2
120
+ signing_key:
121
+ specification_version: 4
122
+ summary: Primitives for schema and structure
123
+ test_files: []