wrapit 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - "1.9.3"
4
+ - "2.0.0"
data/README.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Wrapit
2
2
 
3
+ [![Code Climate](https://codeclimate.com/github/jdguzman/wrapit.png)](https://codeclimate.com/github/jdguzman/wrapit)
4
+ [![Build Status](https://travis-ci.org/jdguzman/wrapit.svg?branch=master)](https://travis-ci.org/jdguzman/wrapit)
5
+ [![Coverage Status](https://coveralls.io/repos/jdguzman/wrapit/badge.png?branch=master)](https://coveralls.io/r/jdguzman/wrapit?branch=master)
6
+
3
7
  Wrapit allows you to easily define attributes in a class that should be wrapped,
4
8
  or wrap attributes that have already been defined. The wrapping logic comes from
5
9
  the [wrapped](https://github.com/mike-burns/wrapped) gem.
@@ -20,7 +24,69 @@ Or install it yourself as:
20
24
 
21
25
  ## Usage
22
26
 
23
- TODO: Write usage instructions here
27
+ So lets say we want to create some setters and getters for a class that come all
28
+ wrapped and ready to go.
29
+
30
+ class Foo
31
+ include Wrapit::AttrWrappable
32
+
33
+ attr_wrappable :test_attr
34
+ end
35
+
36
+ foo = Foo.new
37
+
38
+ foo.test_attr = "bar"
39
+ foo.test_attr => #<Present:0x0000010ad6fc80 @value="bar">
40
+ foo.test_attr.unwrap => "bar"
41
+
42
+ foo.test_attr = nil
43
+ foo.test_attr => #<Blank:0x0000010d3064f8>
44
+ foo.test_attr.unwrap => IndexError: Blank has no value
45
+ foo.test_attr.unwrap_or("bar") => "bar"
46
+
47
+ In addition to wrapping up the attribute readers, attr_wrappable creates *_naked* attribute writers:
48
+
49
+ foo.test_attr_naked => nil
50
+ foo.test_attr_naked = "bar"
51
+ foo.test_attr_naked => "bar"
52
+
53
+ # test_attr= is just an alias to test_attr_naked=
54
+ foo.test_attr = "foo"
55
+ foo.test_attr_naked = "foo"
56
+
57
+ Now lets say you have a method you have inherited from a superclass you want to wrap.
58
+
59
+ class Foo
60
+ def test_method
61
+ "bar"
62
+ end
63
+ end
64
+
65
+ class Bar < Foo
66
+ include Wrapit::MethodWrappable
67
+
68
+ method_wrappable :test_method
69
+ end
70
+
71
+ bar = Bar.new
72
+
73
+ bar.test_method => #<Present:0x0000010ad6fc80 @value="bar">
74
+ bar.test_method.unwrap => "bar"
75
+ bar.test_method_naked => "bar"
76
+
77
+ And that's it!
78
+
79
+ ## Limitations
80
+
81
+ At the moment method_wrappable is only usable on methods defined in a superclass and
82
+ that take no arguments.
83
+
84
+ ## TODO
85
+
86
+ 1. Add option to attr_wrappable to skip creating attribute writers.
87
+ 2. Make method_wrappable work with methods that take arguments.
88
+ 3. Look into method_wrappable working better in scenarios where we want to wrap a dynamic
89
+ method. ie. ActiveRecord attributes.
24
90
 
25
91
  ## Contributing
26
92
 
@@ -6,6 +6,10 @@ module Wrapit::MethodWrappable
6
6
  def method_wrappable(*args)
7
7
  class_eval do
8
8
  args.each do |method|
9
+ define_method :"#{method}_naked" do
10
+ self.class.superclass.instance_method(method).bind(self).call
11
+ end
12
+
9
13
  define_method method do
10
14
  super().wrapped
11
15
  end
@@ -1,3 +1,3 @@
1
1
  module Wrapit
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
data/spec/spec_helper.rb CHANGED
@@ -1 +1,5 @@
1
+ require 'coveralls'
2
+
3
+ Coveralls.wear!
4
+
1
5
  require 'wrapit'
@@ -3,22 +3,22 @@ require 'spec_helper'
3
3
  describe Wrapit::AttrWrappable do
4
4
  it "should add attr_wrappable method when included" do
5
5
  build_class
6
- FooBar.private_methods.include?(:attr_wrappable).should be_true
6
+ expect(FooBar.private_methods.include?(:attr_wrappable)).to be true
7
7
  destroy_class
8
8
  end
9
9
 
10
10
  it "should add _naked attribute reader/writers with attr_wrappable" do
11
11
  build_class
12
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
13
+ expect(FooBar.new.respond_to?(:test_method_naked)).to be true
14
+ expect(FooBar.new.respond_to?(:test_method_naked=)).to be true
15
15
  destroy_class
16
16
  end
17
17
 
18
18
  it "should create a wrapped reader with attr_wrappable" do
19
19
  build_class
20
20
  FooBar.module_eval { attr_wrappable :test_method }
21
- FooBar.new.test_method.should be_kind_of Blank
21
+ expect(FooBar.new.test_method).to be_kind_of Blank
22
22
  destroy_class
23
23
  end
24
24
 
@@ -27,7 +27,7 @@ describe Wrapit::AttrWrappable do
27
27
  FooBar.module_eval { attr_wrappable :test_method }
28
28
  foo_bar = FooBar.new
29
29
  foo_bar.test_method = nil
30
- foo_bar.test_method.should be_kind_of Blank
30
+ expect(foo_bar.test_method).to be_kind_of Blank
31
31
  destroy_class
32
32
  end
33
33
 
@@ -36,7 +36,7 @@ describe Wrapit::AttrWrappable do
36
36
  FooBar.module_eval { attr_wrappable :test_method }
37
37
  foo_bar = FooBar.new
38
38
  foo_bar.test_method = "something"
39
- foo_bar.test_method.should be_kind_of Present
39
+ expect(foo_bar.test_method).to be_kind_of Present
40
40
  destroy_class
41
41
  end
42
42
 
@@ -45,7 +45,7 @@ describe Wrapit::AttrWrappable do
45
45
  FooBar.module_eval { attr_wrappable :test_method }
46
46
  foo_bar = FooBar.new
47
47
  foo_bar.test_method = "something"
48
- foo_bar.test_method.unwrap.should eq foo_bar.test_method_naked
48
+ expect(foo_bar.test_method.unwrap).to eq foo_bar.test_method_naked
49
49
  destroy_class
50
50
  end
51
51
 
@@ -3,14 +3,21 @@ require 'spec_helper'
3
3
  describe Wrapit::MethodWrappable do
4
4
  it "should add method_wrappable method when included" do
5
5
  build_class
6
- FooBar.private_methods.include?(:method_wrappable).should be_true
6
+ expect(FooBar.private_methods.include?(:method_wrappable)).to be true
7
+ destroy_class
8
+ end
9
+
10
+ it "should add _naked attribute reader with method_wrappable" do
11
+ build_class
12
+ FooBar.module_eval { method_wrappable :to_s }
13
+ expect(FooBar.new.respond_to?(:to_s_naked)).to be true
7
14
  destroy_class
8
15
  end
9
16
 
10
17
  it "should create a wrapped version of method with method_wrappable" do
11
18
  build_class
12
19
  FooBar.module_eval { method_wrappable :to_s }
13
- FooBar.new.to_s.should be_kind_of Present
20
+ expect(FooBar.new.to_s).to be_kind_of Present
14
21
  destroy_class
15
22
  end
16
23
 
@@ -18,7 +25,7 @@ describe Wrapit::MethodWrappable do
18
25
  build_class
19
26
  Object.module_eval { define_method :test_method do nil; end }
20
27
  FooBar.module_eval { method_wrappable :test_method }
21
- FooBar.new.test_method.should be_kind_of Blank
28
+ expect(FooBar.new.test_method).to be_kind_of Blank
22
29
  destroy_class
23
30
  end
24
31
 
@@ -26,7 +33,16 @@ describe Wrapit::MethodWrappable do
26
33
  build_class
27
34
  Object.module_eval { define_method :test_method do "something"; end }
28
35
  FooBar.module_eval { method_wrappable :test_method }
29
- FooBar.new.test_method.should be_kind_of Present
36
+ expect(FooBar.new.test_method).to be_kind_of Present
37
+ destroy_class
38
+ end
39
+
40
+ it "wrapped method should return same value as naked method" do
41
+ build_class
42
+ Object.module_eval { define_method :test_method do "something"; end }
43
+ FooBar.module_eval { method_wrappable :test_method }
44
+ foo_bar = FooBar.new
45
+ expect(foo_bar.test_method.unwrap).to eq foo_bar.test_method_naked
30
46
  destroy_class
31
47
  end
32
48
 
data/wrapit.gemspec CHANGED
@@ -22,4 +22,5 @@ Gem::Specification.new do |spec|
22
22
  spec.add_development_dependency "bundler", "~> 1.3"
23
23
  spec.add_development_dependency "rake"
24
24
  spec.add_development_dependency "rspec"
25
+ spec.add_development_dependency "coveralls"
25
26
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wrapit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -75,6 +75,22 @@ dependencies:
75
75
  - - ! '>='
76
76
  - !ruby/object:Gem::Version
77
77
  version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: coveralls
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
78
94
  description: Wrap attributes of any class to explicitly avoid nils.
79
95
  email:
80
96
  - jdguzman@mac.com
@@ -83,6 +99,7 @@ extensions: []
83
99
  extra_rdoc_files: []
84
100
  files:
85
101
  - .gitignore
102
+ - .travis.yml
86
103
  - Gemfile
87
104
  - LICENSE.txt
88
105
  - README.md
@@ -110,7 +127,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
110
127
  version: '0'
111
128
  segments:
112
129
  - 0
113
- hash: -3391027308563744361
130
+ hash: -4453862613727087029
114
131
  required_rubygems_version: !ruby/object:Gem::Requirement
115
132
  none: false
116
133
  requirements:
@@ -119,7 +136,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
119
136
  version: '0'
120
137
  segments:
121
138
  - 0
122
- hash: -3391027308563744361
139
+ hash: -4453862613727087029
123
140
  requirements: []
124
141
  rubyforge_project:
125
142
  rubygems_version: 1.8.25