objectified 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 004ba17c7d6671cca9f8dadad6359197c3d765b47aa64660cd228d18280b201f
4
+ data.tar.gz: 3c7251ef410290046630a2e93f26acd07beb122efac6755ba88d0fa6b575bf42
5
+ SHA512:
6
+ metadata.gz: c4d574e699b63accb1386e1139186524e4fd3a5ca84aa1b66c73902dd955909acbbd74f54472f1edad0d493b00911f667b340af35643c81a1d5c3d7ed369c38b
7
+ data.tar.gz: 0333f648e1163ec41013215066ad65bc2f991b335023deb389b13f716dd6de98f5787a3eb54e6680509d5a80bd3d4e3a27d0bb3b38a6e5e91888ca8d8f5c8e77
File without changes
@@ -0,0 +1,39 @@
1
+ # Objectified
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/objectified`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'objectified'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install objectified
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
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.
30
+
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).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/objectified.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,204 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "objectified/version"
4
+
5
+ require "active_support"
6
+ require "active_support/rails"
7
+ require "active_support/core_ext/string/inflections"
8
+ require "active_support/lazy_load_hooks"
9
+
10
+ # include this module to have resource based, meta programming variables.
11
+ module Objectified
12
+ extend ActiveSupport::Autoload
13
+
14
+ def self.included(base)
15
+ base.class_eval do
16
+ extend ClassMethods
17
+
18
+ def self.inherited(subclass)
19
+ super
20
+ subclass.object_type(object_type_string)
21
+ end
22
+ end
23
+ end
24
+
25
+ # e.g. define object_type :finder for ApplicationFinder
26
+ module ClassMethods
27
+ attr_accessor :object_type_string
28
+
29
+ #
30
+ # The entry point of the api is the :object_type method.
31
+ #
32
+ #
33
+ # Note:
34
+ # Do not use for classes with the prefix 'Application'.
35
+ #
36
+ # Args:
37
+ # object_type_string: (String)
38
+ #
39
+ # Example:
40
+ # class SomeController
41
+ # object_type :controller
42
+ # end
43
+ #
44
+ # SomeController.resource_klass_string
45
+ # => 'Some'
46
+ #
47
+
48
+ def object_type(object_type_string)
49
+ @object_type_string = object_type_string
50
+ end
51
+
52
+
53
+ #
54
+ # Returns the 'object_type' but capitalized and stringified
55
+ #
56
+ # Example:
57
+ # SomeController.object_type_klass_string
58
+ # => 'Controller'
59
+ #
60
+
61
+ def object_type_klass_string
62
+ @object_type_string&.to_s&.camelcase
63
+ end
64
+
65
+
66
+ #
67
+ # Returns the pluralized resource name
68
+ #
69
+ # Example:
70
+ # SomeController.resources_klass_string
71
+ # => 'Somes'
72
+ #
73
+
74
+ def resources_klass_string
75
+ unless object_type_klass_string
76
+ raise 'No Object Type Defined. please override object_type in your class => e.g. object_type :controller'
77
+ end
78
+
79
+ to_s.gsub(object_type_klass_string, '').pluralize
80
+ end
81
+
82
+
83
+ #
84
+ # Returns the singularized resource name
85
+ #
86
+ # Example:
87
+ # SomeController.resource_klass_string
88
+ # => 'Some'
89
+ #
90
+
91
+ def resources_klass_string
92
+ resources_klass_string.singularize
93
+ end
94
+
95
+
96
+ #
97
+ # Returns the constantized class name of the resource
98
+ #
99
+ # Example:
100
+ # SomeController.records_klass
101
+ # => 'Some'
102
+ #
103
+
104
+ def records_klass
105
+ return if resource_klass_string == 'Application'
106
+
107
+ resource_klass_string&.constantize
108
+ rescue NameError
109
+ resource_klass_string
110
+ end
111
+
112
+
113
+ #
114
+ # Returns the name of a the **collection variable** for the resource class, with the option of stripping it's namespaces.
115
+ #
116
+ # Example:
117
+ # SomeNamespace::SomeController.records_instance_variable_name
118
+ # => 'some_namespace_somes'
119
+ #
120
+ # Example:
121
+ # SomeNamespace::SomeController.records_instance_variable_name(namespace: true)
122
+ # => 'somes'
123
+ #
124
+
125
+ def records_instance_variable_name(namespace: false)
126
+ return if resource_klass_string == 'Application'
127
+
128
+ if namespace
129
+ records_klass.to_s.underscore.downcase.pluralize.gsub('/', '_')
130
+ else
131
+ records_klass.to_s.underscore.downcase.pluralize.split('/').last
132
+ end
133
+ end
134
+
135
+
136
+ #
137
+ # Returns the name of a the **member variable** for the resource class, with the option of stripping it's namespaces.
138
+ #
139
+ # Example:
140
+ # SomeNamespace::SomeController.record_instance_variable_name
141
+ # => 'some_namespace_some'
142
+ #
143
+ # Example:
144
+ # SomeNamespace::SomeController.record_instance_variable_name(namespace: true)
145
+ # => 'some'
146
+ #
147
+
148
+ def record_instance_variable_name(namespace: false)
149
+ return if resource_klass_string == 'Application'
150
+
151
+ if namespace
152
+ records_klass.to_s.underscore.downcase
153
+ else
154
+ records_klass.to_s.underscore.downcase.split('/').last
155
+ end
156
+ end
157
+
158
+
159
+ #
160
+ # Mixin to extract class names on demand.
161
+ #
162
+ # Example:
163
+ # SomeNamespace::SomeController.record_instance_variable_name
164
+ # => 'some_namespace_some'
165
+ #
166
+ # Example:
167
+ # SomeNamespace::SomeController.record_instance_variable_name(namespace: true)
168
+ # => 'some'
169
+ #
170
+ def object_klass_for(object_type)
171
+ str = "#{resource_klass_string.pluralize}#{object_type.to_s.camelize}"
172
+ str.constantize
173
+ end
174
+ end
175
+
176
+ #
177
+ # Instance Extensions of Class Methods
178
+ #
179
+
180
+ def records_instance_variable_name(namespace: false)
181
+ self.class.records_instance_variable_name(namespace: namespace)
182
+ end
183
+
184
+ def record_instance_variable_name(namespace: false)
185
+ self.class.record_instance_variable_name(namespace: namespace)
186
+ end
187
+
188
+ def records_klass
189
+ self.class.records_klass
190
+ end
191
+
192
+ def resource_klass_string
193
+ self.class.resource_klass_string
194
+ end
195
+
196
+ def object_klass_for(object_type)
197
+ self.class.object_klass_for(object_type)
198
+ end
199
+
200
+ def resources_klass_string
201
+ self.class.resources_klass_string
202
+ end
203
+ end
204
+
@@ -0,0 +1,3 @@
1
+ module Objectified
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: objectified
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Guilherme Andrade
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-07-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.17'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.17'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: activesupport
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 6.0.3
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 6.0.3
55
+ description: Extract resource based object names with ease.
56
+ email:
57
+ - guilherme.andrade.ao@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - Changelog.md
63
+ - README.md
64
+ - lib/objectified.rb
65
+ - lib/objectified/version.rb
66
+ homepage: https://github.com/guilherme-andrade/objectified
67
+ licenses:
68
+ - MIT
69
+ metadata:
70
+ homepage_uri: https://github.com/guilherme-andrade/objectified
71
+ source_code_uri: https://github.com/guilherme-andrade/objectified
72
+ changelog_uri: https://github.com/guilherme-andrade/objectified/Changelog.md
73
+ post_install_message:
74
+ rdoc_options: []
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ requirements: []
88
+ rubygems_version: 3.0.3
89
+ signing_key:
90
+ specification_version: 4
91
+ summary: A simple mixin with meta coding utilities
92
+ test_files: []