rails_agnostic_models 0.0.5 → 0.0.6

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.
@@ -48,8 +48,20 @@ module RailsAgnosticModels
48
48
 
49
49
  # safely refer to constants that may not be defined. Useful in a gem that might get included in places that might not define EVERY active record model, such as
50
50
  # a satelite administration application. Note that you will still need to handle nil cases
51
- def safe_constant(constant_sym)
52
- return Object.const_get(constant_sym) if Object.const_defined? constant_sym
51
+ #
52
+ # safe_constant(:Object) # => Object
53
+ # safe_constant(:ClassThatDoesNotExist) # => nil
54
+ # safe_constant("Object") # => Object
55
+ # safe_constant("MyModule::MyClass") # => MyModule::MyClass
56
+ def safe_constant(constant)
57
+ case constant
58
+ when Symbol then
59
+ return top_level_constant_lookup constant
60
+ when String then
61
+ drill_down_constant_lookup(constant)
62
+ else
63
+ return nil
64
+ end
53
65
  end
54
66
 
55
67
  # Defines the appropraite scope based on the version of Rails
@@ -69,6 +81,29 @@ module RailsAgnosticModels
69
81
  self.inheritance_column = column_name
70
82
  end
71
83
  end
84
+
85
+ private
86
+
87
+ def drill_down_constant_lookup(constant)
88
+ constant_tokens = constant.split("::")
89
+ first_token = constant_tokens.first
90
+ return_constant = top_level_constant_lookup first_token
91
+ # go ahead and cut bait if the top level constant isn't there
92
+ return nil unless return_constant
93
+ constant_tokens.drop(1).each do | token |
94
+ if return_constant.constants.include?(token.to_sym)
95
+ return_constant = return_constant.const_get(token)
96
+ else
97
+ return nil
98
+ end
99
+ end
100
+ return return_constant
101
+ end
102
+
103
+ def top_level_constant_lookup(constant)
104
+ Object.const_get(constant) if Object.const_defined? constant
105
+ end
106
+
72
107
  end
73
108
  def self.included(klass)
74
109
  klass.extend(ClassMethods)
@@ -1,3 +1,3 @@
1
1
  module RailsAgnosticModels
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
@@ -0,0 +1,36 @@
1
+ require 'spec_helper'
2
+
3
+ class MyClass < ActiveRecord::Base
4
+ end
5
+
6
+ class OtherClass < ActiveRecord::Base
7
+ NUMBER = 1
8
+ end
9
+
10
+ module MyModule
11
+ class MyClass < ActiveRecord::Base
12
+ end
13
+ end
14
+
15
+ describe "#self.safe_constant" do
16
+ it "accepts strings" do
17
+ MyClass.send(:safe_constant, "OtherClass").should eq OtherClass
18
+ end
19
+ it "accepts symbols" do
20
+ MyClass.send(:safe_constant, :OtherClass).should eq OtherClass
21
+ end
22
+ it "does not return the constant if it's not defined" do
23
+ MyClass.send(:safe_constant, :SomeOtherClass).should be_nil
24
+ end
25
+ it "returns the constant if it's defined" do
26
+ MyClass.send(:safe_constant, :OtherClass).should eq OtherClass
27
+ end
28
+ context "namespaced constants" do
29
+ it "returns namedspaced constants" do
30
+ MyClass.send(:safe_constant, "MyModule::MyClass").should eq MyModule::MyClass
31
+ end
32
+ it "returns nil if it doesn't exist" do
33
+ MyClass.send(:safe_constant, "MyModule::SOMETHING_FAKE").should be_nil
34
+ end
35
+ end
36
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_agnostic_models
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -141,6 +141,7 @@ files:
141
141
  - lib/rails_agnostic_models/active_record_extensions.rb
142
142
  - lib/rails_agnostic_models/version.rb
143
143
  - rails_agnostic_models.gemspec
144
+ - spec/rails_agnostic_models/safe_constant_spec.rb
144
145
  - spec/rails_agnostic_models/version_agnostic_inheritance_column_spec.rb
145
146
  - spec/rails_agnostic_models/version_agnostic_scope_spec.rb
146
147
  - spec/spec_helper.rb
@@ -159,7 +160,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
159
160
  version: '0'
160
161
  segments:
161
162
  - 0
162
- hash: -2644159498259307497
163
+ hash: -1154643741630881998
163
164
  required_rubygems_version: !ruby/object:Gem::Requirement
164
165
  none: false
165
166
  requirements:
@@ -168,7 +169,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
168
169
  version: '0'
169
170
  segments:
170
171
  - 0
171
- hash: -2644159498259307497
172
+ hash: -1154643741630881998
172
173
  requirements: []
173
174
  rubyforge_project:
174
175
  rubygems_version: 1.8.24
@@ -177,6 +178,7 @@ specification_version: 3
177
178
  summary: Extends activerecord to provide rails-agnostic versions of common model code
178
179
  to east the pain of upgrading
179
180
  test_files:
181
+ - spec/rails_agnostic_models/safe_constant_spec.rb
180
182
  - spec/rails_agnostic_models/version_agnostic_inheritance_column_spec.rb
181
183
  - spec/rails_agnostic_models/version_agnostic_scope_spec.rb
182
184
  - spec/spec_helper.rb