expandable 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +78 -12
- data/config/initializers/expandable.rb +4 -0
- data/expandable.gemspec +5 -3
- data/lib/expandable.rb +12 -1
- data/lib/expandable/application.rb +4 -0
- data/lib/expandable/load_modules.rb +63 -17
- data/lib/expandable/version.rb +1 -1
- data/lib/generators/expandable/init_generator.rb +13 -8
- metadata +7 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6e6fd53b6eb1b89afec9909f47c5d296dcdc69dc
|
4
|
+
data.tar.gz: aa8354b5f8886d1f99f7af0434200823d7901bab
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 54008a342d9bce8abed407b02a309a4f184dc748e701b62dbc8269df215fb2a71f201f213fb9c02fc4fbfce2658d74d8a3475a380750e6e087c8e9e53ea56af5
|
7
|
+
data.tar.gz: f0669754613e25e132f6632d9ccee1ce2ac13654245b9177029001e31036ec026d6e1192de8c59b2e6bae6d7bfabafae4879edbd1edd841f5c877bbc67fa5056
|
data/README.md
CHANGED
@@ -1,8 +1,11 @@
|
|
1
1
|
# Expandable
|
2
2
|
|
3
|
-
|
3
|
+
Expandable is a Rails gem that allows you to take that big ugly model codes and
|
4
|
+
break it up into many small chunks of organized codes in different files.
|
4
5
|
|
5
|
-
|
6
|
+
It also helps you to break large business logic in controllers in to little
|
7
|
+
pirces. Usually you want to have the business logic out of the controller and
|
8
|
+
in to the model however there are rare times when you just simply can not.
|
6
9
|
|
7
10
|
## Installation
|
8
11
|
|
@@ -16,26 +19,89 @@ And then execute:
|
|
16
19
|
|
17
20
|
$ bundle
|
18
21
|
|
19
|
-
|
22
|
+
## Setup
|
20
23
|
|
21
|
-
|
24
|
+
The first step is to make sure that this folder exist `app/models/expandables`.
|
25
|
+
You can either create the file yourself of run the generator:
|
26
|
+
|
27
|
+
```bash
|
28
|
+
rails generate expandable:init
|
29
|
+
```
|
22
30
|
|
23
31
|
## Usage
|
24
32
|
|
25
|
-
|
33
|
+
This gem is very easy to use in just three steps.
|
34
|
+
|
35
|
+
##### First
|
36
|
+
Create a folder in `app/models/expandables` where the name of the
|
37
|
+
folder is the model name. For an example the book model
|
38
|
+
will be `app/models/expandables/book`.
|
26
39
|
|
27
|
-
|
40
|
+
##### Second
|
41
|
+
Create a file with any name and make sure that then end of the file
|
42
|
+
name ends with the underscore model name and the extension. For an example
|
43
|
+
the book model will have an expandable in
|
44
|
+
`app/models/expandables/book/foo_book.rb`.
|
45
|
+
|
46
|
+
##### Third
|
47
|
+
Make sure that the file is a module type. For example the foo_book.rb
|
48
|
+
will the the structure:
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
module FooBook
|
52
|
+
def foo
|
53
|
+
'bar'
|
54
|
+
end
|
55
|
+
end
|
56
|
+
```
|
57
|
+
|
58
|
+
Now all you have to do is just use it. Here is an example:
|
59
|
+
|
60
|
+
```ruby
|
61
|
+
Book.new.foo
|
62
|
+
'bar'
|
63
|
+
```
|
28
64
|
|
29
|
-
|
65
|
+
The same principle applys to controllers as well.
|
30
66
|
|
31
|
-
|
67
|
+
### Class Methods
|
68
|
+
|
69
|
+
If you like to make an expandable in to a class method then all you have to do
|
70
|
+
is chnge the file name and module name by adding a self_ infront of it. For
|
71
|
+
an example for book model it will be
|
72
|
+
`app/models/expandables/book/self_foo_book.rb`.
|
73
|
+
|
74
|
+
The codes will look like this:
|
75
|
+
|
76
|
+
```ruby
|
77
|
+
module SelfFooBook
|
78
|
+
def bar
|
79
|
+
'foo'
|
80
|
+
end
|
81
|
+
end
|
82
|
+
```
|
83
|
+
|
84
|
+
Now all you have to do is:
|
85
|
+
|
86
|
+
```ruby
|
87
|
+
Book.bar
|
88
|
+
'foo'
|
89
|
+
```
|
32
90
|
|
33
|
-
|
91
|
+
Simple right? Now start organizing your codes and make your application shine.
|
34
92
|
|
35
|
-
|
93
|
+
## Limitations
|
36
94
|
|
95
|
+
The gem has few limitations such as:
|
37
96
|
|
38
|
-
|
97
|
+
##### Only one model can use its counter parts.
|
98
|
+
You can not have another model load book's modules.
|
99
|
+
This is done purposely to stop the clutter.
|
100
|
+
If you have a module that needs to be loaded in two or more models then use
|
101
|
+
"concerns" and include the modules in each models that needs it.
|
39
102
|
|
40
|
-
|
103
|
+
##### Restart Or Reload Needed On Change
|
104
|
+
Dispite my best effort I was unable to have the module auto reload on change.
|
105
|
+
This means every time you make a change to the modules it will not load unless
|
106
|
+
you close and start the application again.
|
41
107
|
|
data/expandable.gemspec
CHANGED
@@ -9,10 +9,12 @@ Gem::Specification.new do |spec|
|
|
9
9
|
spec.authors = ['Saimon Lovell']
|
10
10
|
spec.email = ['staysynchronize@gmail.com']
|
11
11
|
|
12
|
-
spec.summary = %q{Expands models to allow
|
13
|
-
|
12
|
+
spec.summary = %q{Expands models and controllers to allow you to
|
13
|
+
organize you codes.}
|
14
|
+
spec.description = %q{Expandables allows you to create instance methods and
|
15
|
+
and class methods and organize them in folders.}
|
14
16
|
spec.homepage = 'https://github.com/SaimonL/expandable'
|
15
|
-
spec.license = '
|
17
|
+
spec.license = 'GNU3'
|
16
18
|
|
17
19
|
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
18
20
|
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
data/lib/expandable.rb
CHANGED
@@ -7,7 +7,18 @@ module Expandable
|
|
7
7
|
include LoadModules
|
8
8
|
|
9
9
|
config.after_initialize do
|
10
|
-
|
10
|
+
if Rails.env.development?
|
11
|
+
puts ''
|
12
|
+
puts 'Expandable loading ...'
|
13
|
+
end
|
14
|
+
|
15
|
+
start_model_load
|
16
|
+
start_controller_load
|
17
|
+
if Rails.env.development?
|
18
|
+
puts ''
|
19
|
+
puts 'Expandable has loaded'
|
20
|
+
puts ''
|
21
|
+
end
|
11
22
|
end
|
12
23
|
|
13
24
|
end
|
@@ -1,30 +1,76 @@
|
|
1
1
|
module LoadModules
|
2
|
-
def
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
2
|
+
def start_model_load
|
3
|
+
load_modules('app/models/expandables')
|
4
|
+
end
|
5
|
+
|
6
|
+
def start_controller_load
|
7
|
+
load_modules('app/controllers/expandables', false)
|
8
|
+
end
|
9
|
+
|
10
|
+
private
|
11
|
+
def load_modules(path, is_model = true)
|
12
|
+
return unless File.exist?(path)
|
13
|
+
expandables = Dir["#{path}/**/*.rb"]
|
14
|
+
|
15
|
+
unless expandables.empty?
|
16
|
+
expandables.each do |modu|
|
17
|
+
if Rails.env.development?
|
18
|
+
puts ''
|
19
|
+
puts ' Found expandable file: ' + modu
|
20
|
+
end
|
21
|
+
|
22
|
+
modu = File.basename(modu).split('.')[0..-2].join
|
23
|
+
parts = modu.split('_')
|
24
|
+
|
25
|
+
if parts.length > 1
|
26
|
+
if is_model
|
27
|
+
target_class = parts[-1]
|
28
|
+
.singularize
|
29
|
+
.capitalize
|
30
|
+
.classify
|
31
|
+
.safe_constantize
|
32
|
+
|
33
|
+
if Rails.env.development?
|
34
|
+
puts ' Target Model: ' + target_class.to_s
|
35
|
+
puts ' Target Object Type: ' + target_class.class.name
|
36
|
+
end
|
37
|
+
else
|
38
|
+
target_class = [parts[-2].pluralize, parts[-1]].join('_')
|
39
|
+
.capitalize
|
40
|
+
.classify
|
41
|
+
.safe_constantize
|
42
|
+
|
43
|
+
if Rails.env.development?
|
44
|
+
puts ' Target Controller: ' + target_class.to_s
|
45
|
+
puts ' Target Object Type: ' + target_class.class.name
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
expand_module = modu.camelize.classify.safe_constantize
|
50
|
+
load_expandables(target_class, expand_module, parts[0].downcase == 'self')
|
51
|
+
else
|
52
|
+
if Rails.env.development?
|
53
|
+
puts ' The file was not named properly: ' + modu
|
54
|
+
end
|
55
|
+
next
|
56
|
+
end
|
17
57
|
end
|
18
58
|
end
|
19
59
|
end
|
20
|
-
end
|
21
60
|
|
22
|
-
private
|
23
61
|
def load_expandables(target_class, target_module, is_extend)
|
24
62
|
if (target_class.class == Class) && (target_module.class == Module)
|
63
|
+
ActiveSupport::Dependencies.explicitly_unloadable_constants << target_module
|
64
|
+
|
25
65
|
if is_extend
|
66
|
+
if Rails.env.development?
|
67
|
+
puts ' Loading class module: ' + target_module.to_s
|
68
|
+
end
|
26
69
|
target_class.send :extend, target_module
|
27
70
|
else
|
71
|
+
if Rails.env.development?
|
72
|
+
puts ' Loading instance module: ' + target_module.to_s
|
73
|
+
end
|
28
74
|
target_class.send :include, target_module
|
29
75
|
end
|
30
76
|
end
|
data/lib/expandable/version.rb
CHANGED
@@ -7,15 +7,20 @@ module Expandable
|
|
7
7
|
source_root File.expand_path('..', __FILE__)
|
8
8
|
|
9
9
|
def create_structures
|
10
|
-
|
11
|
-
|
12
|
-
puts 'Directory "' + base_path + '" already exist.'
|
13
|
-
else
|
14
|
-
Dir.mkdir(base_path)
|
15
|
-
FileUtils.touch [base_path, '.keep'].join('/')
|
16
|
-
puts 'Directory created: ' + base_path
|
17
|
-
end
|
10
|
+
create_path('app/models/expandables')
|
11
|
+
create_path('app/controllers/expandables')
|
18
12
|
end
|
13
|
+
|
14
|
+
private
|
15
|
+
def create_path(base_path)
|
16
|
+
if File.exists?(base_path)
|
17
|
+
puts 'Directory "' + base_path + '" already exist.'
|
18
|
+
else
|
19
|
+
Dir.mkdir(base_path)
|
20
|
+
FileUtils.touch [base_path, '.keep'].join('/')
|
21
|
+
puts 'Directory created: ' + base_path
|
22
|
+
end
|
23
|
+
end
|
19
24
|
end
|
20
25
|
end
|
21
26
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: expandable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Saimon Lovell
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-08-
|
11
|
+
date: 2016-08-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -38,8 +38,9 @@ dependencies:
|
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
|
-
description:
|
42
|
-
|
41
|
+
description: |-
|
42
|
+
Expandables allows you to create instance methods and
|
43
|
+
and class methods and organize them in folders.
|
43
44
|
email:
|
44
45
|
- staysynchronize@gmail.com
|
45
46
|
executables: []
|
@@ -63,7 +64,7 @@ files:
|
|
63
64
|
- lib/generators/expandable/init_generator.rb
|
64
65
|
homepage: https://github.com/SaimonL/expandable
|
65
66
|
licenses:
|
66
|
-
-
|
67
|
+
- GNU3
|
67
68
|
metadata:
|
68
69
|
allowed_push_host: https://rubygems.org
|
69
70
|
post_install_message:
|
@@ -85,5 +86,5 @@ rubyforge_project:
|
|
85
86
|
rubygems_version: 2.6.6
|
86
87
|
signing_key:
|
87
88
|
specification_version: 4
|
88
|
-
summary: Expands models to allow
|
89
|
+
summary: Expands models and controllers to allow you to organize you codes.
|
89
90
|
test_files: []
|