hash_param 0.0.2

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: 9fa41452500af1f6485cf963eddba85b8b136480
4
+ data.tar.gz: 092c5a37c775949c30911a7a5f5f663bc1ec6da0
5
+ SHA512:
6
+ metadata.gz: 9a2ab5099f422e870b06826926a4259af2b2688882fca1122fddad9c86cf9e6bd5cf3e13c1c694aa44d14d80a2a10a1e4bad003ce8565204f7814d81fcea0978
7
+ data.tar.gz: ee4207b9b643940dfcc2d9d95c217aff7fdc10ce861dc6ab182f2729e2e91541b8af4b00d195e4d6b09b967cb65cc8db2ed298a2b60cff16a3ef6ca262f0e737
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.2
4
+ before_install: gem install bundler -v 1.10.5
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Mike A. Owens
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,95 @@
1
+ # hash_param
2
+
3
+ hash_param is an library that converts functions that take a single "data"
4
+ argument Hash into a function that takes named, formal parameters. This allows
5
+ APIs like ActionCable to be a little easier to work with.
6
+
7
+ For example, it can turn this:
8
+
9
+ ```ruby
10
+ class ChatChannel < ApplicationCable::Channel
11
+ def send_message(data)
12
+ chat_id = data['chat_id']
13
+ author_id = data['author_id']
14
+ body = data['body']
15
+ # ...
16
+ end
17
+
18
+ def typing_status(data)
19
+ chat_id = data['chat_id']
20
+ user_id = data['user_id']
21
+ status = data['status'] || 'typing'
22
+ # ...
23
+ end
24
+ end
25
+ ```
26
+
27
+ into this:
28
+
29
+ ```ruby
30
+ class ChatChannel < ApplicationCable::Channel
31
+ hash_param :send_message, :typing_status
32
+
33
+ def send_message(chat_id, author_id, body)
34
+ # ...
35
+ end
36
+
37
+ def typing_status(chat_id, user_id, status = 'typing')
38
+ # ...
39
+ end
40
+ end
41
+ ```
42
+
43
+ It does this by looking at your method signature and filling in parameters by
44
+ name from the data object. It plays nice with default parameters, keyword
45
+ arguments, both optional and required, `*rest` and `**kwrest` parameters.
46
+
47
+ After named positional arguments, `hash_param` will try to fill named keyword
48
+ arguments, then will send the rest to `**kwrest` if listed, and lastly, will
49
+ send whats left of the data object to `*args`. In practice, this means you'll
50
+ never get values in both `*args` and `**kwrest`.
51
+
52
+ `*args` parameters keep the string key names, while `**kwrest` hashes get their
53
+ keys converted into symbols. If there are no values to pass to `*args`, you'll
54
+ get an empty list, not a list containing an empty Hash.
55
+
56
+ If the argument list cannot be satisfied, `ArgumentError` is raised. Extraneous
57
+ properties sent to the method are ignored if not consumed through the argument
58
+ list.
59
+
60
+
61
+ ## Installation
62
+
63
+ Add this line to your application's Gemfile:
64
+
65
+ ```ruby
66
+ gem 'hash_param'
67
+ ```
68
+
69
+ And then execute:
70
+
71
+ $ bundle
72
+
73
+ Or install it yourself as:
74
+
75
+ $ gem install hash_param
76
+
77
+ ## Development
78
+
79
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run
80
+ `rake test` to run the tests. You can also run `bin/console` for an interactive
81
+ prompt that will allow you to experiment.
82
+
83
+ To install this gem onto your local machine, run `bundle exec rake install`. To
84
+ release a new version, update the version number in `version.rb`, and then run
85
+ `bundle exec rake release`, which will create a git tag for the version, push
86
+ git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
87
+
88
+ ## Contributing
89
+
90
+ Bug reports and pull requests are welcome on GitHub at https://github.com/metermd/hash_param.
91
+
92
+
93
+ ## License
94
+
95
+ 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,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 => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "hash_param"
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,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'hash_param/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "hash_param"
8
+ spec.version = HashParam::VERSION
9
+ spec.authors = ["Mike A. Owens"]
10
+ spec.email = ["mike@meter.md"]
11
+
12
+ spec.summary = %q{Automatically extracts Hash parameters into formal parameters}
13
+ spec.homepage = "https://github.com/metermd/hash_param"
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 "minitest"
24
+ end
@@ -0,0 +1,3 @@
1
+ module HashParam
2
+ VERSION = "0.0.2"
3
+ end
data/lib/hash_param.rb ADDED
@@ -0,0 +1,96 @@
1
+ require 'hash_param/version'
2
+
3
+ module HashParam
4
+ def self.included(cls)
5
+ cls.extend(ClassMethods)
6
+ end
7
+
8
+ module ClassMethods
9
+ def hash_param(*method_names)
10
+ method_names.each do |method_name|
11
+ visibility = instance_method_visibility(method_name)
12
+ hidden_name = "#{method_name}_before_hash_param"
13
+
14
+ alias_method hidden_name, method_name
15
+ private hidden_name
16
+
17
+ define_method(method_name) do |*args|
18
+ hash_param_dispatch(hidden_name, *args)
19
+ end
20
+
21
+ # We set the new wrapping method's visibility to what the original was.
22
+ send(visibility, method_name)
23
+ end
24
+ end
25
+
26
+ private
27
+ # This determines the visibility of an instance method, e.g.,
28
+ # Object.instance_method_visibility(:object_id) => :public
29
+ def instance_method_visibility(method_name)
30
+ return :public if public_instance_methods.include?(method_name)
31
+ return :protected if protected_instance_methods.include?(method_name)
32
+ return :private if private_instance_methods.include?(method_name)
33
+ fail NoMethodError, "Cannot access visibility of #{name}\##{method_name}"
34
+ end
35
+ end
36
+
37
+ private
38
+ def hash_param_dispatch(method_name, data)
39
+ args, kwargs, rest_index, has_kwrest = [], {}, nil, nil
40
+ public_method_name = method_name
41
+
42
+ method(method_name).parameters.each.with_index do |(type, name), i|
43
+ key_name = name.to_s
44
+ exists = data.key?(key_name)
45
+ value = exists ? data.delete(key_name) : nil
46
+
47
+ case type
48
+ when :req
49
+ fail ArgumentError, "#{public_method_name}: required argument " +
50
+ "`#{name}' not present" unless exists
51
+ args.push(value)
52
+
53
+ when :opt
54
+ args.push(value) if exists
55
+
56
+ when :keyreq
57
+ fail ArgumentError, "#{public_method_name}: required keyword argument " +
58
+ "`#{name}' not present" unless exists
59
+ kwargs[name] = value
60
+
61
+ when :key
62
+ kwargs[name] = value if exists
63
+
64
+ when :rest
65
+ # If data happens to contain a key with the same name as the rest arg,
66
+ # we still want it to show up string-ized, so we put back the value
67
+ # we deleted earlier.
68
+ data[key_name] = value if exists
69
+ rest_index = i
70
+
71
+ when :keyrest
72
+ # Because keyrest supercedes rest, we add this to kwargs if the
73
+ # data hash happened to contain a key named the same as the keyrest
74
+ # argument name.
75
+ kwargs[name] = value if exists
76
+ has_kwrest = true
77
+
78
+ else
79
+ fail ArgumentError, "#{public_method_name}: cannot dispatch " +
80
+ "argument `#{name}', (type: #{type}) from Hash"
81
+ end
82
+ end
83
+
84
+ if has_kwrest
85
+ kwargs.merge!(data.map { |k,v| [k.to_sym, v] }.to_h)
86
+ elsif rest_index
87
+ args[rest_index, 0] = data unless data.empty?
88
+ end
89
+
90
+ if kwargs.empty?
91
+ send(method_name, *args)
92
+ else
93
+ send(method_name, *args, **kwargs)
94
+ end
95
+ end
96
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hash_param
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Mike A. Owens
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-09-04 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: minitest
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:
56
+ email:
57
+ - mike@meter.md
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".travis.yml"
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - bin/console
69
+ - bin/setup
70
+ - hash_param.gemspec
71
+ - lib/hash_param.rb
72
+ - lib/hash_param/version.rb
73
+ homepage: https://github.com/metermd/hash_param
74
+ licenses:
75
+ - MIT
76
+ metadata: {}
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 2.4.5.1
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: Automatically extracts Hash parameters into formal parameters
97
+ test_files: []