just_for_fun 0.1.0 → 0.2.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 +5 -3
- data/lib/just_for_fun.rb +0 -22
- data/lib/just_for_fun/railtie.rb +33 -0
- data/lib/just_for_fun/version.rb +1 -1
- metadata +15 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9d25e21c40f2e168dd9f006c418758e44d48c242020d525e8d4f92459fbfe02c
|
4
|
+
data.tar.gz: 1aad777706059803f16678ab460801f204e8c3262e6080628f840bcdbd2a24d9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5f1b36e4d5bb18f6472a4a4b8efd4bc4511925645a8ffe78caf34360901f5ea825c2d5ee076d70e510b397047893922c6dd6ed5b48c6242c0d303a6a8f05ce9c
|
7
|
+
data.tar.gz: 0d21c229ae765b1a03d9c0529d5cbf2bc0e2bc5217b22643b5898b3126ed543d0d54ae4cf8bedb71d6677bda2abd0f719517ae029ce88619852a25174aef1e6e
|
data/README.md
CHANGED
@@ -1,5 +1,3 @@
|
|
1
|
-
# 42.to_user
|
2
|
-
|
3
1
|
This gem is adding shortcuts for Integer and Array classes so you can call `to_XXXXX` method and get object or relation object.
|
4
2
|
|
5
3
|
## Usage
|
@@ -31,7 +29,11 @@ $ gem install just_for_fun
|
|
31
29
|
|
32
30
|
## Implementation
|
33
31
|
|
34
|
-
See `lib/
|
32
|
+
See `lib/tasks/railtie.rb`. When app is starting gem is defining such methods.
|
33
|
+
|
34
|
+
## TODO
|
35
|
+
|
36
|
+
- improve performance, looks like it has impact on it
|
35
37
|
|
36
38
|
## Contributing
|
37
39
|
|
data/lib/just_for_fun.rb
CHANGED
@@ -1,26 +1,4 @@
|
|
1
1
|
require "just_for_fun/railtie"
|
2
2
|
|
3
|
-
class Integer
|
4
|
-
def method_missing(method, *args, &block)
|
5
|
-
klass_name, exclamation = method.to_s.scan(/to_(\w+)(\!?)/).flatten
|
6
|
-
if klass_name
|
7
|
-
klass = klass_name.classify.safe_constantize
|
8
|
-
return klass.find(self) if klass
|
9
|
-
end
|
10
|
-
super(method, *args, &block)
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
class Array
|
15
|
-
def method_missing(method, *args, &block)
|
16
|
-
klass_name, exclamation = method.to_s.scan(/to_(\w+)(\!?)/).flatten
|
17
|
-
if klass_name
|
18
|
-
klass = klass_name.classify.safe_constantize
|
19
|
-
return klass.where(klass.primary_key => self) if klass
|
20
|
-
end
|
21
|
-
super(method, *args, &block)
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
3
|
module JustForFun
|
26
4
|
end
|
data/lib/just_for_fun/railtie.rb
CHANGED
@@ -1,4 +1,37 @@
|
|
1
|
+
require 'pry'
|
2
|
+
|
1
3
|
module JustForFun
|
2
4
|
class Railtie < ::Rails::Railtie
|
5
|
+
|
6
|
+
config.after_initialize do
|
7
|
+
# load models
|
8
|
+
Rails.application.eager_load!
|
9
|
+
|
10
|
+
# iterate over models
|
11
|
+
ApplicationRecord.descendants.each do |klass|
|
12
|
+
Integer.class_eval do
|
13
|
+
method = "to_#{klass.to_s.underscore}"
|
14
|
+
if respond_to?(method)
|
15
|
+
# such method exists
|
16
|
+
else
|
17
|
+
define_method method do
|
18
|
+
klass.find(self)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
Array.class_eval do
|
24
|
+
method = "to_#{klass.to_s.underscore.pluralize}"
|
25
|
+
if respond_to?(method)
|
26
|
+
# such method exists
|
27
|
+
else
|
28
|
+
define_method method do
|
29
|
+
klass.where(klass.primary_key => self)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
3
36
|
end
|
4
37
|
end
|
data/lib/just_for_fun/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: just_for_fun
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Igor Kasyanchuk
|
@@ -38,6 +38,20 @@ dependencies:
|
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: 1.3.6
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: pry
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
41
55
|
description: Find your models with shortcuts. Transform numbers and arrays into AR
|
42
56
|
objects.
|
43
57
|
email:
|