assembler 1.0.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: 710b5f3b455cad26bf924085ddb529d5f0f2be3d
4
+ data.tar.gz: 411e7b9d2ff6cce59cc57b8eb65b236aec9806af
5
+ SHA512:
6
+ metadata.gz: 0857a9705d3ed323475d2629853ca0aec90bec6572b3e9cf1693a8017202cd12150a23902e07218a191044f5b0c671f49a0c924f1b59121ba10259cd6e26b01d
7
+ data.tar.gz: 2a26f698cf4103bed5f8521c5e21764731eee1ccacafbbbaba84733a2a9b10f4d1b9f450a7997964ed1b0d2e85ea1bc8fff90ca16589349e809c6093e590c022
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
data/.travis.yml ADDED
@@ -0,0 +1,9 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.1
4
+ - 2.1.0
5
+ - 2.0.0
6
+ # - 1.9.3
7
+ # - jruby-19mode
8
+ script:
9
+ - bundle exec rake
data/CHANGES.md ADDED
@@ -0,0 +1,5 @@
1
+ # Changes
2
+
3
+ ## 1.0.0
4
+
5
+ * Initial Release. (Ben Hamill)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in assembler.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Ben Hamill
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,98 @@
1
+ # Assembler
2
+ [![Build Status](https://travis-ci.org/benhamill/assembler.png)](https://travis-ci.org/benhamill/assembler)
3
+ [![Code Climate](https://codeclimate.com/github/benhamill/assembler.png)](https://codeclimate.com/github/benhamill/assembler)
4
+
5
+ An [assembler swarm](http://en.wikipedia.org/wiki/Molecular_assembler) is a
6
+ bunch of nanomachines that can build [almost anything](http://en.wikipedia.org/wiki/Molecular_nanotechnology#Assemblers_versus_nanofactories).
7
+
8
+ Assembler is a library that gives you a DSL to describe a super-handy
9
+ initializer pattern. You specify the parameters your object should take and
10
+ Assembler give you an initializer that takes an options hash as well as yielding
11
+ a [builder object](http://c2.com/cgi/wiki?BuilderPattern) to a block. It takes
12
+ care of storing the parameters and gives you private accessors, too.
13
+
14
+
15
+ ## Usage
16
+
17
+ You use it like this:
18
+
19
+ ```ruby
20
+ class AwesomeThing
21
+ extend Assembler
22
+
23
+ assembler_initializer :required_param, optional_param: 'default value'
24
+
25
+ # Additional business logic here...
26
+ end
27
+ ```
28
+
29
+ Then you can instantiate your object with either an options hash or via a block.
30
+ For example:
31
+
32
+ ```ruby
33
+ # These two are equivalent:
34
+ AwesomeThing.new(required_param: 'specialness')
35
+ AwesomeThing.new do |aw|
36
+ aw.required_param = 'specialness'
37
+ end
38
+
39
+ # These two are equivalent:
40
+ AwesomeThing.new(required_param: 'specialness', optional_param: 'override')
41
+ AwesomeThing.new do |aw|
42
+ aw.required_param = 'specialness'
43
+ aw.optional_param = 'override'
44
+ end
45
+ ```
46
+
47
+ This enables some trickery when you're dealing with a world of uncertainty:
48
+
49
+ ```ruby
50
+ class Foo
51
+ extend Assembler
52
+ assembler_initializer :name, :awesome, favorite_color: 'green'
53
+
54
+ def awesome?
55
+ !!awesome
56
+ end
57
+ end
58
+
59
+ def delegating_method(name, awesome=true, favorite_color=nil)
60
+ Foo.new do |foo|
61
+ foo.name = name
62
+ foo.awesome = awesome
63
+
64
+ foo.favorite_color = favorite_color if favorite_color
65
+ end
66
+ end
67
+ ```
68
+
69
+ The delegating method, here, is empowered to reverse the default for `awesome`,
70
+ but then respect the default for `favorite_color` if the calling code doesn't
71
+ pass anything in (assuming `nil` is unacceptable). It also respects if `awesome`
72
+ has a falsey value passed in.
73
+
74
+ Especially when you have objects with a lot of potential arguments being passed
75
+ in and don't want to pass keys that you don't have any information about (did
76
+ my caller pass in this `nil`, or is it my own default?), you can use conditional
77
+ logic in the block, rather than conditionally build of a hash just to pass to a
78
+ constructor method.
79
+
80
+
81
+ ## Contributing
82
+
83
+ Help is gladly welcomed. If you have a feature you'd like to add, it's much more
84
+ likely to get in (or get in faster) the closer you stick to these steps:
85
+
86
+ 1. Open an Issue to talk about it. We can discuss whether it's the right
87
+ direction or maybe help track down a bug, etc.
88
+ 1. Fork the project, and make a branch to work on your feature/fix. Master is
89
+ where you'll want to start from.
90
+ 1. Turn the Issue into a Pull Request. There are several ways to do this, but
91
+ [hub](https://github.com/defunkt/hub) is probably the easiest.
92
+ 1. Make sure your Pull Request includes tests.
93
+ 1. Bonus points if your Pull Request updates `CHANGES.md` to include a summary
94
+ of your changes and your name like the other entries. If the last entry is
95
+ the last release, add a new `## Unreleased` heading at the top.
96
+
97
+ If you don't know how to fix something, even just a Pull Request that includes a
98
+ failing test can be helpful. If in doubt, make an Issue to discuss.
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rspec/core/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task :default => :spec
data/assembler.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'assembler/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "assembler"
8
+ spec.version = Assembler::VERSION
9
+ spec.authors = ["Ben Hamill"]
10
+ spec.email = ["git-commits@benhamill.com"]
11
+ spec.summary = %q{Block-based initializers for your objects.}
12
+ spec.description = %q{Provides a DSL for describing required and optional (with defaults) parameters for object initialization. The initializer accepts an options hash and/or yields a builder object to a block.}
13
+ spec.homepage = "https://github.com/benhamill/assembler#readme"
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{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.5"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "pry"
24
+ spec.add_development_dependency "rspec"
25
+ end
@@ -0,0 +1,27 @@
1
+ module Assembler
2
+ class Builder
3
+ def initialize(*parameter_names)
4
+ @parameter_names = parameter_names
5
+
6
+ parameter_names.each do |parameter_name|
7
+ self.singleton_class.class_eval(<<-RUBY)
8
+ def #{parameter_name}=(value)
9
+ parameters[:#{parameter_name.to_sym}] = value
10
+ end
11
+ RUBY
12
+ end
13
+ end
14
+
15
+ def to_h
16
+ parameters
17
+ end
18
+
19
+ private
20
+
21
+ attr_reader :parameter_names
22
+
23
+ def parameters
24
+ @parameters ||= {}
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,39 @@
1
+ require "assembler/builder"
2
+ require "assembler/parameters"
3
+
4
+ module Assembler
5
+ module Initializer
6
+ def initialize(options={})
7
+ builder = Assembler::Builder.new(*self.class.all_param_names)
8
+
9
+ yield builder if block_given?
10
+
11
+ @full_options = Assembler::Parameters.new(options.merge(builder.to_h))
12
+
13
+ missing_required_params = []
14
+
15
+ self.class.required_params.each do |param_name|
16
+ remember_value_or(param_name) { missing_required_params << param_name }
17
+ end
18
+
19
+ self.class.optional_params.each do |param_name, default_value|
20
+ remember_value_or(param_name) { default_value }
21
+ end
22
+
23
+ raise(ArgumentError, "missing keywords: #{missing_required_params.join(', ')}") if missing_required_params.any?
24
+ end
25
+
26
+ private
27
+
28
+ attr_reader :full_options
29
+
30
+ def remember_value_or(param_name, &block)
31
+ instance_variable_set(
32
+ :"@#{param_name}",
33
+ full_options.fetch(param_name) do
34
+ block.call
35
+ end
36
+ )
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,19 @@
1
+ module Assembler
2
+ class Parameters
3
+ def initialize(params={})
4
+ @params = params
5
+ end
6
+
7
+ def fetch(key, &block)
8
+ params.fetch(key.to_sym) do
9
+ params.fetch(key.to_s) do
10
+ block.call
11
+ end
12
+ end
13
+ end
14
+
15
+ private
16
+
17
+ attr_reader :params
18
+ end
19
+ end
@@ -0,0 +1,3 @@
1
+ module Assembler
2
+ VERSION = "1.0.0"
3
+ end
data/lib/assembler.rb ADDED
@@ -0,0 +1,17 @@
1
+ require "assembler/version"
2
+ require "assembler/initializer"
3
+
4
+ module Assembler
5
+ attr_reader :required_params, :optional_params, :all_param_names
6
+
7
+ def assembler_initializer(*required, **optional)
8
+ include Assembler::Initializer
9
+
10
+ @required_params = required
11
+ @optional_params = optional
12
+ @all_param_names = (required + optional.keys).map(&:to_sym)
13
+
14
+ attr_reader *all_param_names
15
+ private *all_param_names
16
+ end
17
+ end
@@ -0,0 +1,87 @@
1
+ require 'spec_helper'
2
+ require 'assembler'
3
+
4
+ describe Assembler do
5
+ describe "#assembler_initializer" do
6
+ context "without parameters" do
7
+ subject do
8
+ Class.new do
9
+ extend Assembler
10
+
11
+ assembler_initializer
12
+ end
13
+ end
14
+
15
+ it "throws away all input parameters (method arguments)" do
16
+ built_object = subject.new(foo: 'foo', bar: 'bar')
17
+
18
+ expect(built_object.instance_variables).to_not include(:@foo, :@bar)
19
+ end
20
+
21
+ it "doesn't have methods on the builder object" do
22
+ subject.new do |builder|
23
+ expect(builder).to_not respond_to(:foo)
24
+ end
25
+ end
26
+ end
27
+
28
+ context "with a mix of required and optional parameters" do
29
+ subject do
30
+ Class.new do
31
+ extend Assembler
32
+
33
+ assembler_initializer :foo, bar: 'bar'
34
+ end
35
+ end
36
+
37
+ it "barfs from missing required parameters" do
38
+ expect { subject.new }.to raise_error(ArgumentError)
39
+ end
40
+
41
+ it "uses default values for missing parameters (method arguments)" do
42
+ built_object = subject.new(foo: 'foo')
43
+
44
+ expect(built_object.instance_variable_get(:@bar)).to eq('bar')
45
+ end
46
+
47
+ it "uses default values for missing parameters (block)" do
48
+ built_object = subject.new do |builder|
49
+ builder.foo = 'foo'
50
+ end
51
+
52
+ expect(built_object.instance_variable_get(:@bar)).to eq('bar')
53
+ end
54
+
55
+ it "holds onto the parameters (method arguments)" do
56
+ built_object = subject.new(foo: 'baz', bar: 'qux')
57
+
58
+ expect(built_object.instance_variable_get(:@foo)).to eq('baz')
59
+ expect(built_object.instance_variable_get(:@bar)).to eq('qux')
60
+ end
61
+
62
+ it "holds onto the parameters (block)" do
63
+ built_object = subject.new do |builder|
64
+ builder.foo = 'baz'
65
+ builder.bar = 'qux'
66
+ end
67
+
68
+ expect(built_object.instance_variable_get(:@foo)).to eq('baz')
69
+ expect(built_object.instance_variable_get(:@bar)).to eq('qux')
70
+ end
71
+
72
+ it "ignores un-named parameters in method arguments" do
73
+ built_object = subject.new(foo: 'bar', baz: 'baz')
74
+
75
+ expect(built_object.instance_variables).to_not include(:@baz)
76
+ end
77
+
78
+ it "doesn't create builder methods for un-named parameters" do
79
+ expect {
80
+ subject.new do |builder|
81
+ builder.baz = 'baz'
82
+ end
83
+ }.to raise_error(NoMethodError)
84
+ end
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,27 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ RSpec.configure do |config|
8
+ config.treat_symbols_as_metadata_keys_with_true_values = true
9
+ config.run_all_when_everything_filtered = true
10
+ config.filter_run :focus
11
+
12
+ # Run specs in random order to surface order dependencies. If you find an
13
+ # order dependency and want to debug it, you can fix the order by providing
14
+ # the seed, which is printed after each run.
15
+ # --seed 1234
16
+ config.order = 'random'
17
+
18
+ config.expect_with :rspec do |expectations|
19
+ expectations.syntax = :expect
20
+ end
21
+
22
+ config.mock_with :rspec do |mocks|
23
+ mocks.syntax = :expect
24
+ end
25
+ end
26
+
27
+ require 'pry'
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: assembler
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Ben Hamill
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-01 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.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry
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
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
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: Provides a DSL for describing required and optional (with defaults) parameters
70
+ for object initialization. The initializer accepts an options hash and/or yields
71
+ a builder object to a block.
72
+ email:
73
+ - git-commits@benhamill.com
74
+ executables: []
75
+ extensions: []
76
+ extra_rdoc_files: []
77
+ files:
78
+ - ".gitignore"
79
+ - ".rspec"
80
+ - ".travis.yml"
81
+ - CHANGES.md
82
+ - Gemfile
83
+ - LICENSE.txt
84
+ - README.md
85
+ - Rakefile
86
+ - assembler.gemspec
87
+ - lib/assembler.rb
88
+ - lib/assembler/builder.rb
89
+ - lib/assembler/initializer.rb
90
+ - lib/assembler/parameters.rb
91
+ - lib/assembler/version.rb
92
+ - spec/assembler_spec.rb
93
+ - spec/spec_helper.rb
94
+ homepage: https://github.com/benhamill/assembler#readme
95
+ licenses:
96
+ - MIT
97
+ metadata: {}
98
+ post_install_message:
99
+ rdoc_options: []
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ requirements: []
113
+ rubyforge_project:
114
+ rubygems_version: 2.2.2
115
+ signing_key:
116
+ specification_version: 4
117
+ summary: Block-based initializers for your objects.
118
+ test_files:
119
+ - spec/assembler_spec.rb
120
+ - spec/spec_helper.rb