dry-behaviour 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: 9e7a01d677751e7f1cdf88e273a82ae180e1db9b
4
+ data.tar.gz: 815e8290f586e7d69407ab1ac6106f05ae044de1
5
+ SHA512:
6
+ metadata.gz: 79b23ad0703c404ce7094b2cdabd164ca0a5a4fa3331190b8198e08e4ea48692a4910a6dbabd39d8d0dab8296556f8e9939c46d72800d78c7dc04bf4d79b42d4
7
+ data.tar.gz: 122a43832c1aa251ee832d2dbdef28c84034175170deb646afae0cf5d66df1a6f0e74b7a7fec70ef911dee9aaf1c4a3e310b5f41dbcf1f588b7a7fc9b6db3436
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/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.rubocop.yml ADDED
@@ -0,0 +1 @@
1
+ inherit_from: .rubocop_todo.yml
data/.rubocop_todo.yml ADDED
@@ -0,0 +1,99 @@
1
+ # This configuration was generated by
2
+ # `rubocop --auto-gen-config`
3
+ # on 2016-10-18 17:55:23 +0200 using RuboCop version 0.42.0.
4
+ # The point is for the user to remove these configuration records
5
+ # one by one as the offenses are removed from the code base.
6
+ # Note that changes in the inspected code, or installation of new
7
+ # versions of RuboCop, may require this file to be generated again.
8
+
9
+ # Offense count: 2
10
+ Lint/ShadowingOuterLocalVariable:
11
+ Exclude:
12
+ - 'lib/dry/behaviour/dsl.rb'
13
+
14
+ # Offense count: 2
15
+ Lint/UselessAssignment:
16
+ Exclude:
17
+ - 'lib/dry/behaviour/dsl.rb'
18
+
19
+ # Offense count: 1
20
+ Metrics/AbcSize:
21
+ Max: 36
22
+
23
+ # Offense count: 5
24
+ # Configuration parameters: AllowHeredoc, AllowURI, URISchemes.
25
+ # URISchemes: http, https
26
+ Metrics/LineLength:
27
+ Max: 116
28
+
29
+ # Offense count: 1
30
+ # Configuration parameters: CountComments.
31
+ Metrics/MethodLength:
32
+ Max: 21
33
+
34
+ # Offense count: 2
35
+ Style/AsciiIdentifiers:
36
+ Exclude:
37
+ - 'lib/dry/behaviour/dsl.rb'
38
+
39
+ # Offense count: 1
40
+ # Cop supports --auto-correct.
41
+ # Configuration parameters: Keywords.
42
+ # Keywords: TODO, FIXME, OPTIMIZE, HACK, REVIEW
43
+ Style/CommentAnnotation:
44
+ Exclude:
45
+ - 'lib/dry/behaviour/dsl.rb'
46
+
47
+ # Offense count: 2
48
+ Style/Documentation:
49
+ Exclude:
50
+ - 'spec/**/*'
51
+ - 'test/**/*'
52
+ - 'lib/dry/behaviour.rb'
53
+ - 'lib/dry/behaviour/dsl.rb'
54
+
55
+ # Offense count: 3
56
+ # Cop supports --auto-correct.
57
+ # Configuration parameters: AllowAdjacentOneLineDefs.
58
+ Style/EmptyLineBetweenDefs:
59
+ Exclude:
60
+ - 'lib/dry/behaviour/dsl.rb'
61
+
62
+ # Offense count: 1
63
+ # Cop supports --auto-correct.
64
+ # Configuration parameters: EnforcedStyle, SupportedStyles, UseHashRocketsWithSymbolValues, PreferHashRocketsForNonAlnumEndingSymbols.
65
+ # SupportedStyles: ruby19, ruby19_no_mixed_keys, hash_rockets
66
+ Style/HashSyntax:
67
+ Exclude:
68
+ - 'Rakefile'
69
+
70
+ # Offense count: 3
71
+ # Cop supports --auto-correct.
72
+ # Configuration parameters: SupportedStyles.
73
+ # SupportedStyles: call, braces
74
+ Style/LambdaCall:
75
+ EnforcedStyle: braces
76
+
77
+ # Offense count: 2
78
+ # Cop supports --auto-correct.
79
+ # Configuration parameters: EnforcedStyle, SupportedStyles.
80
+ # SupportedStyles: require_parentheses, require_no_parentheses, require_no_parentheses_except_multiline
81
+ Style/MethodDefParentheses:
82
+ Exclude:
83
+ - 'lib/dry/behaviour/dsl.rb'
84
+
85
+ # Offense count: 5
86
+ # Cop supports --auto-correct.
87
+ # Configuration parameters: EnforcedStyle, SupportedStyles, ConsistentQuotesInMultiline.
88
+ # SupportedStyles: single_quotes, double_quotes
89
+ Style/StringLiterals:
90
+ Exclude:
91
+ - 'Rakefile'
92
+ - 'bin/console'
93
+
94
+ # Offense count: 1
95
+ # Configuration parameters: EnforcedStyle, SupportedStyles.
96
+ # SupportedStyles: snake_case, camelCase
97
+ Style/VariableName:
98
+ Exclude:
99
+ - 'lib/dry/behaviour/dsl.rb'
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.1
4
+ - 2.1.10
5
+ - 2.2.2
6
+ - 2.3.1
7
+ before_install: gem install bundler -v 1.10.6
@@ -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,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in dry-behaviour.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Aleksei Matiushkin
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,100 @@
1
+ # Dry::Behaviour
2
+
3
+ [![Build Status](https://travis-ci.org/am-kantox/dry-behaviour.svg?branch=master)](https://travis-ci.org/am-kantox/dry-behaviour)
4
+
5
+ **Tiny library inspired by Elixir [`protocol`](http://elixir-lang.org/getting-started/protocols.html) pattern.**
6
+
7
+ ## Declaration
8
+
9
+ ```ruby
10
+ require 'dry/behaviour'
11
+
12
+ module Protocols
13
+ module Adder
14
+ include Dry::Protocol
15
+
16
+ defprotocol do
17
+ defmethod :add, :this, :other
18
+ defmethod :subtract, :this, :other
19
+
20
+ def add_default(value)
21
+ add(3, 2) + value
22
+ end
23
+ end
24
+
25
+ defimpl Protocols::Adder, for: String do
26
+ def add(this, other)
27
+ this * other
28
+ end
29
+ def subtract(this, other)
30
+ this
31
+ end
32
+ end
33
+ defimpl Protocols::Adder, for: NilClass do
34
+ def add(this, other)
35
+ other
36
+ end
37
+ def subtract(this, other)
38
+ this
39
+ end
40
+ end
41
+
42
+ defimpl for: Integer do
43
+ def add(this, other)
44
+ this + other
45
+ end
46
+ def subtract(this, other)
47
+ this - other
48
+ end
49
+ end
50
+ end
51
+ end
52
+ ```
53
+
54
+ ## Usage
55
+
56
+ ```ruby
57
+ expect(Protocols::Adder.add(5, 3)).to eq(8)
58
+ expect(Protocols::Adder.add(5, 10)).to eq(15)
59
+ expect(Protocols::Adder.subtract(5, 10)).to eq(-5)
60
+ expect(Protocols::Adder.add(15, 10)).to eq(25)
61
+ expect(Protocols::Adder.add("!", 10)).to eq("!!!!!!!!!!")
62
+ expect(Protocols::Adder.add(nil, 10)).to eq(10)
63
+
64
+ expect(Protocols::Adder.add_default(1)).to eq(6)
65
+ ```
66
+
67
+ ## Authors
68
+
69
+ @am-kantox, @saverio-kantox & @kantox
70
+
71
+ ## Installation
72
+
73
+ Add this line to your application's Gemfile:
74
+
75
+ ```ruby
76
+ gem 'dry-behaviour'
77
+ ```
78
+
79
+ And then execute:
80
+
81
+ $ bundle
82
+
83
+ Or install it yourself as:
84
+
85
+ $ gem install dry-behaviour
86
+
87
+ ## Development
88
+
89
+ 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.
90
+
91
+ 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).
92
+
93
+ ## Contributing
94
+
95
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/dry-behaviour. 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.
96
+
97
+
98
+ ## License
99
+
100
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,24 @@
1
+ # encoding: UTF-8
2
+ require 'rubygems'
3
+
4
+ require 'bundler'
5
+ require 'bundler/setup'
6
+ require 'bundler/gem_tasks'
7
+
8
+ require 'rspec/core/rake_task'
9
+
10
+ begin
11
+ Bundler.setup(:default, :development, :test)
12
+ rescue Bundler::BundlerError => e
13
+ $stderr.puts e.message
14
+ $stderr.puts 'Run `bundle install` to install missing gems'
15
+ exit e.status_code
16
+ end
17
+
18
+ desc 'Tests'
19
+ RSpec::Core::RakeTask.new(:spec) do |spec|
20
+ spec.rspec_opts = '-Ispec'
21
+ # spec.rcov = true
22
+ end
23
+
24
+ task default: [:spec]
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "dry/behaviour"
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,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,32 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'dry/behaviour/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'dry-behaviour'
8
+ spec.version = Dry::Behaviour::VERSION
9
+ spec.authors = ['Aleksei Matiushkin', 'Saverio Trioni', 'Kantox LTD']
10
+ spec.email = ['aleksei.matiushkin@kantox.com', 'saverio.trioni@kantox.com']
11
+
12
+ spec.summary = %(Tiny library inspired by Elixir protocol pattern.)
13
+ spec.description = %(This library makes it easy to declare protocols and use in in functional way.)
14
+ spec.homepage = 'https://kantox.com/'
15
+ spec.license = 'MIT'
16
+
17
+ raise 'RubyGems 2.0 or newer is required to protect against public gem pushes.' unless spec.respond_to?(:metadata)
18
+
19
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
20
+ # delete this section to allow pushing this gem to any host.
21
+ # spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
22
+
23
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
24
+ spec.bindir = 'bin'
25
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
26
+ spec.require_paths = ['lib']
27
+
28
+ spec.add_development_dependency 'bundler', '~> 1.10'
29
+ spec.add_development_dependency 'rake', '~> 10.0'
30
+ spec.add_development_dependency 'rspec'
31
+ spec.add_development_dependency 'pry'
32
+ end
@@ -0,0 +1,14 @@
1
+ require 'dry/behaviour/version'
2
+ require 'dry/behaviour/black_tie'
3
+
4
+ module Dry
5
+ module Behaviour
6
+ # Your code goes here...
7
+ end
8
+
9
+ module Protocol
10
+ def self.included(base)
11
+ base.singleton_class.prepend(Dry::BlackTie)
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,48 @@
1
+ module Dry
2
+ module BlackTie
3
+ class << self
4
+ def protocols
5
+ @protocols ||= Hash.new { |h, k| h[k] = h.dup.clear }
6
+ end
7
+
8
+ def implementations
9
+ @implementations ||= Hash.new { |h, k| h[k] = h.dup.clear }
10
+ end
11
+ end
12
+
13
+ def defprotocol(delegate: [])
14
+ raise if BlackTie.protocols.key?(self) # DUPLICATE DEF
15
+ raise unless block_given? || !delegate.empty?
16
+ # FIXME IMPLEMENT DELEGATES!
17
+ raise unless block_given?
18
+
19
+ ims = instance_methods(false)
20
+ class_eval(&Proc.new)
21
+ (instance_methods(false) - ims).each { |m| class_eval { module_function m } }
22
+
23
+ BlackTie.protocols[self].each do |method, *_| # FIXME CHECK PARAMS CORRESPONDENCE HERE
24
+ # receiver, *args = *args
25
+ singleton_class.send :define_method, method do |receiver, *args|
26
+ receiver.class.ancestors.lazy.map do |c|
27
+ BlackTie.implementations[self].fetch(c, nil)
28
+ end.reject(&:nil?).first[method].(receiver, *args)
29
+ end
30
+ end
31
+ end
32
+
33
+ def defmethod name, *params
34
+ BlackTie.protocols[self][name] = params
35
+ end
36
+
37
+ def defimpl protocol = nil, **params
38
+ raise unless block_given?
39
+ raise if params[:for].nil?
40
+
41
+ Module.new { singleton_class.class_eval(&Proc.new) }.tap do |mod|
42
+ mod.methods(false).each do |m|
43
+ BlackTie.implementations[protocol || self][params[:for]][m] = mod.method(m).to_proc
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,5 @@
1
+ module Dry
2
+ module Behaviour
3
+ VERSION = '0.1.0'.freeze
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dry-behaviour
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Aleksei Matiushkin
8
+ - Saverio Trioni
9
+ - Kantox LTD
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2016-10-19 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: bundler
17
+ requirement: !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - "~>"
20
+ - !ruby/object:Gem::Version
21
+ version: '1.10'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - "~>"
27
+ - !ruby/object:Gem::Version
28
+ version: '1.10'
29
+ - !ruby/object:Gem::Dependency
30
+ name: rake
31
+ requirement: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - "~>"
34
+ - !ruby/object:Gem::Version
35
+ version: '10.0'
36
+ type: :development
37
+ prerelease: false
38
+ version_requirements: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - "~>"
41
+ - !ruby/object:Gem::Version
42
+ version: '10.0'
43
+ - !ruby/object:Gem::Dependency
44
+ name: rspec
45
+ requirement: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ type: :development
51
+ prerelease: false
52
+ version_requirements: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ - !ruby/object:Gem::Dependency
58
+ name: pry
59
+ requirement: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ type: :development
65
+ prerelease: false
66
+ version_requirements: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ description: This library makes it easy to declare protocols and use in in functional
72
+ way.
73
+ email:
74
+ - aleksei.matiushkin@kantox.com
75
+ - saverio.trioni@kantox.com
76
+ executables: []
77
+ extensions: []
78
+ extra_rdoc_files: []
79
+ files:
80
+ - ".gitignore"
81
+ - ".rspec"
82
+ - ".rubocop.yml"
83
+ - ".rubocop_todo.yml"
84
+ - ".travis.yml"
85
+ - CODE_OF_CONDUCT.md
86
+ - Gemfile
87
+ - LICENSE.txt
88
+ - README.md
89
+ - Rakefile
90
+ - bin/console
91
+ - bin/setup
92
+ - dry-behaviour.gemspec
93
+ - lib/dry/behaviour.rb
94
+ - lib/dry/behaviour/black_tie.rb
95
+ - lib/dry/behaviour/version.rb
96
+ homepage: https://kantox.com/
97
+ licenses:
98
+ - MIT
99
+ metadata: {}
100
+ post_install_message:
101
+ rdoc_options: []
102
+ require_paths:
103
+ - lib
104
+ required_ruby_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ required_rubygems_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ requirements: []
115
+ rubyforge_project:
116
+ rubygems_version: 2.4.8
117
+ signing_key:
118
+ specification_version: 4
119
+ summary: Tiny library inspired by Elixir protocol pattern.
120
+ test_files: []