mixes 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 226e7e13c0b7f569d886d1d9a5783c2a30aa7fcb
4
+ data.tar.gz: 0edb7e45d02c0b2c98ea6c35936ffdbb9a196236
5
+ SHA512:
6
+ metadata.gz: 65e66a2884f526d378bf3ac9fd03754cd238c5019a949e069ab2772f97ce992fd43573a5e1f7a24d9915e16cd9fd1c1e2e76b643899553984d4baaaa3644c69a
7
+ data.tar.gz: b58da2d0421299794b5b3205576b909bccb971f5cf548f7a1335c3eba713cb4686d357eb3afc0c759398c9edf08727592f4c359a2f78bcb59901cd4a4999f737
data/.gitignore ADDED
@@ -0,0 +1,16 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
15
+ .rspec
16
+ .rvmrc
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Kuba Łopusiński
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,79 @@
1
+ # Mixes
2
+
3
+ Simple Ruby objects mixin, with configuration.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'mixes'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install mixes
20
+
21
+ ## Usage
22
+ ### Plain object extending
23
+
24
+ Include your module inside any Ruby object using the `mixes` syntax:
25
+
26
+ ```ruby
27
+ module FooModule
28
+ def instance_method
29
+ end
30
+
31
+ module ClassMethods
32
+ def class_method
33
+ end
34
+ end
35
+ end
36
+
37
+ class FooClass
38
+ mixes FooModule
39
+ end
40
+ ```
41
+
42
+ `FooModule` module level methods would become instance methods within host object, and `FooModule::ClassMethods` methods would become class level methods.
43
+
44
+ ### Extending and configuring extended objects
45
+ You can pass a second argument to `mixes` method, holding the per-extension configuration values for module beeing mixed in.
46
+
47
+ ```ruby
48
+ class FooClass
49
+ mixes FooModule, :foo
50
+ # OR
51
+ mixes FooModule, {foo: :bar}
52
+ # OR
53
+ mixes FooModule, [:foo, :bar]
54
+ # OR
55
+ # any other configuration values
56
+ end
57
+ ```
58
+
59
+ This values would be available inside extended module throught the `mixin_configuration` class level accessor:
60
+
61
+ ```ruby
62
+ module FooModule
63
+ def self.included(base)
64
+ # prepare module host object based on values held by mixin_configuration
65
+ end
66
+ end
67
+
68
+ class FooClass
69
+ mixes FooModule, :foo
70
+ end
71
+ ```
72
+
73
+ ## Contributing
74
+
75
+ 1. Fork it ( https://github.com/siemakuba/mixes/fork )
76
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
77
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
78
+ 4. Push to the branch (`git push origin my-new-feature`)
79
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,19 @@
1
+ module Mixes
2
+ module Configurator
3
+ def perform(source, config = nil)
4
+ source.send(:extend, ::Mixes::Configurator::Extension)
5
+ source.mixin_configuration = config
6
+ end
7
+ module_function :perform
8
+
9
+ module Extension
10
+ def mixin_configuration=(value)
11
+ @mixin_configuration = value
12
+ end
13
+
14
+ def mixin_configuration
15
+ @mixin_configuration
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,9 @@
1
+ module Mixes
2
+ module Extender
3
+ def perform(target, source)
4
+ target.send(:extend, source.const_get(:ClassMethods)) if source.const_defined?(:ClassMethods)
5
+ target.send(:include, source)
6
+ end
7
+ module_function :perform
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module Mixes
2
+ VERSION = "0.1.0"
3
+ end
data/lib/mixes.rb ADDED
@@ -0,0 +1,12 @@
1
+ require_relative "mixes/version"
2
+ require_relative "mixes/configurator"
3
+ require_relative "mixes/extender"
4
+
5
+ module Mixes
6
+ def mixes(mixed_module, *config)
7
+ ::Mixes::Configurator.perform(mixed_module, *config)
8
+ ::Mixes::Extender.perform(self, mixed_module)
9
+ end
10
+ end
11
+
12
+ Object.extend(::Mixes)
data/mixes.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mixes/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "mixes"
8
+ spec.version = Mixes::VERSION
9
+ spec.authors = ["Kuba Łopusiński"]
10
+ spec.email = ["siemakuba@gmail.com"]
11
+ spec.summary = %q{Load a Mixin with initial configuration.}
12
+ spec.description = ""
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^spec/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rspec", "~> 3.2"
23
+ end
@@ -0,0 +1,73 @@
1
+ require 'mixes'
2
+
3
+ module FooBar
4
+ def foobar_instance_method
5
+ end
6
+
7
+ def self.included(base)
8
+ config = mixin_configuration
9
+
10
+ base.instance_eval do
11
+ define_method(:passed_configuration) do
12
+ config
13
+ end
14
+ end
15
+ end
16
+
17
+ module ClassMethods
18
+ def foobar_class_method
19
+ end
20
+ end
21
+ end
22
+
23
+ describe "mixed_with" do
24
+ let(:foo_class) do
25
+ Class.new do
26
+ mixes ::FooBar, :foo_config
27
+ end
28
+ end
29
+
30
+ let(:bar_class) do
31
+ Class.new do
32
+ mixes ::FooBar, :bar_config
33
+ end
34
+ end
35
+
36
+ let(:baz_class) do
37
+ Class.new do
38
+ mixes ::FooBar
39
+ end
40
+ end
41
+
42
+ context "on class level" do
43
+ it "includes FooBar module" do
44
+ expect(foo_class.included_modules).to include(FooBar)
45
+ expect(bar_class.included_modules).to include(FooBar)
46
+ expect(baz_class.included_modules).to include(FooBar)
47
+ end
48
+
49
+ it "extends FooBar::ClassMethods" do
50
+ expect(foo_class.methods(true)).to include(:foobar_class_method)
51
+ expect(bar_class.methods(true)).to include(:foobar_class_method)
52
+ expect(baz_class.methods(true)).to include(:foobar_class_method)
53
+ end
54
+ end
55
+
56
+ context "on instance level" do
57
+ subject(:foo_object) { foo_class.new }
58
+ subject(:bar_object) { bar_class.new }
59
+ subject(:baz_object) { baz_class.new }
60
+
61
+ it "includes FooBar module methods" do
62
+ expect(foo_object.methods(true)).to include(:foobar_instance_method)
63
+ expect(bar_object.methods(true)).to include(:foobar_instance_method)
64
+ expect(baz_object.methods(true)).to include(:foobar_instance_method)
65
+ end
66
+
67
+ it "passes configuration for each inclusion" do
68
+ expect(foo_object.passed_configuration).to eql(:foo_config)
69
+ expect(bar_object.passed_configuration).to eql(:bar_config)
70
+ expect(baz_object.passed_configuration).to eql(nil)
71
+ end
72
+ end
73
+ end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mixes
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Kuba Łopusiński
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-01-19 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.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.2'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.2'
41
+ description: ''
42
+ email:
43
+ - siemakuba@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - lib/mixes.rb
54
+ - lib/mixes/configurator.rb
55
+ - lib/mixes/extender.rb
56
+ - lib/mixes/version.rb
57
+ - mixes.gemspec
58
+ - spec/mixes_spec.rb
59
+ homepage: ''
60
+ licenses:
61
+ - MIT
62
+ metadata: {}
63
+ post_install_message:
64
+ rdoc_options: []
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ requirements: []
78
+ rubyforge_project:
79
+ rubygems_version: 2.4.3
80
+ signing_key:
81
+ specification_version: 4
82
+ summary: Load a Mixin with initial configuration.
83
+ test_files:
84
+ - spec/mixes_spec.rb