falls_back_on 0.0.2
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.
- data/LICENSE +20 -0
- data/README.rdoc +17 -0
- data/lib/falls_back_on.rb +50 -0
- data/lib/falls_back_on/active_record_ext.rb +5 -0
- data/lib/falls_back_on/app/models/fallback.rb +34 -0
- data/rails/init.rb +2 -0
- metadata +147 -0
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 Derek Kastner
|
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/README.rdoc
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
= falls_back_on
|
2
|
+
|
3
|
+
Description goes here.
|
4
|
+
|
5
|
+
== Note on Patches/Pull Requests
|
6
|
+
|
7
|
+
* Fork the project.
|
8
|
+
* Make your feature addition or bug fix.
|
9
|
+
* Add tests for it. This is important so I don't break it in a
|
10
|
+
future version unintentionally.
|
11
|
+
* Commit, do not mess with rakefile, version, or history.
|
12
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
13
|
+
* Send me a pull request. Bonus points for topic branches.
|
14
|
+
|
15
|
+
== Copyright
|
16
|
+
|
17
|
+
Copyright (c) 2010 Derek Kastner. See LICENSE for details.
|
@@ -0,0 +1,50 @@
|
|
1
|
+
module FallsBackOn
|
2
|
+
def falls_back_on(options = {})
|
3
|
+
class_eval do
|
4
|
+
class_inheritable_accessor :fallback_options
|
5
|
+
end
|
6
|
+
self.fallback_options = options
|
7
|
+
extend FallsBackOn::InstanceMethods
|
8
|
+
end
|
9
|
+
|
10
|
+
module InstanceMethods
|
11
|
+
def fallback
|
12
|
+
Fallback.get_for(class_name)
|
13
|
+
end
|
14
|
+
|
15
|
+
def fallback_value_for(k)
|
16
|
+
v = fallback_options[k]
|
17
|
+
case v
|
18
|
+
when :random
|
19
|
+
fallback_value_for_random(k)
|
20
|
+
when Numeric
|
21
|
+
v
|
22
|
+
when String
|
23
|
+
v.upcase.starts_with?('SELECT') ? ActiveRecord::Base.connection.select_value(v) : v
|
24
|
+
when Proc
|
25
|
+
v.call
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def fallback_value_for_name
|
30
|
+
{ :name => "Fallback #{class_name.underscore.gsub('_', ' ')}" }
|
31
|
+
end
|
32
|
+
|
33
|
+
def fallback_value_for_random(k)
|
34
|
+
random = rand(1e8)
|
35
|
+
return fallback_value_for_random(k) unless send("find_by_#{k}", random).nil?
|
36
|
+
random
|
37
|
+
end
|
38
|
+
|
39
|
+
def destroy_fallback
|
40
|
+
Fallback.destroy_for(class_name)
|
41
|
+
end
|
42
|
+
|
43
|
+
def set_fallback
|
44
|
+
values = {}
|
45
|
+
values = values.merge(fallback_value_for_name) if column_names.include?('name')
|
46
|
+
values = fallback_options.keys.inject({}) { |memo, k| memo.merge(k => fallback_value_for(k)) }
|
47
|
+
Fallback.set_for(class_name, values)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
class Fallback < ActiveRecord::Base
|
2
|
+
index :name if respond_to? :index
|
3
|
+
|
4
|
+
serialize :values
|
5
|
+
|
6
|
+
class << self
|
7
|
+
def get_for(name)
|
8
|
+
klass = name.constantize
|
9
|
+
if fallback = find_by_name(name)
|
10
|
+
begin
|
11
|
+
f = klass.new(fallback.values)
|
12
|
+
rescue ActiveRecord::UnknownAttributeError
|
13
|
+
raise $!, "while trying to use #{fallback.values.inspect} with #{name} #{$!.inspect}"
|
14
|
+
end
|
15
|
+
f.readonly!
|
16
|
+
f
|
17
|
+
elsif klass.set_fallback
|
18
|
+
get_for(name)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def set_for(name, values = {})
|
23
|
+
raise "fallback race condition" if Fallback.count > 100
|
24
|
+
fallback = find_or_create_by_name(name)
|
25
|
+
fallback.update_attributes(:values => values)
|
26
|
+
true
|
27
|
+
end
|
28
|
+
|
29
|
+
def destroy_for(name)
|
30
|
+
find_by_name(name).andand.destroy
|
31
|
+
true
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
data/rails/init.rb
ADDED
metadata
ADDED
@@ -0,0 +1,147 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: falls_back_on
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 2
|
10
|
+
version: 0.0.2
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Seamus Abshere
|
14
|
+
- Derek Kastner
|
15
|
+
autorequire:
|
16
|
+
bindir: bin
|
17
|
+
cert_chain: []
|
18
|
+
|
19
|
+
date: 2010-07-13 00:00:00 -04:00
|
20
|
+
default_executable:
|
21
|
+
dependencies:
|
22
|
+
- !ruby/object:Gem::Dependency
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
name: activerecord
|
26
|
+
version_requirements: &id001 !ruby/object:Gem::Requirement
|
27
|
+
none: false
|
28
|
+
requirements:
|
29
|
+
- - ">="
|
30
|
+
- !ruby/object:Gem::Version
|
31
|
+
hash: 3
|
32
|
+
segments:
|
33
|
+
- 0
|
34
|
+
version: "0"
|
35
|
+
requirement: *id001
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
type: :development
|
38
|
+
prerelease: false
|
39
|
+
name: rake
|
40
|
+
version_requirements: &id002 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
hash: 3
|
46
|
+
segments:
|
47
|
+
- 0
|
48
|
+
version: "0"
|
49
|
+
requirement: *id002
|
50
|
+
- !ruby/object:Gem::Dependency
|
51
|
+
type: :development
|
52
|
+
prerelease: false
|
53
|
+
name: jeweler
|
54
|
+
version_requirements: &id003 !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
hash: 3
|
60
|
+
segments:
|
61
|
+
- 0
|
62
|
+
version: "0"
|
63
|
+
requirement: *id003
|
64
|
+
- !ruby/object:Gem::Dependency
|
65
|
+
type: :development
|
66
|
+
prerelease: false
|
67
|
+
name: bundler
|
68
|
+
version_requirements: &id004 !ruby/object:Gem::Requirement
|
69
|
+
none: false
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
hash: 3
|
74
|
+
segments:
|
75
|
+
- 0
|
76
|
+
version: "0"
|
77
|
+
requirement: *id004
|
78
|
+
description: |
|
79
|
+
= falls_back_on
|
80
|
+
|
81
|
+
Description goes here.
|
82
|
+
|
83
|
+
== Note on Patches/Pull Requests
|
84
|
+
|
85
|
+
* Fork the project.
|
86
|
+
* Make your feature addition or bug fix.
|
87
|
+
* Add tests for it. This is important so I don't break it in a
|
88
|
+
future version unintentionally.
|
89
|
+
* Commit, do not mess with rakefile, version, or history.
|
90
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
91
|
+
* Send me a pull request. Bonus points for topic branches.
|
92
|
+
|
93
|
+
== Copyright
|
94
|
+
|
95
|
+
Copyright (c) 2010 Derek Kastner. See LICENSE for details.
|
96
|
+
|
97
|
+
email: seamus@brighterplanet.com
|
98
|
+
executables: []
|
99
|
+
|
100
|
+
extensions: []
|
101
|
+
|
102
|
+
extra_rdoc_files:
|
103
|
+
- LICENSE
|
104
|
+
- README.rdoc
|
105
|
+
files:
|
106
|
+
- lib/falls_back_on.rb
|
107
|
+
- lib/falls_back_on/active_record_ext.rb
|
108
|
+
- lib/falls_back_on/app/models/fallback.rb
|
109
|
+
- rails/init.rb
|
110
|
+
- LICENSE
|
111
|
+
- README.rdoc
|
112
|
+
has_rdoc: true
|
113
|
+
homepage: http://github.com/dkastner/falls_back_on
|
114
|
+
licenses: []
|
115
|
+
|
116
|
+
post_install_message:
|
117
|
+
rdoc_options:
|
118
|
+
- --charset=UTF-8
|
119
|
+
require_paths:
|
120
|
+
- lib
|
121
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
122
|
+
none: false
|
123
|
+
requirements:
|
124
|
+
- - ">="
|
125
|
+
- !ruby/object:Gem::Version
|
126
|
+
hash: 3
|
127
|
+
segments:
|
128
|
+
- 0
|
129
|
+
version: "0"
|
130
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
131
|
+
none: false
|
132
|
+
requirements:
|
133
|
+
- - ">="
|
134
|
+
- !ruby/object:Gem::Version
|
135
|
+
hash: 3
|
136
|
+
segments:
|
137
|
+
- 0
|
138
|
+
version: "0"
|
139
|
+
requirements: []
|
140
|
+
|
141
|
+
rubyforge_project:
|
142
|
+
rubygems_version: 1.3.7
|
143
|
+
signing_key:
|
144
|
+
specification_version: 3
|
145
|
+
summary: ActiveRecord extension to intelligently fall back on another column when a given column is unavailable
|
146
|
+
test_files: []
|
147
|
+
|