static_list 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "shoulda", ">= 0"
10
+ gem "bundler", "~> 1.0.0"
11
+ gem "jeweler", "~> 1.5.2"
12
+ gem "rcov", ">= 0"
13
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,20 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ git (1.2.5)
5
+ jeweler (1.5.2)
6
+ bundler (~> 1.0.0)
7
+ git (>= 1.2.5)
8
+ rake
9
+ rake (0.8.7)
10
+ rcov (0.9.9)
11
+ shoulda (2.11.3)
12
+
13
+ PLATFORMS
14
+ ruby
15
+
16
+ DEPENDENCIES
17
+ bundler (~> 1.0.0)
18
+ jeweler (~> 1.5.2)
19
+ rcov
20
+ shoulda
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010-2011 Novelys
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,72 @@
1
+ = Static List
2
+
3
+ This module is very useful to handle static lists (like enumerations).
4
+
5
+ == The problem:
6
+
7
+ In your application you may want to handle things in your User model like sex (female, male) or other static lists.
8
+ You want these lists to be handled using 'textual keys' in your application but stored in your database using codes in an integer column.
9
+ You don't want to join other tables to display these information.
10
+ You want these lists to be easily ordered, localized and translated using Rails i18n. You want view helpers to display these lists localized and validations helpers to validate the values in the 'receiving' model.
11
+
12
+ Example :
13
+ (I want to store the hair color of the user...)
14
+
15
+ (hair_color.rb)
16
+ class HairColor
17
+ include StaticList::Model
18
+ static_list [[:white, 1], [:blond, 2], [:red, 3], [:light_brown, 4], [:brown, 5], [:black, 6], [:colored, 7], [:bald, 8]]
19
+ end
20
+
21
+ (user.rb)
22
+ class User < ActiveRecord::Base
23
+ ...
24
+ include StaticList::Validate
25
+
26
+ validates_static_list_value :hair_color, HairColor, :allow_blank => true
27
+ ...
28
+ end
29
+
30
+ (application_helper.rb)
31
+ module ApplicationHelper
32
+ ...
33
+ include StaticList::Helpers
34
+ ...
35
+ end
36
+
37
+ (_form.html.erb)
38
+ ...
39
+ <%= f.select :hair_color, static_list_select_options(HairColor) %>
40
+ ...
41
+
42
+ (show.html.erb)
43
+ ...
44
+ <%= t_static_list(@user.hair_color, HairColor) %>
45
+ ...
46
+
47
+ (en.yml)
48
+ ...
49
+ hair_color:
50
+ white: white
51
+ blond: blond
52
+ red: red
53
+ ...
54
+
55
+ (fr.yml)
56
+ ...
57
+ hair_color:
58
+ white: blancs
59
+ blond: blonds
60
+ red: rouges
61
+ ...
62
+
63
+ == Copyright
64
+
65
+ Copyright (c) 2010-2011 Novelys[http://www.novelys.com]. See LICENSE.txt for details.
66
+
67
+ == Contributors
68
+
69
+ * {Yann Klis}[http://github.com/yannski]
70
+ * {Nicolas Blanco}[http://github.com/slainer68]
71
+ * {David Bourguignon}[http://github.com/dbourguignon]
72
+
data/Rakefile ADDED
@@ -0,0 +1,56 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
15
+ gem.name = "static_list"
16
+ gem.homepage = "http://github.com/novelys/static_list"
17
+ gem.license = "MIT"
18
+ gem.summary = %q{This gem is very useful to handle static lists (like enumerations).}
19
+ gem.description = %q{
20
+ In your application you may want to handle things in your User model like sex (female, male) or other static lists.
21
+ You want these lists to be handled using 'textual keys' in your application but stored in your database using codes in an integer column.
22
+ }
23
+ gem.email = "yann.klis@novelys.com"
24
+ gem.authors = ["Yann Klis"]
25
+ # Include your dependencies below. Runtime dependencies are required when using your gem,
26
+ # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
27
+ # gem.add_runtime_dependency 'jabber4r', '> 0.1'
28
+ # gem.add_development_dependency 'rspec', '> 1.2.3'
29
+ end
30
+ Jeweler::RubygemsDotOrgTasks.new
31
+
32
+ require 'rake/testtask'
33
+ Rake::TestTask.new(:test) do |test|
34
+ test.libs << 'lib' << 'test'
35
+ test.pattern = 'test/**/test_*.rb'
36
+ test.verbose = true
37
+ end
38
+
39
+ require 'rcov/rcovtask'
40
+ Rcov::RcovTask.new do |test|
41
+ test.libs << 'test'
42
+ test.pattern = 'test/**/test_*.rb'
43
+ test.verbose = true
44
+ end
45
+
46
+ task :default => :test
47
+
48
+ require 'rake/rdoctask'
49
+ Rake::RDocTask.new do |rdoc|
50
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
51
+
52
+ rdoc.rdoc_dir = 'rdoc'
53
+ rdoc.title = "static_list #{version}"
54
+ rdoc.rdoc_files.include('README*')
55
+ rdoc.rdoc_files.include('lib/**/*.rb')
56
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,30 @@
1
+ module ActiveSupport
2
+ module Concern
3
+ def self.extended(base)
4
+ base.instance_variable_set("@_dependencies", [])
5
+ end
6
+
7
+ def append_features(base)
8
+ if base.instance_variable_defined?("@_dependencies")
9
+ base.instance_variable_get("@_dependencies") << self
10
+ return false
11
+ else
12
+ return false if base < self
13
+ @_dependencies.each { |dep| base.send(:include, dep) }
14
+ super
15
+ base.extend const_get("ClassMethods") if const_defined?("ClassMethods")
16
+ base.send :include, const_get("InstanceMethods") if const_defined?("InstanceMethods")
17
+ base.class_eval(&@_included_block) if instance_variable_defined?("@_included_block")
18
+ end
19
+ end
20
+
21
+ def included(base = nil, &block)
22
+ if base.nil?
23
+ @_included_block = block
24
+ else
25
+ super
26
+ end
27
+ end
28
+ end
29
+ end
30
+
@@ -0,0 +1,99 @@
1
+ if Rails::VERSION::MAJOR < 3
2
+ require "core_ext/concern"
3
+ end
4
+
5
+ class Hashit
6
+ def initialize(hash)
7
+ hash.each do |k,v|
8
+ self.instance_variable_set("@#{k}", v) ## create and initialize an instance variable for this key/value pair
9
+ self.class.send(:define_method, k, proc{self.instance_variable_get("@#{k}")}) ## create the getter that returns the instance variable
10
+ self.class.send(:define_method, "#{k}=", proc{|v| self.instance_variable_set("@#{k}", v)}) ## create the setter that sets the instance variable
11
+ end
12
+ end
13
+ end
14
+
15
+ module StaticList
16
+ module Validate
17
+ extend ActiveSupport::Concern
18
+
19
+ module ClassMethods
20
+ # Method to validate in the receiving model that the value received is included in the static list model.
21
+ # For example :
22
+ # with_options(:allow_blank => true) do |options|
23
+ # options.validate_static_list_value :hair_color, HairColor
24
+ # options.validate_static_list_value :ethnicity, Ethnicity
25
+ # options.validate_static_list_value :sex, Sex
26
+ # end
27
+ def validates_static_list_value(attribute, model, options = {})
28
+ options.merge!(:in => model.static_list_codes.map { |el| el[1] })
29
+ validates_inclusion_of attribute, options
30
+ end
31
+ end
32
+ end
33
+
34
+ module Model
35
+ extend ActiveSupport::Concern
36
+
37
+ included do
38
+ cattr_accessor :static_list_codes
39
+ end
40
+
41
+ module ClassMethods
42
+ # Returns all elements of the static list
43
+ def all
44
+ static_list_codes.map{|x| Hashit.new(Hash[*["name", I18n.t("#{self.to_s.demodulize.underscore}.#{x.first.to_s}"), "id", x.last]])}
45
+ end
46
+
47
+ # Method to declare the static list in the static list model.
48
+ def static_list(list)
49
+ self.static_list_codes = list
50
+ end
51
+
52
+ # Returns the symbol associated with the code in parameter.
53
+ #
54
+ # For example : HairColor.static_list_code_to_sym(0) # => :white
55
+ #
56
+ def static_list_code_to_sym(code)
57
+ static_list_codes.find { |el| el[1] == code }[0]
58
+ end
59
+
60
+ def t_symbol(code)
61
+ "#{self.to_s.demodulize.underscore}.#{self.static_list_code_to_sym(code)}"
62
+ end
63
+
64
+ def to_code(sym)
65
+ static_list_codes.find { |el| el[0] == sym }[1]
66
+ end
67
+
68
+ def static_codes
69
+ static_list_codes.map{ |e| e[0] }
70
+ end
71
+
72
+ def static_keys
73
+ static_list_codes.map{ |e| e[1] }
74
+ end
75
+ end
76
+ end
77
+
78
+ module Helpers
79
+ # Localizes a static code
80
+ # For example :
81
+ # t_static_list(@user.hair_color, HairColor)
82
+ #
83
+ # will read the key hair_color.white
84
+ #
85
+ def t_static_list(code, static_object)
86
+ return unless code
87
+ t "#{static_object.to_s.demodulize.underscore}.#{static_object.static_list_code_to_sym(code)}"
88
+ end
89
+
90
+ # Localizes all the static codes for select options helper
91
+ #
92
+ # Example :
93
+ # f.select :hair_color, static_list_select_options(HairColor)
94
+ #
95
+ def static_list_select_options(static_object)
96
+ static_object.static_list_codes.map { |code| [t_static_list(code[1], static_object), code[1]] }
97
+ end
98
+ end
99
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+ require 'shoulda'
12
+
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
15
+ require 'static_list'
16
+
17
+ class Test::Unit::TestCase
18
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestStaticList < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,138 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: static_list
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Yann Klis
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-02-20 00:00:00 +01:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: shoulda
23
+ version_requirements: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ prerelease: false
33
+ type: :development
34
+ requirement: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: bundler
37
+ version_requirements: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ~>
41
+ - !ruby/object:Gem::Version
42
+ hash: 23
43
+ segments:
44
+ - 1
45
+ - 0
46
+ - 0
47
+ version: 1.0.0
48
+ prerelease: false
49
+ type: :development
50
+ requirement: *id002
51
+ - !ruby/object:Gem::Dependency
52
+ name: jeweler
53
+ version_requirements: &id003 !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ~>
57
+ - !ruby/object:Gem::Version
58
+ hash: 7
59
+ segments:
60
+ - 1
61
+ - 5
62
+ - 2
63
+ version: 1.5.2
64
+ prerelease: false
65
+ type: :development
66
+ requirement: *id003
67
+ - !ruby/object:Gem::Dependency
68
+ name: rcov
69
+ version_requirements: &id004 !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ hash: 3
75
+ segments:
76
+ - 0
77
+ version: "0"
78
+ prerelease: false
79
+ type: :development
80
+ requirement: *id004
81
+ description: "\n In your application you may want to handle things in your User model like sex (female, male) or other static lists.\n You want these lists to be handled using 'textual keys' in your application but stored in your database using codes in an integer column.\n "
82
+ email: yann.klis@novelys.com
83
+ executables: []
84
+
85
+ extensions: []
86
+
87
+ extra_rdoc_files:
88
+ - LICENSE.txt
89
+ - README.rdoc
90
+ files:
91
+ - .document
92
+ - Gemfile
93
+ - Gemfile.lock
94
+ - LICENSE.txt
95
+ - README.rdoc
96
+ - Rakefile
97
+ - VERSION
98
+ - lib/core_ext/concern.rb
99
+ - lib/static_list.rb
100
+ - test/helper.rb
101
+ - test/test_static_list.rb
102
+ has_rdoc: true
103
+ homepage: http://github.com/novelys/static_list
104
+ licenses:
105
+ - MIT
106
+ post_install_message:
107
+ rdoc_options: []
108
+
109
+ require_paths:
110
+ - lib
111
+ required_ruby_version: !ruby/object:Gem::Requirement
112
+ none: false
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ hash: 3
117
+ segments:
118
+ - 0
119
+ version: "0"
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ hash: 3
126
+ segments:
127
+ - 0
128
+ version: "0"
129
+ requirements: []
130
+
131
+ rubyforge_project:
132
+ rubygems_version: 1.5.0
133
+ signing_key:
134
+ specification_version: 3
135
+ summary: This gem is very useful to handle static lists (like enumerations).
136
+ test_files:
137
+ - test/helper.rb
138
+ - test/test_static_list.rb