actioncable_auto_param 0.0.1

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: b19846f3d4c414a7739036126f1ddc493b26b7ae
4
+ data.tar.gz: de77ca1de521e049be270dca457336b5bc88702f
5
+ SHA512:
6
+ metadata.gz: 24a5c4da22cdce788f5f3fcb2af1749be01540c52513965ce1bd971c4649e8b17bf9d48131192512418edc28a9682d48818f7fd7045bf3e63245ab5ec83e6ef0
7
+ data.tar.gz: d213c5fa440d864864e11f9b85a4eb6efadd2a931b137235a16eb68e3ebbebfb89ddbc7009865340daf63d041831a90619f831b2e83d11ef3dc8f61b5d25f7ec
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -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,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in actioncable_auto_param.gemspec
4
+ gemspec
@@ -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.
@@ -0,0 +1,92 @@
1
+ # actioncable_auto_param
2
+
3
+ actioncable_auto_param is an ActionCable add-on that automatically extracts
4
+ parameters from the data object passed to ActionCable Channel action methods.
5
+
6
+ For example, it can turn this:
7
+
8
+ ```ruby
9
+ class ChatChannel < ApplicationCable::Channel
10
+ def send_message(data)
11
+ chat_id = data['chat_id']
12
+ author_id = data['author_id']
13
+ body = data['body']
14
+ # ...
15
+ end
16
+
17
+ def typing_status(data)
18
+ chat_id = data['chat_id']
19
+ user_id = data['user_id']
20
+ status = data['status'] || 'typing'
21
+ # ...
22
+ end
23
+ end
24
+ ```
25
+
26
+ into this:
27
+
28
+ ```ruby
29
+ class ChatChannel < ApplicationCable::Channel
30
+ auto_param :all # or: `auto_param :send_message, :typing_status`
31
+
32
+ def send_message(chat_id, author_id, body)
33
+ # ...
34
+ end
35
+
36
+ def typing_status(chat_id, user_id, status = 'typing')
37
+ # ...
38
+ end
39
+ end
40
+ ```
41
+
42
+ It does this by looking at your method signature and filling in parameters by
43
+ name from the data object. It plays nice with default parameters, keyword
44
+ arguments, both optional and required, `*rest` and `**kwrest` parameters.
45
+
46
+ After named positional arguments, `auto_param` will try to fill named keyword
47
+ arguments, then will send the rest to `**kwrest` if listed, and lastly, will
48
+ send whats left of the data object to `*args`.
49
+
50
+ If the argument list cannot be satisfied, `ArgumentError` is raised. Extraneous
51
+ properties sent to the action are ignored if not consumed through the argument
52
+ list.
53
+
54
+ ActionCable is still on shaky API ground, and so is this gem. It's effectively
55
+ a monkey patch, so that makes it even more brittle. Be advised.
56
+
57
+
58
+ ## Installation
59
+
60
+ Add this line to your application's Gemfile:
61
+
62
+ ```ruby
63
+ gem 'actioncable_auto_param'
64
+ ```
65
+
66
+ And then execute:
67
+
68
+ $ bundle
69
+
70
+ Or install it yourself as:
71
+
72
+ $ gem install actioncable_auto_param
73
+
74
+ ## Development
75
+
76
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run
77
+ `rake test` to run the tests. You can also run `bin/console` for an interactive
78
+ prompt that will allow you to experiment.
79
+
80
+ To install this gem onto your local machine, run `bundle exec rake install`. To
81
+ release a new version, update the version number in `version.rb`, and then run
82
+ `bundle exec rake release`, which will create a git tag for the version, push
83
+ git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
84
+
85
+ ## Contributing
86
+
87
+ Bug reports and pull requests are welcome on GitHub at https://github.com/metermd/actioncable_auto_param.
88
+
89
+
90
+ ## License
91
+
92
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -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
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'actioncable_auto_param/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "actioncable_auto_param"
8
+ spec.version = ActioncableAutoParam::VERSION
9
+ spec.authors = ["Mike A. Owens"]
10
+ spec.email = ["mike@meter.md"]
11
+
12
+ spec.summary = %q{Automatically extracts ActionCable objects into parameters}
13
+ spec.homepage = "https://github.com/metermd/actioncable_auto_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_dependency "actioncable", "~> 0.0.3"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.10"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "minitest"
26
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "actioncable_auto_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
@@ -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,92 @@
1
+ require 'actioncable_auto_param/version'
2
+ require 'action_cable/channel'
3
+
4
+ module ActioncableAutoParam
5
+ module ClassMethods
6
+ def auto_param(*method_names)
7
+ method_names.each do |method_name|
8
+ if method_name == :all
9
+ self.auto_param_all_methods = true
10
+ end
11
+
12
+ self.auto_param_methods ||= []
13
+ self.auto_param_methods.push(method_name)
14
+ end
15
+ end
16
+
17
+ def auto_param?(method_name)
18
+ auto_param_all_methods || (auto_param_methods || []).include?(method_name)
19
+ end
20
+ end
21
+
22
+ def self.prepended(cls)
23
+ cls.class_attribute :auto_param_all_methods
24
+ cls.class_attribute :auto_param_methods
25
+ end
26
+
27
+ # Monkeypatch
28
+ def dispatch_action(action, data)
29
+ method_name = action.to_sym
30
+ if self.class.auto_param?(method_name)
31
+ auto_param_dispatch(method_name, data)
32
+ else
33
+ super
34
+ end
35
+ rescue
36
+ puts $!, $!.backtrace
37
+ end
38
+
39
+ private
40
+ def auto_param_dispatch(method_name, data)
41
+ args, kwargs, rest_index, has_kwrest = [], {}, nil, nil
42
+
43
+ method(method_name).parameters.each.with_index do |(type, name), i|
44
+ key_name = name.to_s
45
+ exists = data.key?(key_name)
46
+ value = exists ? data.delete(key_name) : nil
47
+
48
+ case type
49
+ when :req
50
+ fail ArgumentError, "#{method_name}: required argument " +
51
+ "`#{name}' not present" unless exists
52
+ args.push(value)
53
+
54
+ when :opt
55
+ args.push(value) if exists
56
+
57
+ when :keyreq
58
+ fail ArgumentError, "#{method_name}: required keyword argument " +
59
+ "`#{name}' not present" unless exists
60
+ kwargs[name] = value
61
+
62
+ when :key
63
+ kwargs[name] = value if exists
64
+
65
+ when :rest
66
+ rest_index = i
67
+
68
+ when :keyrest
69
+ has_kwrest = true
70
+
71
+ else
72
+ fail ArgumentError, "#{method_name}: cannot dispatch argument `#{name}', " +
73
+ "(type: #{type}) from Hash"
74
+ end
75
+ end
76
+
77
+ if has_kwrest
78
+ kwargs.merge!(data.map { |k,v| [k.to_sym, v] }.to_h)
79
+ elsif rest_index
80
+ args[rest_index, 0] = data
81
+ end
82
+
83
+ if kwargs.empty?
84
+ send(method_name, *args)
85
+ else
86
+ send(method_name, *args, **kwargs)
87
+ end
88
+ end
89
+ end
90
+
91
+ ActionCable::Channel::Base.extend(ActioncableAutoParam::ClassMethods)
92
+ ActionCable::Channel::Base.prepend(ActioncableAutoParam)
@@ -0,0 +1,3 @@
1
+ module ActioncableAutoParam
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: actioncable_auto_param
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Mike A. Owens
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-08-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: actioncable
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.0.3
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.0.3
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.10'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.10'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description:
70
+ email:
71
+ - mike@meter.md
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".travis.yml"
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - actioncable_auto_param.gemspec
83
+ - bin/console
84
+ - bin/setup
85
+ - lib/actioncable_auto_param.rb
86
+ - lib/actioncable_auto_param/version.rb
87
+ homepage: https://github.com/metermd/actioncable_auto_param
88
+ licenses:
89
+ - MIT
90
+ metadata: {}
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ requirements: []
106
+ rubyforge_project:
107
+ rubygems_version: 2.4.8
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: Automatically extracts ActionCable objects into parameters
111
+ test_files: []