composed 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: 57e4dee22e5da987b4f9d1c4e81bbe57feee1c0a
4
+ data.tar.gz: b29b251a09aab00c8f9fd144bc9185d05d38b189
5
+ SHA512:
6
+ metadata.gz: 26fe30da18033f53d77f609f15989b7955ed778cd53d41226c36af248eb6b42673cdc9ad805c854f9f5a63e487ee9c58d24b139e7badba9b4fe0c8ec63e3580c
7
+ data.tar.gz: a492c7861d47b5321a502ee361c7390dc2e0f05d53b694414080faee4910322d5228424552be77092acd8b5db9155521acc68aee8edcbc7a16b0e7d76b3ba557
data/.gitignore ADDED
@@ -0,0 +1,14 @@
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
13
+ .byebug_history
14
+ *.gem
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.1
5
+ before_install: gem install bundler -v 1.14.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in composed.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,4 @@
1
+ guard :rspec, cmd: 'bundle exec rspec' do
2
+ watch(%r{^spec/lib}) { 'spec' }
3
+ watch(%r{^lib/}) { 'spec' }
4
+ end
data/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2017 Justin Scott
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,54 @@
1
+ # Composed
2
+
3
+ This is a small utility to help you define new types purely through **composition**. This way you can build all the small objects you want and put them together in a simple way.
4
+
5
+ ## Quick Example
6
+
7
+ ```ruby
8
+ #lib/report.rb
9
+ class Report
10
+ def initialize(config, filter:, output_format:)
11
+ @config = config
12
+ @output_format = output_format
13
+ @filter = filter
14
+ end
15
+
16
+ # ... etc ...
17
+ end
18
+
19
+ #lib/json_report.rb
20
+ ImportantJSONReport = Composed(Report) do
21
+ dep :output_format { JSONFormatter.new }
22
+ dep :filter { ImportantStuffFilter.new }
23
+ end
24
+
25
+ #app/biz_report.rb
26
+ ImportantJSONReport.new(my_config).generate(data)
27
+ ```
28
+
29
+ ## Installation
30
+
31
+ Add this line to your application's Gemfile:
32
+
33
+ ```ruby
34
+ gem 'composed'
35
+ ```
36
+
37
+ And then execute:
38
+
39
+ $ bundle
40
+
41
+ Or install it yourself as:
42
+
43
+ $ gem install composed
44
+
45
+ ## Development
46
+
47
+ 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.
48
+
49
+ 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).
50
+
51
+ ## Contributing
52
+
53
+ Bug reports and pull requests are welcome on GitHub at https://github.com/dr0verride/composed.
54
+
data/Rakefile ADDED
@@ -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,40 @@
1
+ require 'bundler/setup'
2
+ require 'benchmark/ips'
3
+ require 'composed'
4
+
5
+ class AClass
6
+ def initialize(a:,b:,c:,d:); end
7
+ end
8
+
9
+ class InheritedClass < AClass
10
+ def initialize(a: 1, b: 2, c: 3, d: 4); super; end
11
+ end
12
+
13
+ module ModuleClass
14
+ def self.new(a: 1, b: 2, c: 3, d: 4)
15
+ AClass.new(a: a,b: b, c: c, d: d)
16
+ end
17
+ end
18
+
19
+ HalfComposed = Composed(AClass) do
20
+ dep :a { 2 }
21
+ dep :c { 3 }
22
+ end
23
+
24
+
25
+ FullComposed = Composed(AClass) do
26
+ dep :a { 0 }
27
+ dep :b { 1 }
28
+ dep :c { 2 }
29
+ dep :d { 3 }
30
+ end
31
+
32
+ Benchmark.ips do |x|
33
+ x.report("Normal Class") { AClass.new(a: 1, b: 2, c: 3, d: 4) }
34
+ x.report("Inheited Class") { InheritedClass.new }
35
+ x.report("Module Class") { ModuleClass.new }
36
+ x.report("Half Composed") { HalfComposed.new(b: 1, d: 2) }
37
+ x.report("Full Composed") { FullComposed.new }
38
+
39
+ x.compare!
40
+ end
@@ -0,0 +1,40 @@
1
+ require 'bundler/setup'
2
+ require 'benchmark/ips'
3
+ require 'composed'
4
+
5
+ class AClass
6
+ def initialize(a,b,c,d); end
7
+ end
8
+
9
+ class InheritedClass < AClass
10
+ def initialize(a = 1, b = 2, c = 3, d = 4); super; end
11
+ end
12
+
13
+ module ModuleClass
14
+ def self.new(a = 1, b = 2, c = 3, d = 4)
15
+ AClass.new(a,b,c,d)
16
+ end
17
+ end
18
+
19
+ HalfComposed = Composed(AClass) do
20
+ dep 2 { 2 }
21
+ dep 3 { 3 }
22
+ end
23
+
24
+
25
+ FullComposed = Composed(AClass) do
26
+ dep { 0 }
27
+ dep { 1 }
28
+ dep { 2 }
29
+ dep { 3 }
30
+ end
31
+
32
+ Benchmark.ips do |x|
33
+ x.report("Normal Class") { AClass.new(1,2,3,4) }
34
+ x.report("Inheited Class") { InheritedClass.new }
35
+ x.report("Module Class") { ModuleClass.new }
36
+ x.report("Half Composed") { HalfComposed.new(1,2) }
37
+ x.report("Full Composed") { FullComposed.new }
38
+
39
+ x.compare!
40
+ end
@@ -0,0 +1,42 @@
1
+ require 'bundler/setup'
2
+ require 'benchmark/ips'
3
+ require 'composed'
4
+
5
+ Composed::Positional.strategy = :skip
6
+
7
+ class AClass
8
+ def initialize(a,b,c,d); end
9
+ end
10
+
11
+ class InheritedClass < AClass
12
+ def initialize(a = 1, b = 2, c = 3, d = 4); super; end
13
+ end
14
+
15
+ module ModuleClass
16
+ def self.new(a = 1, b = 2, c = 3, d = 4)
17
+ AClass.new(a,b,c,d)
18
+ end
19
+ end
20
+
21
+ HalfComposed = Composed(AClass) do
22
+ dep 1 { 2 }
23
+ dep 2 { 3 }
24
+ end
25
+
26
+
27
+ FullComposed = Composed(AClass) do
28
+ dep { 0 }
29
+ dep { 1 }
30
+ dep { 2 }
31
+ dep { 3 }
32
+ end
33
+
34
+ Benchmark.ips do |x|
35
+ x.report("Normal Class") { AClass.new(1,2,3,4) }
36
+ x.report("Inheited Class") { InheritedClass.new }
37
+ x.report("Module Class") { ModuleClass.new }
38
+ x.report("Half Composed") { HalfComposed.new(1,2) }
39
+ x.report("Full Composed") { FullComposed.new }
40
+
41
+ x.compare!
42
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "composed"
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__)
data/bin/setup ADDED
@@ -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
data/composed.gemspec ADDED
@@ -0,0 +1,39 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'composed/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "composed"
8
+ spec.version = Composed::VERSION
9
+ spec.authors = ["Justin Scott"]
10
+ spec.email = ["jscott7561@gmail.com"]
11
+
12
+ spec.summary = "Tool for defining types or operations that are composed from other types."
13
+ spec.description = spec.description
14
+ spec.homepage = "https://github.com/dr0verride/composed"
15
+ spec.licenses = ['MIT']
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ else
21
+ raise "RubyGems 2.0 or newer is required to protect against " \
22
+ "public gem pushes."
23
+ end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
26
+ f.match(%r{^(test|spec|features)/})
27
+ end
28
+ spec.bindir = "exe"
29
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+
32
+ spec.add_development_dependency "bundler", "~> 1.14"
33
+ spec.add_development_dependency "rake", "~> 10.0"
34
+ spec.add_development_dependency "rspec", "~> 3.0"
35
+ spec.add_development_dependency "guard", "~> 2.14"
36
+ spec.add_development_dependency "guard-rspec", "~> 4.7"
37
+ spec.add_development_dependency "byebug", "~> 9.0"
38
+ spec.add_development_dependency "benchmark-ips", "~> 2.7"
39
+ end
data/lib/composed.rb ADDED
@@ -0,0 +1,74 @@
1
+ require 'composed/version'
2
+ require 'composed/positional'
3
+ require 'composed/keywords'
4
+
5
+ class Composed
6
+ def initialize(klass, interface = :new, &block)
7
+ raise ArgumentError, "Must provide a block to define Composed Type" unless block_given?
8
+ @klass = klass
9
+ @injected_pos_args = Positional.new
10
+ @injected_kw_args = Keywords.new
11
+ @interface = interface
12
+ @ctor = nil
13
+
14
+ instance_eval(&block)
15
+
16
+ singleton_class.send(:alias_method, interface, :execute_composition)
17
+ singleton_class.send(:public, interface, :execute_composition)
18
+
19
+ if interface == :call
20
+ singleton_class.send(:alias_method, :[], :execute_composition)
21
+ singleton_class.send(:public, :[])
22
+ end
23
+ end
24
+
25
+ private
26
+
27
+ def execute_composition(*args, **kwargs, &block)
28
+ args = @injected_pos_args.merge(args)
29
+
30
+ object = if kwargs.empty? && @injected_kw_args.empty?
31
+ @klass.send(@interface, *args, &block)
32
+ else
33
+ kwargs = @injected_kw_args.merge(kwargs)
34
+ @klass.send(@interface, *args, **kwargs, &block)
35
+ end
36
+
37
+ object.instance_eval(&@ctor) if @ctor
38
+ object
39
+ end
40
+
41
+ def define_dependency(keyword_or_idx = nil, &block)
42
+ case keyword_or_idx
43
+ when String
44
+ @injected_kw_args[keyword_or_idx.to_sym] = block
45
+ when Symbol
46
+ @injected_kw_args[keyword_or_idx] = block
47
+ when Integer
48
+ @injected_pos_args[keyword_or_idx] = block
49
+ else
50
+ @injected_pos_args << block
51
+ end
52
+ end
53
+
54
+ alias_method :dep, :define_dependency
55
+
56
+ def define_kw_dependency(keyword, block)
57
+ raise ArgumentError, <<~ERROR if @injected_kw_args.key?(keyword)
58
+ Cannot inject keyword argument \"#{keyword}\" more than once."
59
+ ERROR
60
+ @injected_kw_args[keyword] = block
61
+ end
62
+
63
+ def constructor(&block)
64
+ @ctor = block
65
+ end
66
+
67
+ module Macro
68
+ def Composed(*args, &block)
69
+ Composed.new(*args,&block)
70
+ end
71
+ end
72
+ end
73
+
74
+ include Composed::Macro
@@ -0,0 +1,27 @@
1
+
2
+ class Composed
3
+ class Keywords
4
+ def initialize
5
+ @injected = {}
6
+ end
7
+
8
+ def []=(keyword, value)
9
+ raise ArgumentError, <<~ERROR if @injected.key?(keyword)
10
+ Cannot inject keyword argument \"#{keyword}\" more than once."
11
+ ERROR
12
+ @injected[keyword] = value
13
+ end
14
+
15
+ def merge(args)
16
+ @injected.each do |key, value|
17
+ args[key] = value.call unless args.key?(key)
18
+ end
19
+
20
+ args
21
+ end
22
+
23
+ def empty?
24
+ @injected.empty?
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,100 @@
1
+
2
+ class Composed
3
+ class Positional
4
+ def initialize(merge_strategy = default_strategy)
5
+ @injected = {}
6
+ @index = 0
7
+ @merge_strategy = merge_strategy
8
+ end
9
+
10
+ def []=(idx, value)
11
+ @index = idx
12
+ push(value)
13
+ end
14
+
15
+ def push(value)
16
+ set(@index,value)
17
+ @index += 1
18
+ end
19
+
20
+ alias_method :<<, :push
21
+
22
+ def merge(args)
23
+ @merge_strategy.call(args,@injected)
24
+ end
25
+
26
+ private
27
+ def set(idx, value)
28
+ @injected[idx] = value
29
+ @injected = Hash[@injected.sort]
30
+ end
31
+
32
+ class OverrideStrategy
33
+ def call(args, injected)
34
+ missing = 0
35
+ injected.each do |idx,value|
36
+ if args.size == idx
37
+ args[idx] = value.call
38
+ elsif args.size < idx
39
+ missing += 1
40
+ end
41
+ end
42
+
43
+ raise ArgumentError, <<~ERROR if missing > 0
44
+ wrong number of arguments (given #{args.size}, expected #{args.size + missing}
45
+ ERROR
46
+
47
+ args
48
+ end
49
+ end
50
+
51
+ class SkipStrategy
52
+ def call(args, injected)
53
+ missing = 0
54
+ injected.each do |idx, value|
55
+ missing += 1 if args.size < idx
56
+ args.insert(idx,value.call)
57
+ end
58
+
59
+ raise ArgumentError, <<~ERROR if missing > 0
60
+ wrong number of arguments (given #{args.size}, expected #{args.size + missing}
61
+ ERROR
62
+
63
+ args
64
+ end
65
+ end
66
+
67
+ def default_strategy
68
+ self.class.strategy
69
+ end
70
+
71
+ class << self
72
+ STRATEGY_LOOKUP = {
73
+ skip: SkipStrategy,
74
+ override: OverrideStrategy,
75
+ default: OverrideStrategy
76
+ }
77
+
78
+ def strategy=(strategy)
79
+ @strategy = klass_for(strategy).new
80
+ end
81
+
82
+ def klass_for(strategy)
83
+ return strategy if strategy.respond_to?(:new)
84
+
85
+ STRATEGY_LOOKUP.fetch(strategy) { raise ArgumentError, <<~ERROR }
86
+ Unsupported strategy: #{strategy}. Use one of the following:
87
+ :skip - Skips over injected arguments. Cannot override injections at call time.
88
+ :override - Positional arguments behave as normal function calls. Defaults are overridden in order.
89
+ :default - Set the default. :override
90
+ ERROR
91
+
92
+ end
93
+ end
94
+
95
+ def self.strategy
96
+ @strategy ||= klass_for(:default).new
97
+ end
98
+
99
+ end
100
+ end
@@ -0,0 +1,3 @@
1
+ class Composed
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,160 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: composed
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Justin Scott
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-05-05 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.14'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.14'
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
+ - !ruby/object:Gem::Dependency
56
+ name: guard
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2.14'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '2.14'
69
+ - !ruby/object:Gem::Dependency
70
+ name: guard-rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '4.7'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '4.7'
83
+ - !ruby/object:Gem::Dependency
84
+ name: byebug
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '9.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '9.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: benchmark-ips
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '2.7'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '2.7'
111
+ description: ''
112
+ email:
113
+ - jscott7561@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".rspec"
120
+ - ".travis.yml"
121
+ - Gemfile
122
+ - Guardfile
123
+ - LICENSE.md
124
+ - README.md
125
+ - Rakefile
126
+ - benchmarks/keywords.rb
127
+ - benchmarks/positional_override.rb
128
+ - benchmarks/positional_skip.rb
129
+ - bin/console
130
+ - bin/setup
131
+ - composed.gemspec
132
+ - lib/composed.rb
133
+ - lib/composed/keywords.rb
134
+ - lib/composed/positional.rb
135
+ - lib/composed/version.rb
136
+ homepage: https://github.com/dr0verride/composed
137
+ licenses:
138
+ - MIT
139
+ metadata: {}
140
+ post_install_message:
141
+ rdoc_options: []
142
+ require_paths:
143
+ - lib
144
+ required_ruby_version: !ruby/object:Gem::Requirement
145
+ requirements:
146
+ - - ">="
147
+ - !ruby/object:Gem::Version
148
+ version: '0'
149
+ required_rubygems_version: !ruby/object:Gem::Requirement
150
+ requirements:
151
+ - - ">="
152
+ - !ruby/object:Gem::Version
153
+ version: '0'
154
+ requirements: []
155
+ rubyforge_project:
156
+ rubygems_version: 2.6.11
157
+ signing_key:
158
+ specification_version: 4
159
+ summary: Tool for defining types or operations that are composed from other types.
160
+ test_files: []