defaultinator 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
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 @@
1
+ --colour
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in defaultinator.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 EmberAds Ltd <hello@emberads.com>
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,58 @@
1
+ # Defaultinator
2
+
3
+ Allows you to set default values when setting attributes using `attr_accessor` or `attr_reader`. Saves having to define attributes then instantly override them to set a default method.
4
+
5
+ Turns this:
6
+
7
+ class Foo
8
+ attr_accessor :things
9
+ def things
10
+ @things ||= []
11
+ end
12
+ end
13
+
14
+ into this:
15
+
16
+ class Foo
17
+ attr_accessor :things, default: []
18
+ end
19
+
20
+ Also works with `attr_accessor` or `attr_reader` in modules, and with passing multiple attributes to define at once.
21
+
22
+ ## Installation
23
+
24
+ Add this line to your application's Gemfile:
25
+
26
+ gem 'defaultinator'
27
+
28
+ And then execute:
29
+
30
+ $ bundle
31
+
32
+ Or install it yourself as:
33
+
34
+ $ gem install defaultinator
35
+
36
+ ## Usage
37
+
38
+ If you're not setting default values for the attribute, then just use `attr_accessor` (or `attr_reader`) as normal including setting multiple attributes at once.
39
+
40
+ attr_accessor :foo
41
+ attr_accessor :sed, :fred
42
+ attr_reader :bar
43
+ attr_reader :george, :paul
44
+
45
+ If you want default values, it's much the same but just pass a hash as the last argument stating what the default value is.
46
+
47
+ attr_accessor :foo, default: true
48
+ attr_accessor :sed, :fred, default: []
49
+ attr_reader :bar, default: {}
50
+ attr_reader :george, :paul, default: false
51
+
52
+ ## Contributing
53
+
54
+ 1. Fork it
55
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
56
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
57
+ 4. Push to the branch (`git push origin my-new-feature`)
58
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/defaultinator/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Caius Durling"]
6
+ gem.email = ["caius@emberads.com"]
7
+ gem.description = %q{Allows you to set default values when setting attributes using attr_accessor or attr_reader.}
8
+ gem.summary = %q{Set default values on attributes in one line}
9
+ gem.homepage = "http://github.com/caius/defaultinator"
10
+
11
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
12
+ gem.files = `git ls-files`.split("\n")
13
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
14
+ gem.name = "defaultinator"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Defaultinator::VERSION
17
+
18
+ gem.add_development_dependency "rspec", "> 2.0.0"
19
+ end
@@ -0,0 +1,31 @@
1
+ require "defaultinator/version"
2
+
3
+ class Module
4
+ # todo: figure out how to mix this in as a module slightly nicer
5
+ alias_method :__original_attr_accessor, :attr_accessor
6
+ def attr_accessor *args
7
+ return send(:__original_attr_accessor, *args) unless args.last.is_a?(Hash)
8
+ *method_names, method_options = args
9
+ attr_reader(*method_names, method_options)
10
+ attr_writer(*method_names)
11
+ end
12
+
13
+ alias_method :__original_attr_reader, :attr_reader
14
+ def attr_reader *args
15
+ # Hand-off if no options for us to be clever with
16
+ return send(:__original_attr_reader, *args) unless args.last.is_a?(Hash)
17
+
18
+ *method_names, method_options = args
19
+
20
+ # Hand-off if no defaults or if the default is nil, because that's the ruby runtime default too.
21
+ return send(:__original_attr_reader, *method_names) unless method_options.has_key?(:default) && method_options[:default] != nil
22
+
23
+ method_names.uniq.each do |name|
24
+ instance_name = "@#{name}".to_sym
25
+ # @foo ||= {}
26
+ define_method(name) do
27
+ instance_variable_defined?(instance_name) && instance_variable_get(instance_name) || instance_variable_set(instance_name, method_options[:default])
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,3 @@
1
+ module Defaultinator
2
+ VERSION = "0.5.0"
3
+ end
@@ -0,0 +1,139 @@
1
+ require "defaultinator"
2
+
3
+ describe Defaultinator, "attributes defined in a class with no default" do
4
+ before do
5
+ @instance = Class.new do
6
+ attr_accessor :attribute_1
7
+ attr_accessor :attribute_2, :attribute_3
8
+ attr_reader :attribute_4
9
+ attr_reader :attribute_5, :attribute_6
10
+ end.new
11
+ end
12
+ describe "attr_accessor" do
13
+ it "should define the getters returning nil" do
14
+ @instance.attribute_1.should be_nil
15
+ @instance.attribute_2.should be_nil
16
+ @instance.attribute_3.should be_nil
17
+ end
18
+ it "should define the setters" do
19
+ @instance.attribute_1 = :foo
20
+ @instance.attribute_1.should be == :foo
21
+ @instance.attribute_2 = :foo
22
+ @instance.attribute_2.should be == :foo
23
+ @instance.attribute_3 = :foo
24
+ @instance.attribute_3.should be == :foo
25
+ end
26
+ end
27
+ describe "#attr_reader" do
28
+ it "should define the getters returning nil" do
29
+ @instance.attribute_4.should be_nil
30
+ @instance.attribute_5.should be_nil
31
+ @instance.attribute_6.should be_nil
32
+ end
33
+ end
34
+ end
35
+
36
+ describe Defaultinator, "attributes defined in a class with default" do
37
+ before do
38
+ @instance = Class.new do
39
+ attr_accessor :attribute_1, default: []
40
+ attr_accessor :attribute_2, :attribute_3, default: []
41
+ attr_reader :attribute_4, default: false
42
+ attr_reader :attribute_5, :attribute_6, default: false
43
+ end.new
44
+ end
45
+ describe "#attr_accessor" do
46
+ it "should define the getters returning default value" do
47
+ @instance.attribute_1.should be == []
48
+ @instance.attribute_2.should be == []
49
+ @instance.attribute_3.should be == []
50
+ end
51
+ it "should define the setters" do
52
+ @instance.attribute_1 = :foo
53
+ @instance.attribute_1.should be == :foo
54
+ @instance.attribute_2 = :foo
55
+ @instance.attribute_2.should be == :foo
56
+ @instance.attribute_3 = :foo
57
+ @instance.attribute_3.should be == :foo
58
+ end
59
+ end
60
+ describe "#attr_reader" do
61
+ it "should define the getters returning default value" do
62
+ @instance.attribute_4.should be_false
63
+ @instance.attribute_5.should be_false
64
+ @instance.attribute_6.should be_false
65
+ end
66
+ end
67
+ end
68
+
69
+ describe Defaultinator, "attributes defined in a module with no default" do
70
+ before do
71
+ mod = Module.new do
72
+ attr_accessor :attribute_1
73
+ attr_accessor :attribute_2, :attribute_3
74
+ attr_reader :attribute_4
75
+ attr_reader :attribute_5, :attribute_6
76
+ end
77
+ @instance = Class.new do
78
+ include mod
79
+ end.new
80
+ end
81
+ describe "attr_accessor" do
82
+ it "should define the getters returning nil" do
83
+ @instance.attribute_1.should be_nil
84
+ @instance.attribute_2.should be_nil
85
+ @instance.attribute_3.should be_nil
86
+ end
87
+ it "should define the setters" do
88
+ @instance.attribute_1 = :foo
89
+ @instance.attribute_1.should be == :foo
90
+ @instance.attribute_2 = :foo
91
+ @instance.attribute_2.should be == :foo
92
+ @instance.attribute_3 = :foo
93
+ @instance.attribute_3.should be == :foo
94
+ end
95
+ end
96
+ describe "#attr_reader" do
97
+ it "should define the getters returning nil" do
98
+ @instance.attribute_4.should be_nil
99
+ @instance.attribute_5.should be_nil
100
+ @instance.attribute_6.should be_nil
101
+ end
102
+ end
103
+ end
104
+
105
+ describe Defaultinator, "attributes defined in a module with default" do
106
+ before do
107
+ mod = Module.new do
108
+ attr_accessor :attribute_1, default: []
109
+ attr_accessor :attribute_2, :attribute_3, default: []
110
+ attr_reader :attribute_4, default: false
111
+ attr_reader :attribute_5, :attribute_6, default: false
112
+ end
113
+ @instance = Class.new do
114
+ include mod
115
+ end.new
116
+ end
117
+ describe "#attr_accessor" do
118
+ it "should define the getters returning default value" do
119
+ @instance.attribute_1.should be == []
120
+ @instance.attribute_2.should be == []
121
+ @instance.attribute_3.should be == []
122
+ end
123
+ it "should define the setters" do
124
+ @instance.attribute_1 = :foo
125
+ @instance.attribute_1.should be == :foo
126
+ @instance.attribute_2 = :foo
127
+ @instance.attribute_2.should be == :foo
128
+ @instance.attribute_3 = :foo
129
+ @instance.attribute_3.should be == :foo
130
+ end
131
+ end
132
+ describe "#attr_reader" do
133
+ it "should define the getters returning default value" do
134
+ @instance.attribute_4.should be_false
135
+ @instance.attribute_5.should be_false
136
+ @instance.attribute_6.should be_false
137
+ end
138
+ end
139
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: defaultinator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.5.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Caius Durling
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-07-10 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &70255695206920 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>'
20
+ - !ruby/object:Gem::Version
21
+ version: 2.0.0
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70255695206920
25
+ description: Allows you to set default values when setting attributes using attr_accessor
26
+ or attr_reader.
27
+ email:
28
+ - caius@emberads.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - .gitignore
34
+ - .rspec
35
+ - Gemfile
36
+ - LICENSE
37
+ - README.md
38
+ - Rakefile
39
+ - defaultinator.gemspec
40
+ - lib/defaultinator.rb
41
+ - lib/defaultinator/version.rb
42
+ - spec/defaultinator_spec.rb
43
+ homepage: http://github.com/caius/defaultinator
44
+ licenses: []
45
+ post_install_message:
46
+ rdoc_options: []
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ! '>='
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ requirements: []
62
+ rubyforge_project:
63
+ rubygems_version: 1.8.15
64
+ signing_key:
65
+ specification_version: 3
66
+ summary: Set default values on attributes in one line
67
+ test_files:
68
+ - spec/defaultinator_spec.rb