flexcon 0.1.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: 33633eca47e1ede133d0cd488c05f5d028e04a97
4
+ data.tar.gz: 3318eadbd831c89f3e27f1bea1a591819fd75254
5
+ SHA512:
6
+ metadata.gz: e3b78f3586f7063179b662c7bb5fe02ea51ad102ce56989dcccbbd4688deafa938e1409daa14c0abeb9d4b6fdf00869ba08e2d518e617f00111f37f2453866a4
7
+ data.tar.gz: bf70e9ad2aafbefdbddd8fb01d425f843edab7df1ffed3fef66bc3e2cbe6d0523ad0b039d46c780504e41516a8d628bbdb395dfa8b90f23d971fa9410bf6da2b
@@ -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,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.2.2
5
+ before_install: gem install bundler -v 1.12.5
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in flexcon.gemspec
4
+ gemspec
@@ -0,0 +1,128 @@
1
+ # Flexcon
2
+
3
+ Flexible consignment of arguments to lambdas using scopes.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'flexcon'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install flexcon
20
+
21
+ ## Usage
22
+
23
+ And then execute:
24
+
25
+ $ bundle
26
+
27
+ Or install it yourself as:
28
+
29
+ $ gem install Flexcon
30
+
31
+ ## Basic Usage
32
+
33
+ Given a scope which could be an array, hash or an object, dispatch attributes of the scope into lambdas:
34
+
35
+ ```ruby
36
+ array_scope = ['John', 'Doe', 'Acme Corporation']
37
+ hash_scope = { last_name: 'Doe', first_name: 'John', company: 'Acme Corporation' }
38
+ object_scope = OpenStruct.new(hash_scope)
39
+
40
+ full_name = lambda do |first_name, last_name|
41
+ "#{first_name} #{last_name}"
42
+ end
43
+
44
+ employee = lambda do |first_name, last_name, company|
45
+ "#{first_name} #{last_name} of #{company}"
46
+ end
47
+
48
+ # The following returns 'John Doe'
49
+ Flexcon.dispatch(array_scope, full_name)
50
+ Flexcon.dispatch(hash_scope, full_name)
51
+ Flexcon.dispatch(object_scope, full_name)
52
+
53
+ # The following returns 'John Doe of Acme Corporation'
54
+ Flexcon.dispatch(array_scope, employee)
55
+ Flexcon.dispatch(hash_scope, employee)
56
+ Flexcon.dispatch(object_scope, employee)
57
+
58
+ ```
59
+
60
+ ## Advanced Usage
61
+
62
+ If the method signature needed for certain functions are difficult to predict, instead of enforcing a single method signature, allow methods to ask for what they need from a scope.
63
+
64
+ ```ruby
65
+ class Operation
66
+ def initialize
67
+ @steps = []
68
+ @named = {}
69
+ end
70
+
71
+ def step(name, &block)
72
+ @steps << block
73
+ @named[name] = block
74
+ end
75
+
76
+ def call(scope)
77
+ @steps.each do |step|
78
+ Flexcon.dispatch(scope, step)
79
+ end
80
+ end
81
+ end
82
+
83
+ class Scope
84
+ attr_accessor :params, :models
85
+
86
+ def initialize(params={})
87
+ self.params = params
88
+ self.models = {}
89
+ end
90
+ end
91
+
92
+ op = Operation.new
93
+
94
+ op.step :find_or_init_user do |params, models|
95
+ user = User.find_by(email: params[:user][:email])
96
+ user ||= User.new(params[:user])
97
+
98
+ models[:user] = user
99
+ end
100
+
101
+ op.step :find_or_init_student do |params, models|
102
+ student = Student.find_or_initialize_by(user: models[:user], university_id: params[:university_id])
103
+ student.assign_attributes(params[:student])
104
+
105
+ models[:student] = student
106
+ end
107
+
108
+ op.step :save_user do |models|
109
+ models[:user].save!
110
+ end
111
+
112
+ op.step :save_student do |models|
113
+ models[:student].save!
114
+ end
115
+
116
+ op.call(Scope.new({}))
117
+ ```
118
+
119
+ ## Development
120
+
121
+ 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.
122
+
123
+ 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).
124
+
125
+ ## Contributing
126
+
127
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/flexcon.
128
+
@@ -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 "flexcon"
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,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,32 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'flexcon/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "flexcon"
8
+ spec.version = Flexcon::VERSION
9
+ spec.authors = ["Rodette Pedro"]
10
+ spec.email = ["rpedro@jway.com"]
11
+
12
+ spec.summary = "Demand Lambda Arguments from a Scope"
13
+ spec.description = "Extract properties of a scope and pass it to a lambda function."
14
+ spec.homepage = "https://github.com/rcpedro/flexcon"
15
+
16
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
18
+ if spec.respond_to?(:metadata)
19
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
20
+ else
21
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
22
+ end
23
+
24
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ spec.bindir = "exe"
26
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ["lib"]
28
+
29
+ spec.add_development_dependency "bundler", "~> 1.12"
30
+ spec.add_development_dependency "rake", "~> 10.0"
31
+ spec.add_development_dependency "rspec", "~> 3.0"
32
+ end
@@ -0,0 +1,26 @@
1
+ require "flexcon/version"
2
+
3
+ module Flexcon
4
+ class << self
5
+ def dispatch(scope, handler, *props)
6
+ handler.call(*self.args(scope, handler, *props))
7
+ end
8
+
9
+ def args(scope, handler, *props)
10
+ props = self.argnames(handler) if props.empty?
11
+
12
+ getter = lambda do |index| scope[props[index]] end if scope.is_a?(Hash)
13
+ getter ||= lambda do |index| scope[index] end if scope.is_a?(Array)
14
+ getter ||= lambda do |index| scope.send(props[index]) end
15
+
16
+ limit = handler.arity
17
+ limit = props.size if limit == -1
18
+
19
+ return (0...limit).map(&getter)
20
+ end
21
+
22
+ def argnames(handler)
23
+ handler.parameters.map { |a| a[-1] }
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,3 @@
1
+ module Flexcon
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: flexcon
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Rodette Pedro
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-09-12 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.12'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.12'
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: Extract properties of a scope and pass it to a lambda function.
56
+ email:
57
+ - rpedro@jway.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - README.md
67
+ - Rakefile
68
+ - bin/console
69
+ - bin/setup
70
+ - flexcon.gemspec
71
+ - lib/flexcon.rb
72
+ - lib/flexcon/version.rb
73
+ homepage: https://github.com/rcpedro/flexcon
74
+ licenses: []
75
+ metadata:
76
+ allowed_push_host: https://rubygems.org
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
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: Demand Lambda Arguments from a Scope
97
+ test_files: []