d_struct 0.1.0

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: 6322dce3ef90ced40580ed199de3b4bd7a956f54
4
+ data.tar.gz: db08338cebec0db8b7a0514ea4f6ed294159fed6
5
+ SHA512:
6
+ metadata.gz: d46d82dfbdd681acf51542621dad92fd425f7e4a202c5e29e0f8d1615ac7c08d41c76d3ac105c2f3a892458036f3ff61993eb33d485720c5749568bb5e79c59c
7
+ data.tar.gz: 65c7277fd77a7e87cd66172183d5416c70952c70998b27ee431b737e7b2dca86d8997f0116baa081b711447cf4b60f950de6b52baa309436520876bd1993b813
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.4
4
+ before_install: gem install bundler -v 1.11.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in d_struct.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Damir Roso
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,66 @@
1
+ # DStruct
2
+
3
+ ## Usage
4
+
5
+ ```ruby
6
+ # configure attribute types
7
+ class MyStruct < DStruct::DStruct
8
+ attributes strings: [:string], integers: [:int], booleans: [:bool], arrays: [:arr]
9
+ end
10
+
11
+ # define dry-validation schema - http://dry-rb.org/gems/dry-validation/
12
+ MyValidationSchema = Dry::Validation.Schema do
13
+ key(:string).required(:str?)
14
+ key(:int).required(:int?)
15
+ key(:bool).required(:bool?)
16
+ key(:arr).required(:array?)
17
+ end
18
+
19
+ # initialize with hash
20
+ struct = MyStruct.new({string: 'abc', int: 123, bool: true, arr: [1]})
21
+ struct.add_validation_schema MyValidationSchema
22
+ puts struct.to_h # => {string: 'abc', int: 123, bool: true, arr: [1]}
23
+ puts struct.valid? # => true
24
+ puts struct.errors # => {}
25
+
26
+ # unknown key
27
+ struct = MyStruct.new({unknown_key: '123'})
28
+ puts struct.valid? # => false
29
+ puts struct.errors # => {unknown_key: 'unknown key'}
30
+
31
+ # missing keys
32
+ struct = MyStruct.new({})
33
+ puts struct.valid? # => false
34
+ puts struct.errors # => {:string=>["is missing"], :int=>["is missing"], :bool=>["is missing"], :arr=>["is missing"]}
35
+
36
+ # invalid values
37
+ struct = MyStruct.new({string: nil, int: nil, bool: nil, arr: nil})
38
+ puts struct.valid? # => false
39
+ puts struct.errors # => {:string=>["must be filled"], :int=>["must be filled"], :bool=>["must be filled"], :arr=>["must be filled"]}
40
+ ```
41
+
42
+ ## Installation
43
+
44
+ Add this line to your application's Gemfile:
45
+
46
+ ```ruby
47
+ gem 'd_struct'
48
+ ```
49
+
50
+ And then execute:
51
+
52
+ $ bundle
53
+
54
+ Or install it yourself as:
55
+
56
+ $ gem install d_struct
57
+
58
+ ## Contributing
59
+
60
+ Bug reports and pull requests are welcome on GitHub at https://github.com/damir/d_struct.
61
+
62
+
63
+ ## License
64
+
65
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
66
+
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "d_struct"
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
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/d_struct.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'd_struct/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "d_struct"
8
+ spec.version = DStruct::VERSION
9
+ spec.authors = ["Damir Roso"]
10
+ spec.email = ["damir.roso@webteh.hr"]
11
+
12
+ spec.summary = %q{Struct with type casting and validations}
13
+ spec.description = %q{Uses dry-validation}
14
+ spec.homepage = "https://github.com/damir/d_struct"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.11"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "minitest", "~> 5.0"
25
+
26
+ spec.add_dependency "dry-validation"
27
+ end
@@ -0,0 +1,84 @@
1
+ require 'dry-validation'
2
+
3
+ module DStruct
4
+ class DStruct
5
+
6
+ attr_reader :to_h
7
+ attr_accessor :validation_schema
8
+
9
+ def self.attributes(attributes_hash)
10
+ @attributes_readers = attributes_hash.values.flatten
11
+ @string_attributes = attributes_hash[:strings] || []
12
+ @integer_attributes = attributes_hash[:integers] || []
13
+ @booleans_attributes = attributes_hash[:booleans] || []
14
+ @array_attributes = attributes_hash[:arrays] || []
15
+
16
+ # generate readers
17
+ attr_reader *@attributes_readers
18
+
19
+ # generate writers
20
+ @string_attributes.each do |string_attr|
21
+ define_method "#{string_attr}=" do |str_arg|
22
+ value = str_arg.to_s
23
+ instance_variable_set("@#{string_attr}", str_arg)
24
+ @to_h[string_attr] = value
25
+ end
26
+ end
27
+
28
+ @integer_attributes.each do |int_attr|
29
+ define_method "#{int_attr}=" do |int_arg|
30
+ value = (Integer(int_arg) rescue nil)
31
+ instance_variable_set("@#{int_attr}", value)
32
+ @to_h[int_attr] = value
33
+ end
34
+ end
35
+
36
+ @booleans_attributes.each do |boolean_attr|
37
+ define_method "#{boolean_attr}=" do |boolean_arg|
38
+ value = case boolean_arg
39
+ when TrueClass then true
40
+ when FalseClass then false
41
+ else nil
42
+ end
43
+ instance_variable_set("@#{boolean_attr}", value)
44
+ @to_h[boolean_attr] = value
45
+ end
46
+ end
47
+
48
+ @array_attributes.each do |arr_attr|
49
+ define_method "#{arr_attr}=" do |arr_arg|
50
+ value = (Array(arr_arg) rescue nil)
51
+ instance_variable_set("@#{arr_attr}", value)
52
+ @to_h[arr_attr] = value
53
+ end
54
+ end
55
+ end
56
+
57
+ def add_validation_schema(schema)
58
+ @validation_schema = schema
59
+ end
60
+
61
+ def initialize(attributes_hash)
62
+ @to_h = {}
63
+ attributes_hash.each do |k,v|
64
+ begin
65
+ send("#{k}=", v)
66
+ rescue # unknown key
67
+ @errors = {k => 'unknown key'}
68
+ break
69
+ end
70
+ end
71
+ end
72
+
73
+ def errors
74
+ return @errors if @errors # unknown key
75
+ return {} unless validation_schema # no schema
76
+ @errors ||= validation_schema.call(to_h).messages
77
+ end
78
+
79
+ def valid?
80
+ errors.empty?
81
+ end
82
+
83
+ end
84
+ end
@@ -0,0 +1,3 @@
1
+ module DStruct
2
+ VERSION = "0.1.0"
3
+ end
data/lib/d_struct.rb ADDED
@@ -0,0 +1,6 @@
1
+ require "d_struct/version"
2
+ require "d_struct/d_struct"
3
+
4
+ module DStruct
5
+ # Your code goes here...
6
+ end
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: d_struct
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Damir Roso
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-03-18 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.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
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: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: dry-validation
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
+ description: Uses dry-validation
70
+ email:
71
+ - damir.roso@webteh.hr
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".travis.yml"
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - bin/console
83
+ - bin/setup
84
+ - d_struct.gemspec
85
+ - lib/d_struct.rb
86
+ - lib/d_struct/d_struct.rb
87
+ - lib/d_struct/version.rb
88
+ homepage: https://github.com/damir/d_struct
89
+ licenses:
90
+ - MIT
91
+ metadata: {}
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 2.4.8
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: Struct with type casting and validations
112
+ test_files: []