dto 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6e7ef0812d9d0b60562ca56ddd118e0ee4d206fb
4
+ data.tar.gz: f960ebf636e780d4cacffb11abdd7d9c02991308
5
+ SHA512:
6
+ metadata.gz: 4bec3d82ece2c50a52d00fcec87d3346f85f4921cd65608747a4b44e2c1581af417c41137b69021ca6f5a7a3c54a079719574d48b25bf5eb0c29a7c0c370159f
7
+ data.tar.gz: dcc38b8db0d0a72e0960d03275b6ad4ce22396dc983e21a7c34b6d81177463cb3cced7d7789293d022469a563ce4165adcd4987da231a5275bfd0c360b7f8bea
@@ -0,0 +1,50 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /spec/examples.txt
9
+ /test/tmp/
10
+ /test/version_tmp/
11
+ /tmp/
12
+
13
+ # Used by dotenv library to load environment variables.
14
+ # .env
15
+
16
+ ## Specific to RubyMotion:
17
+ .dat*
18
+ .repl_history
19
+ build/
20
+ *.bridgesupport
21
+ build-iPhoneOS/
22
+ build-iPhoneSimulator/
23
+
24
+ ## Specific to RubyMotion (use of CocoaPods):
25
+ #
26
+ # We recommend against adding the Pods directory to your .gitignore. However
27
+ # you should judge for yourself, the pros and cons are mentioned at:
28
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
29
+ #
30
+ # vendor/Pods/
31
+
32
+ ## Documentation cache and generated files:
33
+ /.yardoc/
34
+ /_yardoc/
35
+ /doc/
36
+ /rdoc/
37
+
38
+ ## Environment normalization:
39
+ /.bundle/
40
+ /vendor/bundle
41
+ /lib/bundler/man/
42
+
43
+ # for a library or gem, you might want to ignore these files since the code is
44
+ # intended to run in multiple environments; otherwise, check them in:
45
+ # Gemfile.lock
46
+ # .ruby-version
47
+ # .ruby-gemset
48
+
49
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
50
+ .rvmrc
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,34 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ dto (0.0.1)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ byebug (9.1.0)
10
+ diff-lcs (1.3)
11
+ rspec (3.7.0)
12
+ rspec-core (~> 3.7.0)
13
+ rspec-expectations (~> 3.7.0)
14
+ rspec-mocks (~> 3.7.0)
15
+ rspec-core (3.7.0)
16
+ rspec-support (~> 3.7.0)
17
+ rspec-expectations (3.7.0)
18
+ diff-lcs (>= 1.2.0, < 2.0)
19
+ rspec-support (~> 3.7.0)
20
+ rspec-mocks (3.7.0)
21
+ diff-lcs (>= 1.2.0, < 2.0)
22
+ rspec-support (~> 3.7.0)
23
+ rspec-support (3.7.0)
24
+
25
+ PLATFORMS
26
+ ruby
27
+
28
+ DEPENDENCIES
29
+ byebug
30
+ dto!
31
+ rspec
32
+
33
+ BUNDLED WITH
34
+ 1.16.0
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2017 Nikita Kononov
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.
@@ -0,0 +1,31 @@
1
+ # Ruby DTO
2
+ Dumb ruby object, similar a Hash, but provide access to attributes by isntance methods
3
+ ## Instalation
4
+ ```ruby
5
+ gem 'dto', git: 'https://github.com/vocrsz/dto'
6
+ ```
7
+
8
+ ## Usage
9
+ ```ruby
10
+ class UserDTO < DTO::Base
11
+ attrs :name, :email, :age
12
+ end
13
+ ```
14
+
15
+ ```ruby
16
+ user_hash = {
17
+ name: 'Nikita',
18
+ email: 'hello@world.com',
19
+ age: 24,
20
+ some_other_attribute: 'no matter'
21
+ }
22
+
23
+ user = UserDTO.new(user_hash)
24
+ # or UserDTO(name: 'Nikita', email: 'hello@world.com')
25
+ ```
26
+ ```ruby
27
+ user.name # "Nikita"
28
+ user.email # "hello@world.ru"
29
+
30
+ user.name = "Not Nikita" # "Not Nikita"
31
+ ```
@@ -0,0 +1,19 @@
1
+ require_relative 'lib/dto/version'
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = 'dto'
5
+ s.version = DTO::VERSION
6
+ s.summary = "Data Transfer Object"
7
+ s.description = "Ruby simple DTO class"
8
+ s.authors = ["Droid Labs", "VocrSz"]
9
+ s.email = 'vocrsz@gmail.com'
10
+
11
+ s.files = `git ls-files`.split($/)
12
+ s.test_files = s.files.grep(%r{^(spec)/})
13
+
14
+ s.add_development_dependency "byebug"
15
+ s.add_development_dependency "rspec"
16
+
17
+ s.homepage = 'https://github.com/vocrsz/dto'
18
+ s.license = 'MIT'
19
+ end
@@ -0,0 +1,5 @@
1
+ module DTO
2
+ end
3
+
4
+ require 'dto/version'
5
+ require 'dto/base'
@@ -0,0 +1,55 @@
1
+ class DTO::Base
2
+ def initialize(data)
3
+ @data = data
4
+ end
5
+
6
+ def to_hash
7
+ @data.dup
8
+ end
9
+
10
+ def self.attrs(*properties)
11
+ not_allowed_methods = self.public_instance_methods & properties
12
+
13
+ raise ArgumentError, "Attribute #{not_allowed_methods} is already in use by any instance of parent class! Use other unreserved name." if not_allowed_methods.any?
14
+
15
+ properties.each { |name| define_attribute(name) }
16
+
17
+ class_eval %Q(
18
+ def initialize(data)
19
+ @data = data.select { |key, _| self.class.properties.include?(key) }
20
+ end
21
+ )
22
+ end
23
+
24
+ def self.properties
25
+ @properties ||= []
26
+ end
27
+
28
+ def self.define_attribute(name)
29
+ properties.push(name)
30
+
31
+ define_method(name) do
32
+ get_attribute(name)
33
+ end
34
+
35
+ define_method("#{name}=") do |value = nil|
36
+ set_attribute(name, value)
37
+ end
38
+ end
39
+
40
+ private
41
+
42
+ def get_attribute(name)
43
+ @data[name]
44
+ end
45
+
46
+ def set_attribute(name, value)
47
+ raise ArgumentError, "Property #{name} is not defined for #{self.class} class!" unless property_defined?(name)
48
+
49
+ @data[name] = value
50
+ end
51
+
52
+ def property_defined?(name)
53
+ @data.keys.include?(name)
54
+ end
55
+ end
@@ -0,0 +1,3 @@
1
+ module DTO
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,61 @@
1
+ require 'spec_helper'
2
+
3
+ describe DTO::Base do
4
+ let(:dto_class) {
5
+ Class.new(DTO::Base) do
6
+ attrs :test, :example, :sample
7
+ end
8
+ }
9
+ let(:dto) { dto_class.new(test: 'test', example: 1, sample: false) }
10
+ let(:dto_hash) { { test: 'test', hello: :world } }
11
+
12
+ context ':attrs' do
13
+ context 'for methods already in use' do
14
+ it 'raises ArgumentError' do
15
+ expect {
16
+ dto_class.attrs :class
17
+ }.to raise_error(ArgumentError, "Attribute [:class] is already in use by any instance of parent class! Use other unreserved name.")
18
+ end
19
+ end
20
+ end
21
+
22
+ context '#initialize' do
23
+ context 'from keyword arguments' do
24
+ it 'init new instance with passed attributes' do
25
+ expect(dto.test).to eq('test')
26
+ expect(dto.example).to eq(1)
27
+ expect(dto.sample).to be(false)
28
+ end
29
+ end
30
+
31
+ context 'from hash' do
32
+ let(:dto_from_hash) { dto_class.new(dto_hash) }
33
+
34
+ it 'uses only allowed params' do
35
+ expect(dto_from_hash.test).to eq('test')
36
+ end
37
+
38
+ it 'returns nil from unset params' do
39
+ expect(dto_from_hash.example).to be_nil
40
+ end
41
+
42
+ it 'not affect to other instances of class' do
43
+ dto_from_hash.example
44
+
45
+ expect(dto.example).to eq(1)
46
+ end
47
+ end
48
+ end
49
+
50
+ context 'when update attributes' do
51
+ it 'not raises error' do
52
+ expect { dto.test = 'new_value' }.to_not raise_error
53
+ end
54
+
55
+ it 'sets new values' do
56
+ dto.test = 'new_value'
57
+
58
+ expect(dto.test).to eq('new_value')
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,5 @@
1
+ $LOAD_PATH.unshift(File.join(__dir__, '..', 'lib'))
2
+ $LOAD_PATH.unshift(__dir__)
3
+
4
+ require 'dto'
5
+ require 'byebug'
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dto
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Droid Labs
8
+ - VocrSz
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2017-12-13 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: byebug
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: '0'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: '0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rspec
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ description: Ruby simple DTO class
43
+ email: vocrsz@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - Gemfile
50
+ - Gemfile.lock
51
+ - LICENSE
52
+ - README.md
53
+ - dto.gemspec
54
+ - lib/dto.rb
55
+ - lib/dto/base.rb
56
+ - lib/dto/version.rb
57
+ - spec/dto/base_spec.rb
58
+ - spec/spec_helper.rb
59
+ homepage: https://github.com/vocrsz/dto
60
+ licenses:
61
+ - MIT
62
+ metadata: {}
63
+ post_install_message:
64
+ rdoc_options: []
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ requirements: []
78
+ rubyforge_project:
79
+ rubygems_version: 2.6.14
80
+ signing_key:
81
+ specification_version: 4
82
+ summary: Data Transfer Object
83
+ test_files:
84
+ - spec/dto/base_spec.rb
85
+ - spec/spec_helper.rb