dystruct 0.4.0

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: 15cdb1d06c10d11c3f0374a7de03941ac4669ab0
4
+ data.tar.gz: 2771045a37aa81359ac1973e1bd116684a237abc
5
+ SHA512:
6
+ metadata.gz: ab19fc8bb08aa374d4a6d1e2b830ad21998c00fe9457cc500d9e58af71373475de2fed4ae00015eaa55e70bcd602cfa204bdd5a381f2073798266c4dc454edbd
7
+ data.tar.gz: 78d77bdfdf9f13b15f4d4273b3c7af283b8b1c69bd12d2fd2ace78b4e5889d514dd7eb5612c19e5be06a7e3619fee802d968c7a6c6ae9b952fb81476f54ef2c2
@@ -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/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.6
4
+ before_install: gem install bundler -v 1.10.4
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in dystruct.gemspec
4
+ gemspec
5
+ gem 'pry'
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Artur Pañach
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.
@@ -0,0 +1,172 @@
1
+ # Dystruct
2
+
3
+ Better Structs for many applications.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'dystruct'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install dystruct
20
+
21
+ ## Usage
22
+
23
+ __Extended OpenStruct:__
24
+
25
+ ```ruby
26
+ context = Dystruct.new(name: 'John', surname: 'Doe')
27
+ context.name # => 'John'
28
+ context.name_provided? # => true
29
+ context.surname # => 'Doe'
30
+ context.foo_provided? # => false
31
+ context.foo_not_provided? # => true
32
+ context.foo = :bar
33
+ context.foo_provided? # => true
34
+ context.foo_not_provided? # => false
35
+ context.foo # => :bar
36
+ context.to_h # => {:name=>"John", :surname=>"Doe", :foo=>:bar}
37
+ ```
38
+
39
+ _more complex example_
40
+ ```ruby
41
+ class Input < Dystruct
42
+ permit :name, :city, :address, :phone_number, :free_text, :country_code,
43
+ :country, :zip, :types
44
+ defaults types: ['lodging']
45
+ aliases :name, :hotel_name
46
+ aliases :phone_number, :telephone
47
+
48
+ def long_name
49
+ [name, address, city].join(', ')
50
+ end
51
+
52
+ def types
53
+ Array.wrap(args[:types])
54
+ end
55
+ end
56
+
57
+ i = Input.new(name: 'Hotel', city: 'Barcelona', address: 'Happy street', not_permitted: 'dangerous')
58
+ i.types
59
+ # => ["lodging"]
60
+ i.long_name
61
+ # => "Hotel, Happy street, Barcelona"
62
+ i.name
63
+ # => "Hotel"
64
+ i.hotel_name
65
+ # => "Hotel"
66
+ i.phone_number_provided?
67
+ # => false
68
+ i.not_permitted
69
+ # => NoMethodError: undefined method
70
+ ```
71
+
72
+ ### Building better Structs
73
+
74
+ **no_method_error**
75
+ ```ruby
76
+ class Example < Dystruct
77
+ no_method_error false
78
+ end
79
+
80
+ Example.new(foo: :bar).hello # => nil
81
+ ```
82
+
83
+ ```ruby
84
+ class Example < Dystruct
85
+ no_method_error
86
+ end
87
+
88
+ Example.new(foo: :bar).hello # => => NoMethodError: undefined method
89
+ ```
90
+
91
+ **required**
92
+ ```ruby
93
+ class Example < Dystruct
94
+ required :required_arg
95
+ end
96
+
97
+ Example.new(foo: :bar)
98
+ #=> Error Dystruct::RequiredFieldNotPresent
99
+ ```
100
+
101
+ **aliases**
102
+ ```ruby
103
+ class Example < Dystruct
104
+ aliases :hello, :greeting, :welcome
105
+ end
106
+ ex = Example.new(hello: 'Hey!')
107
+ # => #<Example:0x007fd88ba30398 @args={:hello=>"Hey!"}>
108
+ ex.hello
109
+ # => "Hey!"
110
+ ex.greeting
111
+ # => "Hey!"
112
+ ex.welcome
113
+ # => "Hey!"
114
+ ```
115
+
116
+ **defaults**
117
+ ```ruby
118
+ class Example2 < Dystruct
119
+ defaults foo: :bar, bar: :foo
120
+ end
121
+ ex = Example2.new
122
+ ex.foo
123
+ # => :bar
124
+ ex.bar
125
+ # => :foo
126
+ ex.foo = :hello
127
+ ex.foo
128
+ # => :hello
129
+
130
+ ex2 = Example2.new(foo: 'something', bar: true)
131
+ ex2.foo
132
+ # => 'something'
133
+ ex2.bar
134
+ # => true
135
+ ```
136
+
137
+ **ensure_presence**
138
+ ```ruby
139
+ class EnsurePresence < Dystruct
140
+ ensure_presence :foo
141
+ end
142
+ EnsurePresence.new(hello: 'asdf')
143
+ #=> Error: Dystruct::PresenceRequired
144
+
145
+ EnsurePresence.new(foo: nil)
146
+ #=> Error: Dystruct::PresenceRequired
147
+
148
+ EnsurePresence.new(foo: '').foo #=> ""
149
+ ```
150
+
151
+ **permit**
152
+ ```ruby
153
+ per = Permit.new(foo: :bar, hello: 'Hey!', bar: 'bla', yuju: 'dangerous')
154
+ => #<Permit:0x007fd88b9dd878 @args={:foo=>:bar, :hello=>"Hey!"}>
155
+ per.foo #=> :bar
156
+ per.yuju #=> nil
157
+ ```
158
+
159
+ ## Development
160
+
161
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
162
+
163
+ 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).
164
+
165
+ ## Contributing
166
+
167
+ Bug reports and pull requests are welcome on GitHub at https://github.com/arturictus/dystruct. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
168
+
169
+
170
+ ## License
171
+
172
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "dystruct"
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
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'dystruct'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "dystruct"
8
+ spec.version = Dystruct::VERSION
9
+ spec.authors = ["Artur Pañach"]
10
+ spec.email = ["arturictus@gmail.com"]
11
+ spec.summary = %q{Structs with steroids.}
12
+ spec.description = %q{Better way to improve your data structs.}
13
+ spec.homepage = "https://www.github.com/arturictus/dystruct"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.10"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec"
24
+ end
@@ -0,0 +1,72 @@
1
+ class Dystruct
2
+ VERSION = "0.4.0"
3
+ autoload :ClassMethods, 'dystruct/class_methods'
4
+ autoload :InstanceMethods, 'dystruct/instance_methods'
5
+
6
+ class RequiredFieldNotPresent < ArgumentError; end
7
+ class PresenceRequired < ArgumentError; end
8
+ class WrongArgument < ArgumentError; end
9
+
10
+ def self.inherited(subclass)
11
+ subclass.extend ClassMethods
12
+ subclass.include InstanceMethods
13
+ end
14
+
15
+ attr_reader :attrs
16
+ alias_method :to_h, :attrs
17
+ alias_method :to_hash, :attrs
18
+
19
+ def initialize(hash = {})
20
+ unless hash.class <= Hash
21
+ fail WrongArgument, "[Dystruct ERROR]: `#{self.class}` expects to receive a `Hash` or and object having `Hash` as ancestor."
22
+ end
23
+ @attrs = hash
24
+ @attrs.each do |k, v|
25
+ define_dystruct_method(k, v)
26
+ end
27
+ end
28
+
29
+ def [](key)
30
+ attrs[key]
31
+ end
32
+
33
+ def []=(key, value)
34
+ set_attribute(key, value)
35
+ end
36
+
37
+ def method_missing(name, *args, &block)
38
+ if name =~ /\A\w+=\z/
39
+ set_attribute_macro(name, *args, &block)
40
+ elsif out = provided_macro(name)
41
+ out[:out]
42
+ else
43
+ super
44
+ end
45
+ end
46
+
47
+ private
48
+
49
+ def set_attribute_macro(name, *args, &block)
50
+ value = args.first || block
51
+ key = name.to_s.gsub('=', '').to_sym
52
+ set_attribute(key, value)
53
+ end
54
+
55
+ def provided_macro(name)
56
+ case name.to_s
57
+ when /\A\w+_not_provided\?\z/ then { out: true }
58
+ when /\A\w+_provided\?\z/ then { out: false }
59
+ end
60
+ end
61
+
62
+ def set_attribute(key, value)
63
+ attrs[key] = value
64
+ define_dystruct_method(key, value)
65
+ end
66
+
67
+ def define_dystruct_method(key, value)
68
+ define_singleton_method(key) { attrs.fetch(key) }
69
+ define_singleton_method("#{key}_provided?") { true }
70
+ define_singleton_method("#{key}_not_provided?") { false }
71
+ end
72
+ end
@@ -0,0 +1,37 @@
1
+ class Dystruct
2
+ module ClassMethods
3
+
4
+ def settings
5
+ @settings ||= {}
6
+ end
7
+
8
+ def required(*names)
9
+ settings[:required] = names.map(&:to_sym)
10
+ end
11
+
12
+ def no_method_error(input = true)
13
+ settings[:no_method_error] = input
14
+ end
15
+
16
+ def open_struct_behavior(input = true)
17
+ settings[:no_method_error] = !input
18
+ end
19
+
20
+ def ensure_presence(*names)
21
+ settings[:presence_required] = names.map(&:to_sym)
22
+ end
23
+
24
+ def aliases(*names)
25
+ settings[:equivalents] ||= []
26
+ settings[:equivalents] << names.map(&:to_sym)
27
+ end
28
+
29
+ def defaults(hash)
30
+ settings[:defaults] = hash
31
+ end
32
+
33
+ def permit(*names)
34
+ settings[:permitted] = names.map(&:to_sym)
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,95 @@
1
+ class Dystruct
2
+ module InstanceMethods
3
+ def initialize(hash = {})
4
+ check_input_errors(hash)
5
+ hash = hash.select{|k, v| _permitted.include?(k.to_sym) } if _only_permitted?
6
+ @attrs = _defaults.merge(hash)
7
+ attrs.each do |k, v|
8
+ define_dystruct_method(k, v)
9
+ end
10
+ end
11
+
12
+ def method_missing(name, *args, &block)
13
+ if ary = find_in_equivalents(name)
14
+ _from_equivalents(ary)
15
+ elsif name =~ /\A\w+=\z/
16
+ set_attribute_macro(name, *args, &block)
17
+ else
18
+ out = provided_macro(name)
19
+ return out[:out] if out
20
+ if _no_method_error
21
+ super
22
+ # raise NoMethodError, "Method not found for #{self.class}: `#{name}`"
23
+ end
24
+ end
25
+ end
26
+
27
+ def find_in_equivalents(name)
28
+ found = nil
29
+ _equivalents.each do |ary|
30
+ found = ary if ary.include?(name.to_sym)
31
+ break if found
32
+ end
33
+ found
34
+ end
35
+
36
+ def _from_equivalents(ary)
37
+ out = nil
38
+ ary.each do |method|
39
+ out = attrs[method.to_sym]
40
+ break if out
41
+ end
42
+ out
43
+ end
44
+
45
+ def _only_permitted?
46
+ _permitted.any?
47
+ end
48
+
49
+ def _permitted
50
+ _get_config[:permitted] || []
51
+ end
52
+
53
+ def _equivalents
54
+ _get_config[:equivalents] || []
55
+ end
56
+
57
+ def _presence_required
58
+ _get_config[:presence_required] || []
59
+ end
60
+
61
+ def _no_method_error
62
+ _get_config[:no_method_error].nil? ? true : _get_config[:no_method_error]
63
+ end
64
+
65
+ def _defaults
66
+ _get_config[:defaults] || {}
67
+ end
68
+
69
+ def _required_args
70
+ _get_config[:required] || []
71
+ end
72
+
73
+ def _get_config
74
+ self.class.settings
75
+ end
76
+
77
+ def check_input_errors(hash)
78
+ unless hash.class <= Hash
79
+ fail WrongArgument, "[Dystruct ERROR]: `#{self.class}` expects to receive a `Hash` or and object having `Hash` as ancestor."
80
+ end
81
+
82
+ _required_args.map(&:to_sym).each do |r|
83
+ unless hash.keys.map(&:to_sym).include?(r)
84
+ fail RequiredFieldNotPresent, "[Dystruct ERROR]: `#{self.class}` expect to be initialized with `#{r}` as an attribute."
85
+ end
86
+ end
87
+
88
+ _presence_required.map(&:to_sym).each do |r|
89
+ if hash[r].nil?
90
+ fail PresenceRequired, "[Dystruct ERROR]: `#{self.class}` expects to receive an attribute named `#{r}` not beeing `nil`"
91
+ end
92
+ end
93
+ end
94
+ end
95
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dystruct
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.0
5
+ platform: ruby
6
+ authors:
7
+ - Artur Pañach
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-10-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.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
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: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Better way to improve your data structs.
56
+ email:
57
+ - arturictus@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - CODE_OF_CONDUCT.md
66
+ - Gemfile
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - bin/console
71
+ - bin/setup
72
+ - dystruct.gemspec
73
+ - lib/dystruct.rb
74
+ - lib/dystruct/class_methods.rb
75
+ - lib/dystruct/instance_methods.rb
76
+ homepage: https://www.github.com/arturictus/dystruct
77
+ licenses:
78
+ - MIT
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 2.2.3
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: Structs with steroids.
100
+ test_files: []