wrapit 0.2.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.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +31 -0
- data/Rakefile +7 -0
- data/lib/wrapit/attr_wrappable.rb +21 -0
- data/lib/wrapit/method_wrappable.rb +17 -0
- data/lib/wrapit/version.rb +3 -0
- data/lib/wrapit.rb +5 -0
- data/spec/spec_helper.rb +1 -0
- data/spec/wrapit/attr_wrappable_spec.rb +71 -0
- data/spec/wrapit/method_wrappable_spec.rb +52 -0
- data/wrapit.gemspec +25 -0
- metadata +133 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 JD Guzman
|
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,31 @@
|
|
1
|
+
# Wrapit
|
2
|
+
|
3
|
+
Wrapit allows you to easily define attributes in a class that should be wrapped,
|
4
|
+
or wrap attributes that have already been defined. The wrapping logic comes from
|
5
|
+
the [wrapped](https://github.com/mike-burns/wrapped) gem.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'wrapit'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle install
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install wrapit
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
TODO: Write usage instructions here
|
24
|
+
|
25
|
+
## Contributing
|
26
|
+
|
27
|
+
1. Fork it
|
28
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
29
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
30
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
31
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
module Wrapit::AttrWrappable
|
2
|
+
def self.included(base)
|
3
|
+
class << base
|
4
|
+
private
|
5
|
+
|
6
|
+
def attr_wrappable(*args)
|
7
|
+
attr_accessor *args.map { |m| "#{m.to_s}_naked".to_sym }
|
8
|
+
|
9
|
+
args.each do |method|
|
10
|
+
define_method method do
|
11
|
+
send(:"#{method}_naked").wrapped
|
12
|
+
end
|
13
|
+
|
14
|
+
define_method :"#{method}=" do |value|
|
15
|
+
send(:"#{method}_naked=", value)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Wrapit::MethodWrappable
|
2
|
+
def self.included(base)
|
3
|
+
class << base
|
4
|
+
private
|
5
|
+
|
6
|
+
def method_wrappable(*args)
|
7
|
+
class_eval do
|
8
|
+
args.each do |method|
|
9
|
+
define_method method do
|
10
|
+
super().wrapped
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
data/lib/wrapit.rb
ADDED
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'wrapit'
|
@@ -0,0 +1,71 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Wrapit::AttrWrappable do
|
4
|
+
it "should add attr_wrappable method when included" do
|
5
|
+
build_class
|
6
|
+
FooBar.private_methods.include?(:attr_wrappable).should be_true
|
7
|
+
destroy_class
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should add _naked attribute reader/writers with attr_wrappable" do
|
11
|
+
build_class
|
12
|
+
FooBar.module_eval { attr_wrappable :test_method }
|
13
|
+
FooBar.new.respond_to?(:test_method_naked).should be_true
|
14
|
+
FooBar.new.respond_to?(:test_method_naked=).should be_true
|
15
|
+
destroy_class
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should create a wrapped reader with attr_wrappable" do
|
19
|
+
build_class
|
20
|
+
FooBar.module_eval { attr_wrappable :test_method }
|
21
|
+
FooBar.new.test_method.should be_kind_of Blank
|
22
|
+
destroy_class
|
23
|
+
end
|
24
|
+
|
25
|
+
it "attr_wrappable should return instance of Blank when attribute nil" do
|
26
|
+
build_class
|
27
|
+
FooBar.module_eval { attr_wrappable :test_method }
|
28
|
+
foo_bar = FooBar.new
|
29
|
+
foo_bar.test_method = nil
|
30
|
+
foo_bar.test_method.should be_kind_of Blank
|
31
|
+
destroy_class
|
32
|
+
end
|
33
|
+
|
34
|
+
it "attr_wrappable should return instance of Present when attribute not nil" do
|
35
|
+
build_class
|
36
|
+
FooBar.module_eval { attr_wrappable :test_method }
|
37
|
+
foo_bar = FooBar.new
|
38
|
+
foo_bar.test_method = "something"
|
39
|
+
foo_bar.test_method.should be_kind_of Present
|
40
|
+
destroy_class
|
41
|
+
end
|
42
|
+
|
43
|
+
it "attr_wrappable _naked and wrapped readers should return same value" do
|
44
|
+
build_class
|
45
|
+
FooBar.module_eval { attr_wrappable :test_method }
|
46
|
+
foo_bar = FooBar.new
|
47
|
+
foo_bar.test_method = "something"
|
48
|
+
foo_bar.test_method.unwrap.should eq foo_bar.test_method_naked
|
49
|
+
destroy_class
|
50
|
+
end
|
51
|
+
|
52
|
+
def build_class
|
53
|
+
define_class 'FooBar', 'Object' do |klass|
|
54
|
+
klass.send :include, Wrapit::AttrWrappable
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def define_class(class_name, base, &block)
|
59
|
+
eval <<-CLASS_DEF
|
60
|
+
class ::#{class_name} < #{base}
|
61
|
+
end
|
62
|
+
CLASS_DEF
|
63
|
+
|
64
|
+
klass = Object.const_get(class_name)
|
65
|
+
klass.class_eval(&block) if block_given?
|
66
|
+
end
|
67
|
+
|
68
|
+
def destroy_class
|
69
|
+
Object.send :remove_const, 'FooBar'
|
70
|
+
end
|
71
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Wrapit::MethodWrappable do
|
4
|
+
it "should add method_wrappable method when included" do
|
5
|
+
build_class
|
6
|
+
FooBar.private_methods.include?(:method_wrappable).should be_true
|
7
|
+
destroy_class
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should create a wrapped version of method with method_wrappable" do
|
11
|
+
build_class
|
12
|
+
FooBar.module_eval { method_wrappable :to_s }
|
13
|
+
FooBar.new.to_s.should be_kind_of Present
|
14
|
+
destroy_class
|
15
|
+
end
|
16
|
+
|
17
|
+
it "wrapped method should return instance of Blank when nil returned" do
|
18
|
+
build_class
|
19
|
+
Object.module_eval { define_method :test_method do nil; end }
|
20
|
+
FooBar.module_eval { method_wrappable :test_method }
|
21
|
+
FooBar.new.test_method.should be_kind_of Blank
|
22
|
+
destroy_class
|
23
|
+
end
|
24
|
+
|
25
|
+
it "wrapped method should return instance of Present when anything other than nil is returned" do
|
26
|
+
build_class
|
27
|
+
Object.module_eval { define_method :test_method do "something"; end }
|
28
|
+
FooBar.module_eval { method_wrappable :test_method }
|
29
|
+
FooBar.new.test_method.should be_kind_of Present
|
30
|
+
destroy_class
|
31
|
+
end
|
32
|
+
|
33
|
+
def build_class
|
34
|
+
define_class 'FooBar', 'Object' do |klass|
|
35
|
+
klass.send :include, Wrapit::MethodWrappable
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def define_class(class_name, base, &block)
|
40
|
+
eval <<-CLASS_DEF
|
41
|
+
class ::#{class_name} < #{base}
|
42
|
+
end
|
43
|
+
CLASS_DEF
|
44
|
+
|
45
|
+
klass = Object.const_get(class_name)
|
46
|
+
klass.class_eval(&block) if block_given?
|
47
|
+
end
|
48
|
+
|
49
|
+
def destroy_class
|
50
|
+
Object.send :remove_const, 'FooBar'
|
51
|
+
end
|
52
|
+
end
|
data/wrapit.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 'wrapit/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "wrapit"
|
8
|
+
spec.version = Wrapit::VERSION
|
9
|
+
spec.authors = ["JD Guzman"]
|
10
|
+
spec.email = ["jdguzman@mac.com"]
|
11
|
+
spec.description = %q{Wrap attributes of any class to explicitly avoid nils.}
|
12
|
+
spec.summary = %q{This gem allows you to define attribtues that need to be wrapped, or wrap already defined attributes.}
|
13
|
+
spec.homepage = "https://github.com/jdguzman/wrapit"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_dependency "wrapped", "~> 0.0.2"
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
23
|
+
spec.add_development_dependency "rake"
|
24
|
+
spec.add_development_dependency "rspec"
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,133 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: wrapit
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- JD Guzman
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2014-06-10 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: wrapped
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 0.0.2
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 0.0.2
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: bundler
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '1.3'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '1.3'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rake
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: rspec
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
description: Wrap attributes of any class to explicitly avoid nils.
|
79
|
+
email:
|
80
|
+
- jdguzman@mac.com
|
81
|
+
executables: []
|
82
|
+
extensions: []
|
83
|
+
extra_rdoc_files: []
|
84
|
+
files:
|
85
|
+
- .gitignore
|
86
|
+
- Gemfile
|
87
|
+
- LICENSE.txt
|
88
|
+
- README.md
|
89
|
+
- Rakefile
|
90
|
+
- lib/wrapit.rb
|
91
|
+
- lib/wrapit/attr_wrappable.rb
|
92
|
+
- lib/wrapit/method_wrappable.rb
|
93
|
+
- lib/wrapit/version.rb
|
94
|
+
- spec/spec_helper.rb
|
95
|
+
- spec/wrapit/attr_wrappable_spec.rb
|
96
|
+
- spec/wrapit/method_wrappable_spec.rb
|
97
|
+
- wrapit.gemspec
|
98
|
+
homepage: https://github.com/jdguzman/wrapit
|
99
|
+
licenses:
|
100
|
+
- MIT
|
101
|
+
post_install_message:
|
102
|
+
rdoc_options: []
|
103
|
+
require_paths:
|
104
|
+
- lib
|
105
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
106
|
+
none: false
|
107
|
+
requirements:
|
108
|
+
- - ! '>='
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
segments:
|
112
|
+
- 0
|
113
|
+
hash: -3391027308563744361
|
114
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
115
|
+
none: false
|
116
|
+
requirements:
|
117
|
+
- - ! '>='
|
118
|
+
- !ruby/object:Gem::Version
|
119
|
+
version: '0'
|
120
|
+
segments:
|
121
|
+
- 0
|
122
|
+
hash: -3391027308563744361
|
123
|
+
requirements: []
|
124
|
+
rubyforge_project:
|
125
|
+
rubygems_version: 1.8.25
|
126
|
+
signing_key:
|
127
|
+
specification_version: 3
|
128
|
+
summary: This gem allows you to define attribtues that need to be wrapped, or wrap
|
129
|
+
already defined attributes.
|
130
|
+
test_files:
|
131
|
+
- spec/spec_helper.rb
|
132
|
+
- spec/wrapit/attr_wrappable_spec.rb
|
133
|
+
- spec/wrapit/method_wrappable_spec.rb
|