quacks 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: bb32509485b5d1544468fd3c8b3d2febba4f0bb8
4
+ data.tar.gz: e089a05755d5f489607f152357ca150510f9f353
5
+ SHA512:
6
+ metadata.gz: c2cc4349f1c9924a8ec49747995d8134e4c66882e74a68b67f906b9c2b3fd72a295af65475bb622fdde60249aa612f68ca3f58d79b3f569855bf1355ee9e536e
7
+ data.tar.gz: 1949d819ce3167c0582be5520ba0653f1ec2377ab2dca4b6fdb9a669306dc7d9182c54a316317e0e05ce868b0482c656ddceef6cb44162c9a59be208767edccb
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.0
5
+ before_install: gem install bundler -v 1.15.4
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in quack.gemspec
6
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Emric
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,72 @@
1
+ # Quack
2
+ Quack makes ducktyping easy!
3
+
4
+ ## Installation
5
+ Add this line to your application's Gemfile:
6
+
7
+ ```ruby
8
+ gem 'quack'
9
+ ```
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install quack
18
+
19
+ ## Usage
20
+ To use Quack you annotate your methods with the `.quacks_like` method.
21
+
22
+ ```ruby
23
+ def add(int_a, int_b)
24
+ int_a + int_b
25
+ end
26
+ quacks_like :add, :to_i, :to_i
27
+ ```
28
+
29
+ This will automatically convert the arguments if possible:
30
+
31
+ ```ruby
32
+ add('1', 2)
33
+ #=> 3
34
+ ```
35
+
36
+ If the arguments can not be converted an error will be raised:
37
+ ```ruby
38
+ add(1, {})
39
+ #=> Quack::SignatureError: `{}` must respond to `to_i`.
40
+ ```
41
+
42
+ You can force symbol arguments to be converted like so:
43
+ ```ruby
44
+ def divide(int_a, divisor: 2)
45
+ int_a / divisor
46
+ end
47
+ quacks_like :divide, :to_i, divisor: :to_f
48
+ ```
49
+
50
+ If you want to add a signature to a class method you use the singleton class:
51
+ ```ruby
52
+ class Calculator
53
+ def self.add(int_a, int_b)
54
+ int_a + int_b
55
+ end
56
+ singleton_class.quacks_like :add, :int_a, :int_b
57
+ end
58
+ ```
59
+
60
+ ## Development
61
+
62
+ 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.
63
+
64
+ 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).
65
+
66
+ ## Contributing
67
+
68
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/quack.
69
+
70
+ ## License
71
+
72
+ 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 "quack"
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(__FILE__)
@@ -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
@@ -0,0 +1,16 @@
1
+ require "quacks/version"
2
+ require "quacks/signature"
3
+ require "quacks/default_convertor"
4
+ require "quacks/hash_convertor"
5
+ require "quacks/error"
6
+ require "quacks/signature_error"
7
+ require "quacks/wrong_number_of_arguments_error"
8
+ require "quacks/quackable"
9
+
10
+ module Quacks
11
+ end
12
+
13
+ class Object
14
+ extend Quacks::Quackable
15
+ singleton_class.extend Quacks::Quackable
16
+ end
@@ -0,0 +1,34 @@
1
+ # Internal: The default convertor class to be used in order to convert a single
2
+ # argument.
3
+ class Quacks::DefaultConvertor
4
+ attr_reader :conversion_method
5
+
6
+ # Internal: Initialize a DefaultConvertor.
7
+ #
8
+ # conversion_method - The method to call that does the conversion
9
+ def initialize(conversion_method)
10
+ @conversion_method = conversion_method
11
+ end
12
+
13
+ # Internal: Converts the given argument with the conversion method given in the
14
+ # initializer.
15
+ #
16
+ # argument - Any object to convert.
17
+ #
18
+ # Returns the converted argument.
19
+ # Raises Quacks::SignatureError if the argument could not be converted.
20
+ def convert!(argument)
21
+ return argument.public_send(conversion_method) if convertable?(argument)
22
+ raise(Quacks::SignatureError,
23
+ "`#{argument}` must respond to `#{conversion_method}`")
24
+ end
25
+
26
+ private
27
+
28
+ # Internal: Tells whether the provided argument can be converted or not.
29
+ #
30
+ # Returns a Bool.
31
+ def convertable?(argument)
32
+ argument.respond_to? conversion_method
33
+ end
34
+ end
@@ -0,0 +1,2 @@
1
+ class Quacks::Error < StandardError
2
+ end
@@ -0,0 +1,32 @@
1
+ # Internal: The convertor class to iterate and convert symbol arguments or
2
+ # hashes.
3
+ class Quacks::HashConvertor
4
+ attr_reader :conversion_methods
5
+
6
+ # Internal: Initialize a HashConvertor.
7
+ #
8
+ # conversion_methods - The keywords and conversion methods to be used.
9
+ def initialize(conversion_methods)
10
+ @conversion_methods = conversion_methods
11
+ end
12
+
13
+ # Internal: Converts the given symbol arguments with the provided conversion
14
+ # methods.
15
+ #
16
+ # argument_hash - The hash with arguments to convert.
17
+ #
18
+ # Examples:
19
+ #
20
+ # convertor = Quacks::HashConvertor.new(word: :to_s, number: :to_i)
21
+ # convertor.convert!(word: nil, number: "100")
22
+ # #=> { word: "", number: 100 }
23
+ #
24
+ # Returns an Hash with the converted arguments.
25
+ # Raises Quacks::SignatureError if the arguments could not be converted.
26
+ def convert!(argument_hash)
27
+ conversion_methods
28
+ .each_with_object(argument_hash) do |(name, conversion), args|
29
+ args[name] = Quacks::DefaultConvertor.new(conversion).convert!(args[name])
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,40 @@
1
+ # Public: Concern to be extended in order to enable the quacks like method.
2
+ module Quacks::Quackable
3
+ # Public: Force the given method to use the given conversions for its
4
+ # arguments.
5
+ #
6
+ # Examples
7
+ # class Calculator
8
+ # def add(int_a, int_b)
9
+ # int_a + int_b
10
+ # end
11
+ # quacks_like :add, :to_i, :to_i
12
+ # end
13
+ #
14
+ # # Supported arguments will be converted accordingly.
15
+ # Calculator.new.add(1, '2')
16
+ # #=> 3
17
+ #
18
+ # # Unspported arguments will raise an error
19
+ # Calculator.new.add(1, {})
20
+ # #=> SignatureError: Expected `{}` to respond to `to_i`.
21
+ #
22
+ # # To add signatures to class methods you can use the singleton class.
23
+ # class Calculator
24
+ # singleton_class.extend(Quacks::Quackable)
25
+ #
26
+ # def self.add(int_a, int_b)
27
+ # int_a + int_b
28
+ # end
29
+ # singleton_class.quacks_like :add, :to_i, :to_i
30
+ # end
31
+ #
32
+ # Returns the Symbol method name.
33
+ def quacks_like(method_name, *signature_args)
34
+ alias_method "orig_#{method_name}", method_name
35
+ define_method(method_name) do |*args|
36
+ signature = Quacks::Signature.new(*signature_args)
37
+ send("orig_#{method_name}", *signature.apply!(*args))
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,74 @@
1
+ # Internal: The class responsible for massaging parameters before passing them
2
+ # to the signatured method.
3
+ class Quacks::Signature
4
+ attr_reader :signature
5
+
6
+ # Internal: Initialize a signature object.
7
+ #
8
+ # signature - The Symbol or Hash conversion method(s) that depicts this
9
+ # signature.
10
+ def initialize(*signature)
11
+ @signature = signature
12
+ end
13
+
14
+ # Internal: Convert the given arguments, with the given conversion methods.
15
+ #
16
+ # Examples:
17
+ #
18
+ # Quacks::Signature.new(:to_i, bignum: :to_s).apply!('1', 100)
19
+ # #=> [1, { bignum: '100' }]
20
+ #
21
+ # Returns an Array of arguments.
22
+ # Raises Quacks::SignatureError if the arguments could not be converted.
23
+ # Raises Quacks::WrongNumberOfArgumentsError if wrong number of arguments.
24
+ def apply!(*args)
25
+ validate args, signature
26
+ signature.each_with_index.map do |arg_signature, i|
27
+ convert! args[i], arg_signature
28
+ end
29
+ end
30
+
31
+ private
32
+
33
+ # Internal: Returns a convertor to perform the given conversion.
34
+ #
35
+ # conversion - An Hash with conversions or a Symbol conversion.
36
+ #
37
+ # Returns a Quacks::HashConvertor or Quacks::DefaultConvertor instance.
38
+ def convertor_for(conversion)
39
+ convertor_class(conversion).new(conversion)
40
+ end
41
+
42
+ # Internal: Returns the class to be used for applying the given conversion.
43
+ #
44
+ # conversion = An Hash with conversions or a Symbol conversion.
45
+ def convertor_class(conversion)
46
+ case conversion
47
+ when Hash
48
+ Quacks::HashConvertor
49
+ else
50
+ Quacks::DefaultConvertor
51
+ end
52
+ end
53
+
54
+ # Internal: Converts the given arguments with the given conversion(s).
55
+ #
56
+ #
57
+ # arguments - The Hash symbol arguments or any Object to convert.
58
+ # conversion = An Hash with conversions or a Symbol conversion.
59
+ #
60
+ # Returns the converted argument.
61
+ def convert!(arguments, conversion)
62
+ convertor_for(conversion).convert!(arguments)
63
+ end
64
+
65
+ # Internal: Determine if correct number of arguments were given.
66
+ #
67
+ # Returns true if correct number of arguments.
68
+ # Raises Quacks::WrongNumberOfArgumentsError if wrong number of arguments
69
+ # given.
70
+ def validate(arguments, signature)
71
+ return true if arguments.length == signature.length
72
+ raise Quacks::WrongNumberOfArgumentsError
73
+ end
74
+ end
@@ -0,0 +1,2 @@
1
+ class Quacks::SignatureError < Quacks::Error
2
+ end
@@ -0,0 +1,3 @@
1
+ module Quacks
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,2 @@
1
+ class Quacks::WrongNumberOfArgumentsError < ArgumentError
2
+ end
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "quacks/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "quacks"
8
+ spec.version = Quacks::VERSION
9
+ spec.authors = ["Emric"]
10
+ spec.email = ["w.e.w@live.se"]
11
+
12
+ spec.summary = "Add signatures to your methods"
13
+ spec.homepage = "http://github.com/istanful/quacks"
14
+ spec.license = "MIT"
15
+
16
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
19
+ f.match(%r{^(test|spec|features)/})
20
+ end
21
+ spec.bindir = "exe"
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ["lib"]
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.15"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "rspec", "~> 3.0"
28
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: quacks
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Emric
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-07-23 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.15'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.15'
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: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description:
56
+ email:
57
+ - w.e.w@live.se
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - bin/console
70
+ - bin/setup
71
+ - lib/quacks.rb
72
+ - lib/quacks/default_convertor.rb
73
+ - lib/quacks/error.rb
74
+ - lib/quacks/hash_convertor.rb
75
+ - lib/quacks/quackable.rb
76
+ - lib/quacks/signature.rb
77
+ - lib/quacks/signature_error.rb
78
+ - lib/quacks/version.rb
79
+ - lib/quacks/wrong_number_of_arguments_error.rb
80
+ - quacks.gemspec
81
+ homepage: http://github.com/istanful/quacks
82
+ licenses:
83
+ - MIT
84
+ metadata:
85
+ allowed_push_host: https://rubygems.org
86
+ post_install_message:
87
+ rdoc_options: []
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ requirements: []
101
+ rubyforge_project:
102
+ rubygems_version: 2.5.1
103
+ signing_key:
104
+ specification_version: 4
105
+ summary: Add signatures to your methods
106
+ test_files: []