default_options 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: 2df275a34037e316ac5aeda858bc4c4fc4c2f446
4
+ data.tar.gz: d1569d20650db07a6549d4689faaca9b4b804db8
5
+ SHA512:
6
+ metadata.gz: 4e31dfafe075d4270cd604a6369ed4745350064c0dfa58402ddcee01e904b4909fc04ff505f5c3e1d7606ca372d5a8a6a6728f238428367f6e614055d9100818
7
+ data.tar.gz: a04c893ca72a9af025687f8b6cfccd9e2b4015cb96b96fee6fe39a2e93d2a8a2fc407737e4fac6076f3fdd82880d0cf425ad777f15852d470b95352054e80442
@@ -0,0 +1,22 @@
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
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in default_options.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 John Mertens
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.
@@ -0,0 +1,73 @@
1
+ # DefaultOptions
2
+
3
+ Super simple library provides an easy DSL for setting default options in methods.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'default_options'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install default_options
18
+
19
+ ## Usage
20
+
21
+ Let's say you have a method that returns the number of views a particular blog post receives. This
22
+ method also accepts a hash of options to allow use to limit the number of views returned to a
23
+ particular date range:
24
+
25
+ **This is a Rails example, but the library works in any Ruby project**
26
+
27
+ ```
28
+ def blog_views(opts = {})
29
+ default_options = {
30
+ :start_date => self.created_at,
31
+ :end_date => Time.zone.now
32
+ }
33
+ options = opts.reverse_merge(default_options)
34
+
35
+ self.views.where(:start_date => options[:start_date], :end_date => options[:end_date]).count
36
+ end
37
+ ```
38
+
39
+ With DefaultOptions, this same code would look like:
40
+
41
+ ```
42
+ def blog_views(opts = {})
43
+ options = defaults_for opts do
44
+ start_date self.created_at
45
+ end_date Time.zone.now
46
+ end
47
+
48
+ self.views.where(:start_date => options.start_date, :end_date => options.end_date).count
49
+ end
50
+ ```
51
+
52
+ To add this functionality to any Ruby class, just include the library.
53
+
54
+ ```
55
+ require 'default_options' # If you are using Rails, you don't need this line
56
+
57
+ class AwesomeSauce
58
+ include DefautOptions
59
+
60
+ def rad_thing(opts = {})
61
+
62
+ end
63
+ end
64
+ ```
65
+
66
+
67
+ ## Contributing
68
+
69
+ 1. Fork it ( https://github.com/mertonium/default_options/fork )
70
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
71
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
72
+ 4. Push to the branch (`git push origin my-new-feature`)
73
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -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 'default_options/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "default_options"
8
+ spec.version = DefaultOptions::VERSION
9
+ spec.authors = ["John Mertens"]
10
+ spec.email = ["john@versahq.com"]
11
+ spec.summary = %q{Super simple library provides an easy DSL for setting defaults on optional methods arguments.}
12
+ spec.description = %q{Implements a simple pattern for creating "options" in Ruby method. Mix it in to a class and use the DSL to setup defaults for methods that can easily be overwritten as needed.}
13
+ spec.homepage = "https://github.com/mertonium/default_options"
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.6"
22
+ spec.add_development_dependency "rspec", "~> 3.0", ">= 3.0.0"
23
+ end
@@ -0,0 +1,18 @@
1
+ require "default_options/version"
2
+ require "default_options/parser"
3
+
4
+ module DefaultOptions
5
+ module Macros
6
+ def defaults_for(opts, &block)
7
+ DefaultOptions::Parser.new opts, &block
8
+ end
9
+ end
10
+
11
+ def self.included(base)
12
+ base.extend(Macros)
13
+ end
14
+
15
+ def defaults_for(opts, &block)
16
+ DefaultOptions::Parser.new opts, &block
17
+ end
18
+ end
@@ -0,0 +1,27 @@
1
+ module DefaultOptions
2
+ class Parser
3
+ def initialize(orides, &block)
4
+ @defaults = {}
5
+ overrides(orides.dup)
6
+ instance_eval(&block)
7
+ end
8
+
9
+ def overrides(orides)
10
+ @overrides = orides
11
+ end
12
+
13
+ def method_missing(method_name, *args)
14
+ if args.size > 0
15
+ @defaults[method_name] = args.first
16
+ else
17
+ if @overrides.has_key? method_name
18
+ @overrides[method_name]
19
+ else
20
+ @defaults[method_name]
21
+ end
22
+ end
23
+ end
24
+ end
25
+
26
+
27
+ end
@@ -0,0 +1,3 @@
1
+ module DefaultOptions
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,41 @@
1
+ require 'helper'
2
+
3
+ class DummyClass
4
+ def self.dummy_method(opts = {})
5
+ options = DefaultOptions::Parser.new opts do
6
+ thing1 "foo"
7
+ thing2 "bar"
8
+ end
9
+
10
+ return options
11
+ end
12
+ end
13
+
14
+ describe DefaultOptions::Parser do
15
+ context "when no overrides are given" do
16
+ it "initializes the default options" do
17
+ result = DummyClass.dummy_method
18
+ expect(result.thing1).to eq('foo')
19
+ expect(result.thing2).to eq('bar')
20
+ end
21
+ end
22
+
23
+ context "when simple overrides are given" do
24
+ it "merges the defaults with the overrides" do
25
+ result = DummyClass.dummy_method(:thing1 => 'baz')
26
+ expect(result.thing1).to eq('baz')
27
+ expect(result.thing2).to eq('bar')
28
+ end
29
+ end
30
+
31
+ context "when non-defaulted overrides are given" do
32
+ it "merges the defaults with the overrides" do
33
+ result = DummyClass.dummy_method(:thing3 => 'baz')
34
+ expect(result.thing1).to eq('foo')
35
+ expect(result.thing2).to eq('bar')
36
+ expect(result.thing3).to eq('baz')
37
+ end
38
+ end
39
+ end
40
+
41
+
@@ -0,0 +1,2 @@
1
+ require 'default_options'
2
+ require 'rspec'
@@ -0,0 +1,45 @@
1
+ require 'helper'
2
+
3
+ class DummyClass2
4
+ include DefaultOptions
5
+ def self.dummy_class_method(opts = {})
6
+ options = defaults_for opts do
7
+ thing1 "foo"
8
+ thing2 "bar"
9
+ end
10
+
11
+ return options
12
+ end
13
+
14
+ def dummy_instance_method(opts = {})
15
+ options = defaults_for opts do
16
+ thing1 "foo"
17
+ thing2 "bar"
18
+ end
19
+
20
+ return options
21
+ end
22
+ end
23
+
24
+ describe DefaultOptions, :new_baktun => true do
25
+ describe ".default_for" do
26
+ context "when no overrides are given" do
27
+ it "initializes the default options" do
28
+ result = DummyClass2.dummy_class_method
29
+ expect(result.thing1).to eq('foo')
30
+ expect(result.thing2).to eq('bar')
31
+ end
32
+ end
33
+ end
34
+
35
+ describe "#default_for" do
36
+ context "when no overrides are given" do
37
+ it "initializes the default options" do
38
+ result = DummyClass2.new.dummy_instance_method
39
+ expect(result.thing1).to eq('foo')
40
+ expect(result.thing2).to eq('bar')
41
+ end
42
+ end
43
+ end
44
+ end
45
+
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: default_options
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - John Mertens
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-28 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.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
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.0'
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: 3.0.0
37
+ type: :development
38
+ prerelease: false
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - "~>"
42
+ - !ruby/object:Gem::Version
43
+ version: '3.0'
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: 3.0.0
47
+ description: Implements a simple pattern for creating "options" in Ruby method. Mix
48
+ it in to a class and use the DSL to setup defaults for methods that can easily be
49
+ overwritten as needed.
50
+ email:
51
+ - john@versahq.com
52
+ executables: []
53
+ extensions: []
54
+ extra_rdoc_files: []
55
+ files:
56
+ - ".gitignore"
57
+ - Gemfile
58
+ - LICENSE.txt
59
+ - README.md
60
+ - Rakefile
61
+ - default_options.gemspec
62
+ - lib/default_options.rb
63
+ - lib/default_options/parser.rb
64
+ - lib/default_options/version.rb
65
+ - spec/default_options/parser_spec.rb
66
+ - spec/helper.rb
67
+ - spec/macros_spec.rb
68
+ homepage: https://github.com/mertonium/default_options
69
+ licenses:
70
+ - MIT
71
+ metadata: {}
72
+ post_install_message:
73
+ rdoc_options: []
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ requirements: []
87
+ rubyforge_project:
88
+ rubygems_version: 2.2.2
89
+ signing_key:
90
+ specification_version: 4
91
+ summary: Super simple library provides an easy DSL for setting defaults on optional
92
+ methods arguments.
93
+ test_files:
94
+ - spec/default_options/parser_spec.rb
95
+ - spec/helper.rb
96
+ - spec/macros_spec.rb