internal_name_helper 0.1.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/LICENSE +21 -0
- data/README.md +30 -0
- data/lib/internal_name_helper.rb +4 -0
- data/lib/internal_name_helper/helper.rb +7 -0
- data/lib/internal_name_helper/methods.rb +50 -0
- data/lib/internal_name_helper/version.rb +3 -0
- metadata +73 -0
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2014 Elan Meng
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
Internal Name Helper for Active Model
|
2
|
+
====================
|
3
|
+
|
4
|
+
Pre-Load all records from database, then generate helper methods.
|
5
|
+
|
6
|
+
For example
|
7
|
+
|
8
|
+
There are 3 plans in the Plan model
|
9
|
+
|
10
|
+
internal_name
|
11
|
+
lite
|
12
|
+
standard
|
13
|
+
pro
|
14
|
+
|
15
|
+
The following methods will be generated
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
Plan.lite
|
19
|
+
Plan.standard
|
20
|
+
Plan.pro
|
21
|
+
```
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
plan.is_lite?
|
25
|
+
plan.is_not_lite?
|
26
|
+
plan.is_standard?
|
27
|
+
plan.is_not_standard?
|
28
|
+
plan.is_pro?
|
29
|
+
plan.is_not_pro?
|
30
|
+
```
|
@@ -0,0 +1,50 @@
|
|
1
|
+
# if the model respond to "internal"
|
2
|
+
# Model#name
|
3
|
+
# Model#is_name?
|
4
|
+
# Model#is_not_name?
|
5
|
+
# will be defined
|
6
|
+
|
7
|
+
module InternalNameHelper
|
8
|
+
module Methods
|
9
|
+
def self.included(base)
|
10
|
+
base.send(:include, InternalNameHelper::Helper)
|
11
|
+
# pre-load all instances
|
12
|
+
instances = base.all
|
13
|
+
|
14
|
+
eigenclass = class << base
|
15
|
+
self
|
16
|
+
end
|
17
|
+
|
18
|
+
eigenclass.class_eval do
|
19
|
+
instances.each do |inst|
|
20
|
+
method_name = inst.dashlized_internal_name
|
21
|
+
|
22
|
+
define_method(method_name) do
|
23
|
+
inst
|
24
|
+
end
|
25
|
+
|
26
|
+
public method_name
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
base.class_eval do
|
31
|
+
instances.each do |inst|
|
32
|
+
method_name = "is_" + inst.dashlized_internal_name + "?"
|
33
|
+
|
34
|
+
define_method(method_name) do
|
35
|
+
self.internal_name == inst.internal_name
|
36
|
+
end
|
37
|
+
|
38
|
+
public method_name
|
39
|
+
|
40
|
+
method_name = "is_not_" + inst.dashlized_internal_name + "?"
|
41
|
+
define_method(method_name) do
|
42
|
+
self.internal_name != inst.internal_name
|
43
|
+
end
|
44
|
+
|
45
|
+
public method_name
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
metadata
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: internal_name_helper
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Elan Meng
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2014-06-09 00:00:00 +08:00
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description: Auto load all instance for the model and generate helper methods.
|
23
|
+
email:
|
24
|
+
- dreamwords@gmail.com
|
25
|
+
executables: []
|
26
|
+
|
27
|
+
extensions: []
|
28
|
+
|
29
|
+
extra_rdoc_files: []
|
30
|
+
|
31
|
+
files:
|
32
|
+
- lib/internal_name_helper.rb
|
33
|
+
- lib/internal_name_helper/helper.rb
|
34
|
+
- lib/internal_name_helper/methods.rb
|
35
|
+
- lib/internal_name_helper/version.rb
|
36
|
+
- LICENSE
|
37
|
+
- README.md
|
38
|
+
has_rdoc: true
|
39
|
+
homepage: https://github.com/dreamwords/internal_name_helper
|
40
|
+
licenses:
|
41
|
+
- MIT
|
42
|
+
post_install_message:
|
43
|
+
rdoc_options: []
|
44
|
+
|
45
|
+
require_paths:
|
46
|
+
- lib
|
47
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
48
|
+
none: false
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
hash: 3
|
53
|
+
segments:
|
54
|
+
- 0
|
55
|
+
version: "0"
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
hash: 3
|
62
|
+
segments:
|
63
|
+
- 0
|
64
|
+
version: "0"
|
65
|
+
requirements: []
|
66
|
+
|
67
|
+
rubyforge_project:
|
68
|
+
rubygems_version: 1.4.2
|
69
|
+
signing_key:
|
70
|
+
specification_version: 3
|
71
|
+
summary: Auto load all instance for the model and generate helper methods
|
72
|
+
test_files: []
|
73
|
+
|