constantizable 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a14d1a27b451918b97c68bb2e22bc4a66d460a5e
4
+ data.tar.gz: d56a6c3559c2185dcbce35f44152965c44d3c262
5
+ SHA512:
6
+ metadata.gz: 785d69c96c41ac2d0681ae6a24b986d417eeb0fe3b9a7251720bf13f0ef2da9df68fc7008fdf27e76ec84d49e1d11713187cf9602a177592a1783496383fda3f
7
+ data.tar.gz: 782983fc339112987fd3a3121a7a33b0a65505c15e87b9b2fc2de6825dbedd7bf15641f998932ff3497752a7ff74c71dea3d225b2f549ced41c3a2fe8d2cc8f6
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2014 YOURNAME
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,32 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'Constantizable'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+
18
+
19
+
20
+ Bundler::GemHelper.install_tasks
21
+
22
+ require 'rake/testtask'
23
+
24
+ Rake::TestTask.new(:test) do |t|
25
+ t.libs << 'lib'
26
+ t.libs << 'test'
27
+ t.pattern = 'test/**/*_test.rb'
28
+ t.verbose = false
29
+ end
30
+
31
+
32
+ task default: :test
@@ -0,0 +1,77 @@
1
+ module Constantizable
2
+ extend ActiveSupport::Concern
3
+ # Constantizable extends `ActiveSupport::Concern` for some of the rails niceties
4
+ # like, `ClassMethods` and to include it into `ActiveRecord::Base`.
5
+
6
+ module ClassMethods
7
+ def constantize_column(column)
8
+ # This method is set as a class method as it can directly be invoked from model definitions
9
+ # as follows.
10
+
11
+ # Class Country < ActiveRecord::Base
12
+ # constantize_column :name
13
+ # end
14
+
15
+ @constantized_column = column
16
+ end
17
+
18
+ private
19
+ def method_missing(method, *args, &block)
20
+ # In the case that an object's constantized column value corresponds to the method name,
21
+ # the object is returned, else execution is delegated to the default `method_missing`
22
+ # implementation.
23
+
24
+ # If Column name isn't present it should fallback to the default `method_missing`
25
+ # implementation.
26
+
27
+ column_name = @constantized_column
28
+ super if column_name.nil?
29
+
30
+ # The value of the constantized column needs to be titleized or underscored,
31
+ # for the implementation to work.
32
+ # (eg)
33
+ # Country with name "United States Of America", will correspond to the query,
34
+ # Country.united_states_of_america.
35
+ # Country with name "India", will correspond to the query,
36
+ # Country.india.
37
+ # Country with name "united_kingdom", will correspond to the query,
38
+ # Country.united_kingdom.
39
+
40
+ record = (
41
+ self.find_by("#{column_name} = ? or #{column_name} = ?", method.to_s, method.to_s.titleize)
42
+ )
43
+
44
+ if record.present?
45
+ record
46
+ else
47
+ super
48
+ end
49
+
50
+ rescue
51
+ super
52
+ end
53
+ end
54
+
55
+
56
+ def method_missing(method, *args, &block)
57
+ # Refer https://github.com/rails/rails/blob/master/activesupport/lib/active_support/string_inquirer.rb
58
+ # Inquiry happens only if method is an inquiry method (i.e) method name ends with a '?'
59
+
60
+ if method[-1] == '?'
61
+ # If Column name isn't present it should fallback to the default `method_missing`
62
+ # implementation.
63
+
64
+ column_name = self.class.instance_variable_get(:@constantized_column)
65
+ super if column_name.nil?
66
+
67
+ # The value of the constantized column needs to be titleized or underscored.
68
+
69
+ data = self.send(column_name)
70
+ method[0..-2] == data.titleize.tr(' ','').underscore
71
+ else
72
+ super
73
+ end
74
+ end
75
+ end
76
+
77
+ ActiveRecord::Base.send(:include, Constantizable)
@@ -0,0 +1,3 @@
1
+ module Constantizable
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :constantizable do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: constantizable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Abhishek Sarkar
8
+ - Yuvaraja Balamurugan
9
+ - Nithin Krishna
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2014-09-27 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rails
17
+ requirement: !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - "~>"
20
+ - !ruby/object:Gem::Version
21
+ version: 4.1.1
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - "~>"
27
+ - !ruby/object:Gem::Version
28
+ version: 4.1.1
29
+ - !ruby/object:Gem::Dependency
30
+ name: sqlite3
31
+ requirement: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ type: :development
37
+ prerelease: false
38
+ version_requirements: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ - !ruby/object:Gem::Dependency
44
+ name: rspec-rails
45
+ requirement: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ type: :development
51
+ prerelease: false
52
+ version_requirements: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ - !ruby/object:Gem::Dependency
58
+ name: debugger
59
+ requirement: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - "~>"
62
+ - !ruby/object:Gem::Version
63
+ version: 1.6.8
64
+ type: :development
65
+ prerelease: false
66
+ version_requirements: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - "~>"
69
+ - !ruby/object:Gem::Version
70
+ version: 1.6.8
71
+ description: An elegant way to query constant tables, and inquire constant table objects.
72
+ email:
73
+ - abhishek@codebrahma.com
74
+ - yuva@codebrahma.com
75
+ - nithin@codebrahma.com
76
+ executables: []
77
+ extensions: []
78
+ extra_rdoc_files: []
79
+ files:
80
+ - MIT-LICENSE
81
+ - Rakefile
82
+ - lib/constantizable.rb
83
+ - lib/constantizable/version.rb
84
+ - lib/tasks/constantizable_tasks.rake
85
+ homepage: http://codebrahma.com/constantizable
86
+ licenses:
87
+ - MIT
88
+ metadata: {}
89
+ post_install_message:
90
+ rdoc_options: []
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ requirements: []
104
+ rubyforge_project:
105
+ rubygems_version: 2.2.2
106
+ signing_key:
107
+ specification_version: 4
108
+ summary: An elegant way to query constant tables, and inquire constant table objects.
109
+ test_files: []