bits2life-rails_utils 0.0.1
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/MIT-LICENCE +20 -0
- data/README +18 -0
- data/lib/rails_utils.rb +1 -0
- data/lib/rails_utils/ordinal.rb +57 -0
- data/lib/rails_utils/version.rb +9 -0
- data/rails/init.rb +3 -0
- data/spec/ordinal_spec.rb +42 -0
- metadata +66 -0
data/MIT-LICENCE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2008 Erik Hansson
|
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
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
= Rails Utils
|
2
|
+
|
3
|
+
This is just a collection of utility classes and adaptations for Rails that I've come
|
4
|
+
to use on occasion. I've been torn between bunching them up or releasing them in
|
5
|
+
smaller packages, but came to the realization that I'd better not put a gazillion
|
6
|
+
12-line gems out there. My sanity will break first.
|
7
|
+
|
8
|
+
== Installation
|
9
|
+
|
10
|
+
Install like a rails plugin, either by:
|
11
|
+
|
12
|
+
script/plugin install git://github.com/bits2life/rails_utils.git
|
13
|
+
|
14
|
+
... or load the gem in your environment.rb:
|
15
|
+
|
16
|
+
config.gem 'bits2life-rails_utils', :source => "http://gems.github.com", :lib => "rails_utils"
|
17
|
+
|
18
|
+
If you choose the latter, also make sure you install the gem.
|
data/lib/rails_utils.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'rails_utils/ordinal.rb'
|
@@ -0,0 +1,57 @@
|
|
1
|
+
|
2
|
+
module RailsUtils
|
3
|
+
|
4
|
+
class OrdinalValue
|
5
|
+
include Comparable
|
6
|
+
|
7
|
+
def initialize(possible_values, value)
|
8
|
+
@possible_values = possible_values
|
9
|
+
set value
|
10
|
+
end
|
11
|
+
|
12
|
+
def set(value)
|
13
|
+
@value = @possible_values.index value.to_sym
|
14
|
+
end
|
15
|
+
|
16
|
+
def <=>(other)
|
17
|
+
@value <=> @possible_values.index(other.to_sym)
|
18
|
+
end
|
19
|
+
|
20
|
+
def to_s; @possible_values[@value].to_s; end
|
21
|
+
def to_sym; @possible_values[@value]; end
|
22
|
+
end
|
23
|
+
|
24
|
+
|
25
|
+
module ActiveRecord
|
26
|
+
module BaseExtensions
|
27
|
+
|
28
|
+
def ordinal_field(field_name, options = {})
|
29
|
+
define_method "#{field_name}" do
|
30
|
+
s = instance_variable_get "@#{field_name}_status"
|
31
|
+
if s.nil?
|
32
|
+
s = OrdinalValue.new options[:values], (self[field_name] || options[:default])
|
33
|
+
instance_variable_set "@#{field_name}_status", s
|
34
|
+
end
|
35
|
+
s
|
36
|
+
end
|
37
|
+
|
38
|
+
define_method "#{field_name}=" do |value|
|
39
|
+
s = self.send(field_name)
|
40
|
+
s.set value
|
41
|
+
self[field_name] = s.to_s
|
42
|
+
s
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def ordinal_fields(*args)
|
47
|
+
options = (Hash === args[-1]) ? args.pop : {}
|
48
|
+
|
49
|
+
args.each do |field_name|
|
50
|
+
ordinal_field field_name, options
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
data/rails/init.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
$:.push File.dirname(__FILE__) + "/../lib"
|
2
|
+
require File.dirname(__FILE__) + "/../init.rb"
|
3
|
+
|
4
|
+
|
5
|
+
describe "Ordinal" do
|
6
|
+
|
7
|
+
class MockUser
|
8
|
+
extend RailsUtils::ActiveRecord::BaseExtensions
|
9
|
+
|
10
|
+
ordinal_field :guild_access, :values => [:none, :member, :officer, :guildmaster], :default => :none
|
11
|
+
|
12
|
+
def [](key); instance_variable_get("@#{key.to_s}"); end
|
13
|
+
def []=(key, value); instance_variable_set("@#{key.to_s}", value); end
|
14
|
+
end
|
15
|
+
|
16
|
+
|
17
|
+
before(:each) do
|
18
|
+
@user = MockUser.new
|
19
|
+
@user.guild_access = :member
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should be comparable against known access levels" do
|
23
|
+
@user.guild_access.should >= :none
|
24
|
+
@user.guild_access.should >= :member
|
25
|
+
@user.guild_access.should_not >= :officer
|
26
|
+
@user.guild_access.should_not >= :guildmaster
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should raise an error if compared with unknown access level" do
|
30
|
+
lambda do
|
31
|
+
@user.guild_access.should >= :foobar
|
32
|
+
end.should raise_error
|
33
|
+
end
|
34
|
+
|
35
|
+
|
36
|
+
it "should be able to provide a default value" do
|
37
|
+
@user = MockUser.new
|
38
|
+
@user.guild_access.should >= :none
|
39
|
+
@user.guild_access.should_not >= :member
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
metadata
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: bits2life-rails_utils
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Erik Hansson
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-10-16 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description:
|
17
|
+
email: erik@bits2life.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- MIT-LICENCE
|
26
|
+
- README
|
27
|
+
- lib/rails_utils/ordinal.rb
|
28
|
+
- lib/rails_utils/version.rb
|
29
|
+
- lib/rails_utils.rb
|
30
|
+
- rails/init.rb
|
31
|
+
- spec/ordinal_spec.rb
|
32
|
+
has_rdoc: true
|
33
|
+
homepage: http://github.com/bits2life/rails_utils
|
34
|
+
post_install_message:
|
35
|
+
rdoc_options:
|
36
|
+
- --title
|
37
|
+
- RailsUtils
|
38
|
+
- --main
|
39
|
+
- README
|
40
|
+
- README
|
41
|
+
- lib
|
42
|
+
- spec
|
43
|
+
- --line-numbers
|
44
|
+
require_paths:
|
45
|
+
- lib
|
46
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: "0"
|
51
|
+
version:
|
52
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: "0"
|
57
|
+
version:
|
58
|
+
requirements: []
|
59
|
+
|
60
|
+
rubyforge_project:
|
61
|
+
rubygems_version: 1.2.0
|
62
|
+
signing_key:
|
63
|
+
specification_version: 2
|
64
|
+
summary: A number of small rails utilities and adaptations.
|
65
|
+
test_files: []
|
66
|
+
|