conscious_concern 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/lib/conscious_concern/eager_loader.rb +34 -0
- data/lib/conscious_concern.rb +89 -0
- metadata +61 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 09274c5c2bfd29266342c8d7fe175475dd33b9bd
|
4
|
+
data.tar.gz: c800ab2890c4c61a322e9ddfdb19dcf03901b84b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d36abdb2c800622d66163a3906c764a0233864f9f0d463c562bf77fef7f9141e64b2794b9fa8dbedc7c5a1bab49b37ffeca6d81437be642c7b6c8ad8149dbeed
|
7
|
+
data.tar.gz: 4c8c86e86cfcbf711858181f9c09e436ac04a82d96a367c278010920e59d929f3c4553ae7621092fc7b2428bc5a31941b532fb7f6c0012e31ad814fb7821405a
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module ConsciousConcern
|
2
|
+
#
|
3
|
+
# Recursively finds Ruby Classes in a given directory.
|
4
|
+
#
|
5
|
+
class EagerLoader
|
6
|
+
DEBUG = false
|
7
|
+
|
8
|
+
def self.load_classes_in_rails_dir(name)
|
9
|
+
rails = Object.const_defined?('Rails') ? 'Rails'.constantize : nil
|
10
|
+
unless rails && rails.root
|
11
|
+
puts 'no initialized Rails application' if DEBUG
|
12
|
+
return false
|
13
|
+
end
|
14
|
+
load_classes_in(rails.root.join('app', name))
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.load_classes_in(dir)
|
18
|
+
Dir.foreach(dir) do |entry|
|
19
|
+
if entry.end_with?('.rb')
|
20
|
+
class_from_filename(entry)
|
21
|
+
elsif File.directory?(dir.join(entry)) && entry[0] != '.'
|
22
|
+
load_classes_in(dir.join(entry))
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.class_from_filename(filename)
|
28
|
+
clazz = filename[0..-4].classify.constantize
|
29
|
+
puts "eager loaded class '#{clazz}'" if DEBUG
|
30
|
+
rescue => e
|
31
|
+
puts e.message if DEBUG
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,89 @@
|
|
1
|
+
#
|
2
|
+
# Decorates ActiveSupport::Concern
|
3
|
+
#
|
4
|
+
module ConsciousConcern
|
5
|
+
require_relative File.join('conscious_concern', 'eager_loader')
|
6
|
+
require 'active_support/concern'
|
7
|
+
include ActiveSupport::Concern
|
8
|
+
|
9
|
+
def self.load_classes(*custom_dirs)
|
10
|
+
ConsciousConcern::EagerLoader.load_classes_in_rails_dir('controllers')
|
11
|
+
ConsciousConcern::EagerLoader.load_classes_in_rails_dir('models')
|
12
|
+
custom_dirs.each { |dir| EagerLoader.load_classes_in_dir(dir) }
|
13
|
+
extenders.each { |concern| concern._classes_loaded_callbacks.each(&:call) }
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.extenders
|
17
|
+
@extenders ||= []
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.extended(base)
|
21
|
+
extenders << base
|
22
|
+
ActiveSupport::Concern.extended(base)
|
23
|
+
end
|
24
|
+
|
25
|
+
def included(base = nil, &block)
|
26
|
+
_classes << base if base && base.is_a?(Class)
|
27
|
+
super
|
28
|
+
end
|
29
|
+
|
30
|
+
def prepended(base = nil)
|
31
|
+
_classes << base if base && base.is_a?(Class)
|
32
|
+
super
|
33
|
+
end
|
34
|
+
|
35
|
+
# underscore aliases protect core functionality against overrides in extenders
|
36
|
+
|
37
|
+
def classes
|
38
|
+
@_classes ||= []
|
39
|
+
end
|
40
|
+
alias_method :_classes, :classes
|
41
|
+
|
42
|
+
def controllers
|
43
|
+
return _classes unless _model_concern?
|
44
|
+
_classes.map { |m| _try_constant("#{m.to_s.pluralize}Controller") }.compact
|
45
|
+
end
|
46
|
+
alias_method :_controllers, :controllers
|
47
|
+
|
48
|
+
def models
|
49
|
+
return _classes if _model_concern?
|
50
|
+
_classes.map { |ctr| _try_constant(ctr.controller_name) }.compact
|
51
|
+
end
|
52
|
+
alias_method :_models, :models
|
53
|
+
|
54
|
+
def resources(options = { only: [] }, &resource_routing_block)
|
55
|
+
resource_args = _tables
|
56
|
+
app_routes_block = resource_routing_block.binding.eval('self')
|
57
|
+
app_routes_block.instance_eval do
|
58
|
+
resources(*resource_args, options, &resource_routing_block)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def tables
|
63
|
+
_models.map { |model| model.try(:table_name) }.compact
|
64
|
+
end
|
65
|
+
alias_method :_tables, :tables
|
66
|
+
|
67
|
+
def let_controllers(method_args_hash)
|
68
|
+
_classes_loaded_callbacks << proc do
|
69
|
+
method_args_hash.each do |method, args|
|
70
|
+
_controllers.each { |ctr| ctr.send(method, *args) }
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def classes_loaded_callbacks
|
76
|
+
@_classes_loaded_callbacks ||= []
|
77
|
+
end
|
78
|
+
alias_method :_classes_loaded_callbacks, :classes_loaded_callbacks
|
79
|
+
|
80
|
+
private
|
81
|
+
|
82
|
+
def _model_concern?
|
83
|
+
_classes.any? && !_classes.first.to_s.end_with?('Controller')
|
84
|
+
end
|
85
|
+
|
86
|
+
def _try_constant(name)
|
87
|
+
Object.const_defined?(name.classify) ? name.classify.constantize : nil
|
88
|
+
end
|
89
|
+
end
|
metadata
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: conscious_concern
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Janosch Müller
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-11-06 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4'
|
27
|
+
description: ConsciousConcern is a decorator for ActiveSupport::Concernthat adds several
|
28
|
+
metaprogramming features.
|
29
|
+
email:
|
30
|
+
- janosch84@gmail.com
|
31
|
+
executables: []
|
32
|
+
extensions: []
|
33
|
+
extra_rdoc_files: []
|
34
|
+
files:
|
35
|
+
- lib/conscious_concern.rb
|
36
|
+
- lib/conscious_concern/eager_loader.rb
|
37
|
+
homepage: https://github.com/janosch-x/conscious_concern
|
38
|
+
licenses:
|
39
|
+
- MIT
|
40
|
+
metadata: {}
|
41
|
+
post_install_message:
|
42
|
+
rdoc_options: []
|
43
|
+
require_paths:
|
44
|
+
- lib
|
45
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '0'
|
50
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
requirements: []
|
56
|
+
rubyforge_project:
|
57
|
+
rubygems_version: 2.4.5
|
58
|
+
signing_key:
|
59
|
+
specification_version: 4
|
60
|
+
summary: Enhances ActiveSupport::Concern
|
61
|
+
test_files: []
|