stipulate 1.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in stipulate.gemspec
4
+ gemspec
data/README ADDED
@@ -0,0 +1,38 @@
1
+ == Info ===========================================================
2
+ This gem was developed by Jonathan Martin for use in a number of Rails projects. Very often "statusesque" string fields are used for maximum flexibility in designing state-sensitive models, such as a "pending" order or an "inactive" user. Homebrew solutions leave the individual statuses undocumented and require string sensitive comparisons. This gem introduces readability and adds some convenient method generation so that code remains readable, but just as flexible as the string field backing it.
3
+
4
+ Be sure to check out www.nybblr.com for other Rails goodies, and if you're looking for more of my gems (when I get around to it!) go to nybblr.com/gems.
5
+
6
+ == Why Stipulate? ====================================================
7
+ If you don't specify what conditions apply, you might get sued.
8
+
9
+ == Usage ==========================================================
10
+ From your ActiveRecord model, simply call the stipulate method for each enumeratable type field:
11
+
12
+ class User < ActiveRecord::Base
13
+ # Example usages
14
+ stipulate :that => :status, :can_be => [:active, :inactive, :pending, :suspended]
15
+
16
+ stipulate :that => :type, :can_be => [:admin, :moderator, :end_user]
17
+
18
+ puts statuses # => "[:active, :inactive, :pending, :suspended]"
19
+
20
+ end
21
+
22
+ The stipulate method adds some convenience methods. As shown, a class method with the enumerated types is generated (for use in dropdowns, like this simple_form example):
23
+
24
+ <%= simple_form_for :user do |f| %>
25
+ <%= f.input :status,
26
+ :as => :select,
27
+ :collection => User.statuses.collect {|r| [r.to_s.titleize, r] } if can? :alter_role, f.object %>
28
+ <% end %>
29
+
30
+ For each enumeration, a boolean check method is generated:
31
+
32
+ User.last.admin?
33
+
34
+ To make sure CanCan works with cached DB objects, you can set the field as you normally would with a symbol or a string; both will be converted to a string, so in CanCan you can simply call:
35
+
36
+ can :edit, User, :status => "active"
37
+
38
+ ...and not need to worry if a symbol was assigned to the in-memory object. Symbols are preferred for assigning because they emphasize the finiteness of inputs (there are only so many different statuses), they stand out from regular strings, and they enforce lowercase convention.
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/lib/stipulate.rb ADDED
@@ -0,0 +1,53 @@
1
+ require "stipulate/version"
2
+
3
+ module Stipulate
4
+ def self.included(mod)
5
+ mod.extend ClassMethods
6
+ end
7
+
8
+ module ClassMethods
9
+ def stipulate(opts={})
10
+ method = opts[:that].to_s
11
+ enumeration = method.pluralize
12
+
13
+ singleton_class.class_eval <<-"END"
14
+ cattr_accessor :#{enumeration}
15
+ END
16
+
17
+ # If the method is customized, we want to retain changes through a wrapper
18
+ if instance_methods.include? method.to_sym
19
+ class_eval <<-"END"
20
+ # Ensure fields are converted to string
21
+ def #{method}_with_stipulation
22
+ v = #{method}_without_stipulation
23
+ v.nil? ? nil : v.to_s
24
+ end
25
+ END
26
+
27
+ alias_method_chain method.to_sym, :stipulation
28
+ else
29
+ class_eval <<-"END"
30
+ # Ensure fields are converted to string
31
+ def #{method}
32
+ v = attributes["#{method}"]
33
+ v.nil? ? nil : v.to_s
34
+ end
35
+ END
36
+ end
37
+
38
+ singleton_class.send :"#{enumeration}=", opts[:can_be]
39
+
40
+ for enum in opts[:can_be] do
41
+ class_eval <<-"END"
42
+ def #{enum}?
43
+ #{method}.to_s.to_sym == :#{enum}
44
+ end
45
+ END
46
+ end
47
+ end
48
+ end
49
+ end
50
+
51
+ class ActiveRecord::Base
52
+ include Stipulate
53
+ end
@@ -0,0 +1,3 @@
1
+ module Stipulate
2
+ VERSION = "1.0.5"
3
+ end
data/stipulate.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "stipulate/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "stipulate"
7
+ s.version = Stipulate::VERSION
8
+ s.authors = ["Jonathan Martin"]
9
+ s.email = ["me@nybblr.com"]
10
+ s.homepage = "http://nybblr.com"
11
+ s.summary = %q{Very often "statusesque" string fields are used for maximum flexibility in designing state-sensitive models, such as a "pending" order or an "inactive" user. Homebrew solutions leave the individual statuses undocumented and require string sensitive comparisons. This gem introduces readability and adds some convenient method generation so that code remains readable, but just as flexible as the string field backing it. Be sure to check out www.nybblr.com for other Rails goodies, and if you're looking for more of my gems (when I get around to it!) go to nybblr.com/gems.}
12
+ s.description = %q{Enumeration helper for ActiveRecord string fields}
13
+
14
+ s.rubyforge_project = "stipulate"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ # specify any dependencies here; for example:
22
+ # s.add_development_dependency "rspec"
23
+ # s.add_runtime_dependency "rest-client"
24
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: stipulate
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.5
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jonathan Martin
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-06-10 00:00:00.000000000Z
13
+ dependencies: []
14
+ description: Enumeration helper for ActiveRecord string fields
15
+ email:
16
+ - me@nybblr.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - README
24
+ - Rakefile
25
+ - lib/stipulate.rb
26
+ - lib/stipulate/version.rb
27
+ - stipulate.gemspec
28
+ homepage: http://nybblr.com
29
+ licenses: []
30
+ post_install_message:
31
+ rdoc_options: []
32
+ require_paths:
33
+ - lib
34
+ required_ruby_version: !ruby/object:Gem::Requirement
35
+ none: false
36
+ requirements:
37
+ - - ! '>='
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ required_rubygems_version: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ requirements: []
47
+ rubyforge_project: stipulate
48
+ rubygems_version: 1.8.15
49
+ signing_key:
50
+ specification_version: 3
51
+ summary: Very often "statusesque" string fields are used for maximum flexibility in
52
+ designing state-sensitive models, such as a "pending" order or an "inactive" user.
53
+ Homebrew solutions leave the individual statuses undocumented and require string
54
+ sensitive comparisons. This gem introduces readability and adds some convenient
55
+ method generation so that code remains readable, but just as flexible as the string
56
+ field backing it. Be sure to check out www.nybblr.com for other Rails goodies, and
57
+ if you're looking for more of my gems (when I get around to it!) go to nybblr.com/gems.
58
+ test_files: []