expandable 0.0.1 → 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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 87e9485895ba6a82a0899a9f5a44dcd1e5a33a5c
4
- data.tar.gz: 827595dee0cc4c2c28fcd82bcecf3fd54a45a28e
3
+ metadata.gz: 6e6fd53b6eb1b89afec9909f47c5d296dcdc69dc
4
+ data.tar.gz: aa8354b5f8886d1f99f7af0434200823d7901bab
5
5
  SHA512:
6
- metadata.gz: ead7c1ab2fd059ba9ae71188ef933f6486a706041e1e07b223a755bb51eaddaeaaf450bfaf51f27f9fb8fc7786853a47d50283ca4106b6a729cee8bb8f2ff3b3
7
- data.tar.gz: 9c33e5fdf8ddd747d7e609982f2284b9a224fbd705adbd9a7a247da7fe8ee027abd982ee020ac75ab200673cbf2df9743201dc07d463e493de55fc98c6c5eb1b
6
+ metadata.gz: 54008a342d9bce8abed407b02a309a4f184dc748e701b62dbc8269df215fb2a71f201f213fb9c02fc4fbfce2658d74d8a3475a380750e6e087c8e9e53ea56af5
7
+ data.tar.gz: f0669754613e25e132f6632d9ccee1ce2ac13654245b9177029001e31036ec026d6e1192de8c59b2e6bae6d7bfabafae4879edbd1edd841f5c877bbc67fa5056
data/README.md CHANGED
@@ -1,8 +1,11 @@
1
1
  # Expandable
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/expandable`. To experiment with that code, run `bin/console` for an interactive prompt.
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
- TODO: Delete this and the text above, and describe your gem
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
- Or install it yourself as:
22
+ ## Setup
20
23
 
21
- $ gem install expandable
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
- TODO: Write usage instructions here
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
- ## Development
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
- After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
65
+ The same principle applys to controllers as well.
30
66
 
31
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
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
- ## Contributing
91
+ Simple right? Now start organizing your codes and make your application shine.
34
92
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/expandable.
93
+ ## Limitations
36
94
 
95
+ The gem has few limitations such as:
37
96
 
38
- ## License
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
- The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
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
 
@@ -1,3 +1,7 @@
1
1
  unless File.exist?('app/models/expandables')
2
2
  Dir.mkdir 'app/models/expandables'
3
3
  end
4
+
5
+ unless File.exist?('app/controllers/expandables')
6
+ Dir.mkdir 'app/controllers/expandables'
7
+ end
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 more business logic.}
13
- spec.description = %q{Expandables allows you to create instance methods and and class methods and organize them in folders.}
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 = 'GNU'
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
- start_post_load
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
@@ -3,5 +3,9 @@ module Expandable
3
3
  config.paths.add Rails.root.join('app', 'models', 'expandables').to_s,
4
4
  eager_load: true,
5
5
  glob: '*'
6
+
7
+ config.paths.add Rails.root.join('app', 'controllers', 'expandables').to_s,
8
+ eager_load: true,
9
+ glob: '*'
6
10
  end
7
11
  end
@@ -1,30 +1,76 @@
1
1
  module LoadModules
2
- def start_post_load
3
- return unless File.exist?('app/models/expandables')
4
- expandables = Dir['app/models/expandables/**/*.rb']
5
-
6
- unless expandables.empty?
7
- expandables.each do |modu|
8
- modu = File.basename(modu).split('.')[0..-2].join
9
- parts = modu.split('_')
10
-
11
- if parts.length > 1
12
- target_class = parts[-1].capitalize.classify.safe_constantize
13
- expand_module = modu.camelize.classify.safe_constantize
14
- load_expandables(target_class, expand_module, parts[0].downcase == 'self')
15
- else
16
- next
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
@@ -1,3 +1,3 @@
1
1
  module Expandable
2
- VERSION = '0.0.1'
2
+ VERSION = '0.1.0'
3
3
  end
@@ -7,15 +7,20 @@ module Expandable
7
7
  source_root File.expand_path('..', __FILE__)
8
8
 
9
9
  def create_structures
10
- base_path = 'app/models/expandables'
11
- if File.exists?(base_path)
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.1
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-24 00:00:00.000000000 Z
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: Expandables allows you to create instance methods and and class methods
42
- and organize them in folders.
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
- - GNU
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 more business logic.
89
+ summary: Expands models and controllers to allow you to organize you codes.
89
90
  test_files: []