easily_typable 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE.txt +20 -0
- data/README.md +103 -0
- data/lib/easily_typable.rb +45 -0
- metadata +168 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 0221935b88c5cbac4176a0085bd516a6aa2bbec2
|
4
|
+
data.tar.gz: e2a24f81c4e66ddc96bb68a6b2f86dd2532c44f8
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 779755f35a05255d541f1e8d8feddad158922967d830f5cd1867d274f4cc96dbebc734dbb7ec083cc644497f889a50569f92bc728acdefed858956d913ae4641
|
7
|
+
data.tar.gz: c4de309f4db5af0d07f230fda47a3e68001fd45aa3a5f81b572bcc92321d616e57c6b12bb1b0dcd4de98f22447b38a360c28a3de8c73a80503c5a1fe20638b66
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2017 AndyObtiva
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
# Easily Typable v1.0.0
|
2
|
+
[![Gem Version](https://badge.fury.io/rb/easily_typable.svg)](http://badge.fury.io/rb/easily_typable)
|
3
|
+
[![Build Status](https://api.travis-ci.org/AndyObtiva/easily_typable.svg?branch=master)](https://travis-ci.org/AndyObtiva/easily_typable)
|
4
|
+
[![Coverage Status](https://coveralls.io/repos/AndyObtiva/easily_typable/badge.svg?branch=master)](https://coveralls.io/r/AndyObtiva/easily_typable?branch=master)
|
5
|
+
[![Code Climate](https://codeclimate.com/github/AndyObtiva/easily_typable.svg)](https://codeclimate.com/github/AndyObtiva/easily_typable)
|
6
|
+
|
7
|
+
## Introduction:
|
8
|
+
|
9
|
+
Although polymorphism is a recommended standard in Object-Oriented programming
|
10
|
+
for invoking varied behavior in an inheritance hierarchy, sometimes it is still
|
11
|
+
useful to verify if a particular model belongs to a certain type when the
|
12
|
+
behavior concerned does not belong to the model and is too small to require a
|
13
|
+
Design Pattern like Strategy.
|
14
|
+
|
15
|
+
A common example in Rails is checking user roles before rendering certain
|
16
|
+
parts of the view:
|
17
|
+
|
18
|
+
```erb
|
19
|
+
<% if user.is_a?(Admin) %>
|
20
|
+
<%= link_to 'Admin', admin_dashboard_path %>
|
21
|
+
<% end %>
|
22
|
+
```
|
23
|
+
|
24
|
+
To avoid the ```model.is_a?(CertainType)``` syntax, a more readable approach
|
25
|
+
that developers resort to is to add an English-like method that hides the
|
26
|
+
details of type checking ```model.certain_type?```.
|
27
|
+
|
28
|
+
The Rails example above would then become:
|
29
|
+
|
30
|
+
```erb
|
31
|
+
<% if user.admin? %>
|
32
|
+
<%= link_to 'Admin', admin_dashboard_path %>
|
33
|
+
<% end %>
|
34
|
+
```
|
35
|
+
|
36
|
+
Implementing such methods manually gets repetitive after a while, so an easier
|
37
|
+
way to get these methods automatically is to mixin the ```EasilyTypable```
|
38
|
+
module.
|
39
|
+
|
40
|
+
## Example:
|
41
|
+
|
42
|
+
```ruby
|
43
|
+
require 'easily_typable'
|
44
|
+
|
45
|
+
class TypeA
|
46
|
+
include EasilyTypable
|
47
|
+
end
|
48
|
+
|
49
|
+
class TypeB < TypeA
|
50
|
+
end
|
51
|
+
|
52
|
+
class TypeC < TypeB
|
53
|
+
end
|
54
|
+
|
55
|
+
## RSpec of Easily Typable
|
56
|
+
require 'spec_helper'
|
57
|
+
|
58
|
+
describe EasilyTypable do
|
59
|
+
it "adds type_a? method to TypeA object" do
|
60
|
+
expect(TypeA.new.type_a?).to be_truthy
|
61
|
+
end
|
62
|
+
it "adds type_b? method to TypeA object" do
|
63
|
+
expect(TypeA.new.type_b?).to be_falsey
|
64
|
+
end
|
65
|
+
it "adds type_c? method to TypeA object" do
|
66
|
+
expect(TypeA.new.type_c?).to be_falsey
|
67
|
+
end
|
68
|
+
it "adds type_a? method to TypeB object" do
|
69
|
+
expect(TypeB.new.type_a?).to be_truthy
|
70
|
+
end
|
71
|
+
it "adds type_b? method to TypeB object" do
|
72
|
+
expect(TypeB.new.type_b?).to be_truthy
|
73
|
+
end
|
74
|
+
it "adds type_c? method to TypeB object" do
|
75
|
+
expect(TypeB.new.type_c?).to be_falsey
|
76
|
+
end
|
77
|
+
it "adds type_a? method to TypeC object" do
|
78
|
+
expect(TypeC.new.type_a?).to be_truthy
|
79
|
+
end
|
80
|
+
it "adds type_b? method to TypeC object" do
|
81
|
+
expect(TypeC.new.type_b?).to be_truthy
|
82
|
+
end
|
83
|
+
it "adds type_c? method to TypeC object" do
|
84
|
+
expect(TypeC.new.type_c?).to be_truthy
|
85
|
+
end
|
86
|
+
end
|
87
|
+
```
|
88
|
+
|
89
|
+
## Contributing to Easily Typable
|
90
|
+
|
91
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
|
92
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
|
93
|
+
* Fork the project.
|
94
|
+
* Start a feature/bugfix branch.
|
95
|
+
* Commit and push until you are happy with your contribution.
|
96
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
97
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
98
|
+
|
99
|
+
## Copyright
|
100
|
+
|
101
|
+
* Copyright (c) 2012 - 2017 Andy Maleh
|
102
|
+
* Copyright (c) 2009 - 2011 Andy Maleh - Obtiva Corp.
|
103
|
+
* See LICENSE.txt for further details.
|
@@ -0,0 +1,45 @@
|
|
1
|
+
module EasilyTypable
|
2
|
+
|
3
|
+
def self.included(class_constant)
|
4
|
+
super
|
5
|
+
class_constant.class_eval <<-end_eval
|
6
|
+
def #{__methodize__(class_constant.name)}?
|
7
|
+
self.is_a?(#{class_constant.name})
|
8
|
+
end
|
9
|
+
def self.inherited(subclass_constant)
|
10
|
+
super
|
11
|
+
subclass_constant.send :include, SubClassMethods
|
12
|
+
end
|
13
|
+
end_eval
|
14
|
+
end
|
15
|
+
|
16
|
+
module SubClassMethods
|
17
|
+
def self.included(subclass_constant)
|
18
|
+
super
|
19
|
+
subclass_constant.class_eval(type_method_definition(subclass_constant))
|
20
|
+
superclass_constant = subclass_constant.superclass
|
21
|
+
begin
|
22
|
+
superclass_constant.class_eval(type_method_definition(subclass_constant))
|
23
|
+
superclass_constant = superclass_constant.superclass
|
24
|
+
end while superclass_constant.include?(EasilyTypable)
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
def self.type_method_definition(subclass_constant)
|
29
|
+
<<-MULTI
|
30
|
+
def #{EasilyTypable::__methodize__(subclass_constant.name)}?
|
31
|
+
self.is_a?(#{subclass_constant.name})
|
32
|
+
end
|
33
|
+
MULTI
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
protected
|
38
|
+
#added this method to break reliance on ActiveSupport and Facets
|
39
|
+
def self.__methodize__(text)
|
40
|
+
text.gsub(/([A-Z]+)([A-Z])/,'\1_\2').
|
41
|
+
gsub(/([a-z])([A-Z])/,'\1_\2').
|
42
|
+
downcase
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
metadata
ADDED
@@ -0,0 +1,168 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: easily_typable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- AndyObtiva
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-02-07 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: jeweler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 2.0.1
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 2.0.1
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rdoc
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 5.0.0
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 5.0.0
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 3.5.0
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 3.5.0
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 10.4.2
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 10.4.2
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rack
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 1.6.5
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 1.6.5
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: nokogiri
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 1.6.8.1
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 1.6.8.1
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: tins
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: 1.6.0
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: 1.6.0
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: term-ansicolor
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - "~>"
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: 1.3.2
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - "~>"
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: 1.3.2
|
125
|
+
description: Although polymorphism is a recommended standard in Object-Oriented programming
|
126
|
+
for invoking varied behavior in an inheritance hierarchy, sometimes it is still
|
127
|
+
useful to verify if a particular model belongs to a certain type when the behavior
|
128
|
+
concerned does not belong to the model and is too small to require a Design Pattern
|
129
|
+
like Strategy. To avoid the model.is_a?(CertainType) syntax, a more readable approach
|
130
|
+
that developers resort to is to add an English-like method that hides the details
|
131
|
+
of type checking model.certain_type?. Implementing such methods manually gets repetitive
|
132
|
+
after a while, so an easier way to get these methods automatically is to mixin the
|
133
|
+
EasilyTypable module.
|
134
|
+
email: andy.am@gmail.com
|
135
|
+
executables: []
|
136
|
+
extensions: []
|
137
|
+
extra_rdoc_files:
|
138
|
+
- LICENSE.txt
|
139
|
+
- README.md
|
140
|
+
files:
|
141
|
+
- LICENSE.txt
|
142
|
+
- README.md
|
143
|
+
- lib/easily_typable.rb
|
144
|
+
homepage: http://github.com/AndyObtiva/easily_typable
|
145
|
+
licenses:
|
146
|
+
- MIT
|
147
|
+
metadata: {}
|
148
|
+
post_install_message:
|
149
|
+
rdoc_options: []
|
150
|
+
require_paths:
|
151
|
+
- lib
|
152
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
153
|
+
requirements:
|
154
|
+
- - ">="
|
155
|
+
- !ruby/object:Gem::Version
|
156
|
+
version: '0'
|
157
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
158
|
+
requirements:
|
159
|
+
- - ">="
|
160
|
+
- !ruby/object:Gem::Version
|
161
|
+
version: '0'
|
162
|
+
requirements: []
|
163
|
+
rubyforge_project:
|
164
|
+
rubygems_version: 2.4.8
|
165
|
+
signing_key:
|
166
|
+
specification_version: 4
|
167
|
+
summary: Easier type checking via auto-generated certain_type? methods
|
168
|
+
test_files: []
|