static_list 0.1.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -3,6 +3,8 @@ source "http://rubygems.org"
3
3
  # Example:
4
4
  # gem "activesupport", ">= 2.3.5"
5
5
 
6
+ gem 'activesupport', '>= 2.3.4'
7
+
6
8
  # Add dependencies to develop your gem here.
7
9
  # Include everything needed to run rake, tests, features, etc.
8
10
  group :development do
@@ -10,4 +12,7 @@ group :development do
10
12
  gem "bundler", "~> 1.0.0"
11
13
  gem "jeweler", "~> 1.5.2"
12
14
  gem "rcov", ">= 0"
15
+ gem 'rspec', '>= 2.5.0'
16
+ gem 'i18n'
13
17
  end
18
+
data/Gemfile.lock CHANGED
@@ -1,20 +1,34 @@
1
1
  GEM
2
2
  remote: http://rubygems.org/
3
3
  specs:
4
+ activesupport (3.0.5)
5
+ diff-lcs (1.1.2)
4
6
  git (1.2.5)
7
+ i18n (0.5.0)
5
8
  jeweler (1.5.2)
6
9
  bundler (~> 1.0.0)
7
10
  git (>= 1.2.5)
8
11
  rake
9
12
  rake (0.8.7)
10
13
  rcov (0.9.9)
14
+ rspec (2.5.0)
15
+ rspec-core (~> 2.5.0)
16
+ rspec-expectations (~> 2.5.0)
17
+ rspec-mocks (~> 2.5.0)
18
+ rspec-core (2.5.1)
19
+ rspec-expectations (2.5.0)
20
+ diff-lcs (~> 1.1.2)
21
+ rspec-mocks (2.5.0)
11
22
  shoulda (2.11.3)
12
23
 
13
24
  PLATFORMS
14
25
  ruby
15
26
 
16
27
  DEPENDENCIES
28
+ activesupport (>= 2.3.4)
17
29
  bundler (~> 1.0.0)
30
+ i18n
18
31
  jeweler (~> 1.5.2)
19
32
  rcov
33
+ rspec (>= 2.5.0)
20
34
  shoulda
data/README.rdoc CHANGED
@@ -13,52 +13,52 @@ Example :
13
13
  (I want to store the hair color of the user...)
14
14
 
15
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
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
20
 
21
21
  (user.rb)
22
- class User < ActiveRecord::Base
23
- ...
24
- include StaticList::Validate
22
+ class User < ActiveRecord::Base
23
+ ...
24
+ include StaticList::Validate
25
25
 
26
- validates_static_list_value :hair_color, HairColor, :allow_blank => true
27
- ...
28
- end
26
+ validates_static_list_value :hair_color, HairColor, :allow_blank => true
27
+ ...
28
+ end
29
29
 
30
30
  (application_helper.rb)
31
- module ApplicationHelper
32
- ...
33
- include StaticList::Helpers
34
- ...
35
- end
31
+ module ApplicationHelper
32
+ ...
33
+ include StaticList::Helpers
34
+ ...
35
+ end
36
36
 
37
37
  (_form.html.erb)
38
- ...
39
- <%= f.select :hair_color, static_list_select_options(HairColor) %>
40
- ...
38
+ ...
39
+ <%= f.select :hair_color, static_list_select_options(HairColor) %>
40
+ ...
41
41
 
42
42
  (show.html.erb)
43
- ...
44
- <%= t_static_list(@user.hair_color, HairColor) %>
45
- ...
43
+ ...
44
+ <%= t_static_list(@user.hair_color, HairColor) %>
45
+ ...
46
46
 
47
47
  (en.yml)
48
- ...
49
- hair_color:
50
- white: white
51
- blond: blond
52
- red: red
53
- ...
48
+ ...
49
+ hair_color:
50
+ white: white
51
+ blond: blond
52
+ red: red
53
+ ...
54
54
 
55
55
  (fr.yml)
56
- ...
57
- hair_color:
58
- white: blancs
59
- blond: blonds
60
- red: rouges
61
- ...
56
+ ...
57
+ hair_color:
58
+ white: blancs
59
+ blond: blonds
60
+ red: rouges
61
+ ...
62
62
 
63
63
  == Copyright
64
64
 
data/Rakefile CHANGED
@@ -26,24 +26,18 @@ Jeweler::Tasks.new do |gem|
26
26
  # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
27
27
  # gem.add_runtime_dependency 'jabber4r', '> 0.1'
28
28
  # gem.add_development_dependency 'rspec', '> 1.2.3'
29
+ gem.add_runtime_dependency 'activesupport', '>= 2.3.4'
29
30
  end
30
31
  Jeweler::RubygemsDotOrgTasks.new
31
32
 
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
33
+ require "rspec"
34
+ require "rspec/core/rake_task"
38
35
 
39
- require 'rcov/rcovtask'
40
- Rcov::RcovTask.new do |test|
41
- test.libs << 'test'
42
- test.pattern = 'test/**/test_*.rb'
43
- test.verbose = true
36
+ Rspec::Core::RakeTask.new(:spec) do |spec|
37
+ spec.pattern = "spec/**/*_spec.rb"
44
38
  end
45
39
 
46
- task :default => :test
40
+ task :default => :spec
47
41
 
48
42
  require 'rake/rdoctask'
49
43
  Rake::RDocTask.new do |rdoc|
@@ -54,3 +48,4 @@ Rake::RDocTask.new do |rdoc|
54
48
  rdoc.rdoc_files.include('README*')
55
49
  rdoc.rdoc_files.include('lib/**/*.rb')
56
50
  end
51
+
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.2.1
data/lib/static_list.rb CHANGED
@@ -1,4 +1,6 @@
1
- if Rails::VERSION::MAJOR < 3
1
+ require "active_support"
2
+
3
+ if ActiveSupport::VERSION::MAJOR < 3
2
4
  require "core_ext/concern"
3
5
  end
4
6
 
@@ -51,18 +53,22 @@ module StaticList
51
53
 
52
54
  # Returns the symbol associated with the code in parameter.
53
55
  #
54
- # For example : HairColor.static_list_code_to_sym(0) # => :white
56
+ # For example : HairColor.code_to_sym(0) # => :white
55
57
  #
56
- def static_list_code_to_sym(code)
58
+ def code_to_sym(code)
57
59
  static_list_codes.find { |el| el[1] == code }[0]
58
60
  end
59
61
 
60
- def t_symbol(code)
61
- "#{self.to_s.demodulize.underscore}.#{self.static_list_code_to_sym(code)}"
62
+ # Returns the code associated with the symbol in parameter.
63
+ #
64
+ # For example : HairColor.sym_to_code(:white) # => 0
65
+ #
66
+ def sym_to_code(sym)
67
+ static_list_codes.find {|el| el[0] == sym }[1]
62
68
  end
63
69
 
64
- def to_code(sym)
65
- static_list_codes.find { |el| el[0] == sym }[1]
70
+ def t_key_from_code(code)
71
+ "#{self.to_s.demodulize.underscore}.#{self.code_to_sym(code)}"
66
72
  end
67
73
 
68
74
  def static_codes
@@ -84,7 +90,7 @@ module StaticList
84
90
  #
85
91
  def t_static_list(code, static_object)
86
92
  return unless code
87
- t "#{static_object.to_s.demodulize.underscore}.#{static_object.static_list_code_to_sym(code)}"
93
+ t(static_object.t_key_from_code(code))
88
94
  end
89
95
 
90
96
  # Localizes all the static codes for select options helper
@@ -0,0 +1,9 @@
1
+ require 'rspec'
2
+ require "active_support/core_ext"
3
+
4
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'static_list')
5
+
6
+ RSpec.configure do |config|
7
+ config.mock_with :rspec
8
+ end
9
+
@@ -0,0 +1,85 @@
1
+ require "spec_helper"
2
+
3
+ class HairColor
4
+ include StaticList::Model
5
+ include StaticList::Validate
6
+ @@hair_color_list = [[:white, 1], [:blond, 2], [:red, 3], [:light_brown, 4], [:brown, 5], [:black, 6], [:colored, 7], [:bald, 8]]
7
+ cattr_accessor :hair_color_list
8
+
9
+ static_list @@hair_color_list
10
+ end
11
+
12
+ include StaticList::Helpers
13
+
14
+ describe HairColor do
15
+ describe ".static_list" do
16
+ it "should return the static list array" do
17
+ HairColor.static_list_codes.should == HairColor.hair_color_list
18
+ end
19
+ end
20
+
21
+ describe ".code_to_sym" do
22
+ it "should return the code from the symbol" do
23
+ HairColor.code_to_sym(3).should == :red
24
+ end
25
+ end
26
+
27
+ describe ".sym_to_code" do
28
+ it "should return the symbol from the code" do
29
+ HairColor.sym_to_code(:black).should == 6
30
+ end
31
+ end
32
+
33
+ describe ".all" do
34
+ it "should return all the list" do
35
+ HairColor.all.should have(8).elements
36
+ end
37
+ end
38
+
39
+ describe ".static_codes" do
40
+ it "should return all the codes" do
41
+ HairColor.static_codes.should == [:white, :blond, :red, :light_brown, :brown, :black, :colored, :bald]
42
+ end
43
+ end
44
+
45
+ describe ".static_keys" do
46
+ it "should return all the keys" do
47
+ HairColor.static_keys.should == [1, 2, 3, 4, 5, 6, 7, 8]
48
+ end
49
+ end
50
+
51
+ describe ".t_symbol" do
52
+ it "should return the correct translation key" do
53
+ HairColor.t_key_from_code(3).should == "hair_color.red"
54
+ end
55
+ end
56
+ end
57
+
58
+ describe "StaticList::Helpers" do
59
+ describe ".t_static_list" do
60
+ it "should return the translation" do
61
+ StaticList::Helpers.should_receive(:t).with("hair_color.white")
62
+
63
+ StaticList::Helpers.t_static_list(1, HairColor)
64
+ end
65
+ end
66
+
67
+ describe ".static_list_select_options" do
68
+ it "should return a list for a select" do
69
+ StaticList::Helpers.should_receive(:t_static_list).exactly(8).times.and_return("ok")
70
+
71
+ StaticList::Helpers.static_list_select_options(HairColor).should be_an(Array)
72
+ end
73
+ end
74
+ end
75
+
76
+ describe "StaticList::Validate" do
77
+ describe ".validates_static_list_value" do
78
+ it "should validate inclusion" do
79
+ HairColor.should_receive(:validates_inclusion_of).with(:hair_color_code, { :in => [1, 2, 3, 4, 5, 6, 7, 8] })
80
+
81
+ HairColor.validates_static_list_value(:hair_color_code, HairColor)
82
+ end
83
+ end
84
+ end
85
+
@@ -0,0 +1,83 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{static_list}
8
+ s.version = "0.2.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Yann Klis"]
12
+ s.date = %q{2011-03-01}
13
+ s.description = %q{
14
+ In your application you may want to handle things in your User model like sex (female, male) or other static lists.
15
+ You want these lists to be handled using 'textual keys' in your application but stored in your database using codes in an integer column.
16
+ }
17
+ s.email = %q{yann.klis@novelys.com}
18
+ s.extra_rdoc_files = [
19
+ "LICENSE.txt",
20
+ "README.rdoc"
21
+ ]
22
+ s.files = [
23
+ ".document",
24
+ "Gemfile",
25
+ "Gemfile.lock",
26
+ "LICENSE.txt",
27
+ "README.rdoc",
28
+ "Rakefile",
29
+ "VERSION",
30
+ "lib/core_ext/concern.rb",
31
+ "lib/static_list.rb",
32
+ "spec/spec_helper.rb",
33
+ "spec/static_list_spec.rb",
34
+ "static_list.gemspec",
35
+ "test/helper.rb",
36
+ "test/test_static_list.rb"
37
+ ]
38
+ s.homepage = %q{http://github.com/novelys/static_list}
39
+ s.licenses = ["MIT"]
40
+ s.require_paths = ["lib"]
41
+ s.rubygems_version = %q{1.5.0}
42
+ s.summary = %q{This gem is very useful to handle static lists (like enumerations).}
43
+ s.test_files = [
44
+ "spec/spec_helper.rb",
45
+ "spec/static_list_spec.rb",
46
+ "test/helper.rb",
47
+ "test/test_static_list.rb"
48
+ ]
49
+
50
+ if s.respond_to? :specification_version then
51
+ s.specification_version = 3
52
+
53
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
54
+ s.add_runtime_dependency(%q<activesupport>, [">= 2.3.4"])
55
+ s.add_development_dependency(%q<shoulda>, [">= 0"])
56
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
57
+ s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
58
+ s.add_development_dependency(%q<rcov>, [">= 0"])
59
+ s.add_development_dependency(%q<rspec>, [">= 2.5.0"])
60
+ s.add_development_dependency(%q<i18n>, [">= 0"])
61
+ s.add_runtime_dependency(%q<activesupport>, [">= 2.3.4"])
62
+ else
63
+ s.add_dependency(%q<activesupport>, [">= 2.3.4"])
64
+ s.add_dependency(%q<shoulda>, [">= 0"])
65
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
66
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
67
+ s.add_dependency(%q<rcov>, [">= 0"])
68
+ s.add_dependency(%q<rspec>, [">= 2.5.0"])
69
+ s.add_dependency(%q<i18n>, [">= 0"])
70
+ s.add_dependency(%q<activesupport>, [">= 2.3.4"])
71
+ end
72
+ else
73
+ s.add_dependency(%q<activesupport>, [">= 2.3.4"])
74
+ s.add_dependency(%q<shoulda>, [">= 0"])
75
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
76
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
77
+ s.add_dependency(%q<rcov>, [">= 0"])
78
+ s.add_dependency(%q<rspec>, [">= 2.5.0"])
79
+ s.add_dependency(%q<i18n>, [">= 0"])
80
+ s.add_dependency(%q<activesupport>, [">= 2.3.4"])
81
+ end
82
+ end
83
+
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: static_list
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 21
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
+ - 2
8
9
  - 1
9
- - 0
10
- version: 0.1.0
10
+ version: 0.2.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Yann Klis
@@ -15,12 +15,28 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-02-20 00:00:00 +01:00
18
+ date: 2011-03-01 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
22
- name: shoulda
22
+ name: activesupport
23
23
  version_requirements: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 11
29
+ segments:
30
+ - 2
31
+ - 3
32
+ - 4
33
+ version: 2.3.4
34
+ prerelease: false
35
+ type: :runtime
36
+ requirement: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: shoulda
39
+ version_requirements: &id002 !ruby/object:Gem::Requirement
24
40
  none: false
25
41
  requirements:
26
42
  - - ">="
@@ -31,10 +47,10 @@ dependencies:
31
47
  version: "0"
32
48
  prerelease: false
33
49
  type: :development
34
- requirement: *id001
50
+ requirement: *id002
35
51
  - !ruby/object:Gem::Dependency
36
52
  name: bundler
37
- version_requirements: &id002 !ruby/object:Gem::Requirement
53
+ version_requirements: &id003 !ruby/object:Gem::Requirement
38
54
  none: false
39
55
  requirements:
40
56
  - - ~>
@@ -47,10 +63,10 @@ dependencies:
47
63
  version: 1.0.0
48
64
  prerelease: false
49
65
  type: :development
50
- requirement: *id002
66
+ requirement: *id003
51
67
  - !ruby/object:Gem::Dependency
52
68
  name: jeweler
53
- version_requirements: &id003 !ruby/object:Gem::Requirement
69
+ version_requirements: &id004 !ruby/object:Gem::Requirement
54
70
  none: false
55
71
  requirements:
56
72
  - - ~>
@@ -63,10 +79,10 @@ dependencies:
63
79
  version: 1.5.2
64
80
  prerelease: false
65
81
  type: :development
66
- requirement: *id003
82
+ requirement: *id004
67
83
  - !ruby/object:Gem::Dependency
68
84
  name: rcov
69
- version_requirements: &id004 !ruby/object:Gem::Requirement
85
+ version_requirements: &id005 !ruby/object:Gem::Requirement
70
86
  none: false
71
87
  requirements:
72
88
  - - ">="
@@ -77,7 +93,53 @@ dependencies:
77
93
  version: "0"
78
94
  prerelease: false
79
95
  type: :development
80
- requirement: *id004
96
+ requirement: *id005
97
+ - !ruby/object:Gem::Dependency
98
+ name: rspec
99
+ version_requirements: &id006 !ruby/object:Gem::Requirement
100
+ none: false
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ hash: 27
105
+ segments:
106
+ - 2
107
+ - 5
108
+ - 0
109
+ version: 2.5.0
110
+ prerelease: false
111
+ type: :development
112
+ requirement: *id006
113
+ - !ruby/object:Gem::Dependency
114
+ name: i18n
115
+ version_requirements: &id007 !ruby/object:Gem::Requirement
116
+ none: false
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ hash: 3
121
+ segments:
122
+ - 0
123
+ version: "0"
124
+ prerelease: false
125
+ type: :development
126
+ requirement: *id007
127
+ - !ruby/object:Gem::Dependency
128
+ name: activesupport
129
+ version_requirements: &id008 !ruby/object:Gem::Requirement
130
+ none: false
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ hash: 11
135
+ segments:
136
+ - 2
137
+ - 3
138
+ - 4
139
+ version: 2.3.4
140
+ prerelease: false
141
+ type: :runtime
142
+ requirement: *id008
81
143
  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
144
  email: yann.klis@novelys.com
83
145
  executables: []
@@ -97,6 +159,9 @@ files:
97
159
  - VERSION
98
160
  - lib/core_ext/concern.rb
99
161
  - lib/static_list.rb
162
+ - spec/spec_helper.rb
163
+ - spec/static_list_spec.rb
164
+ - static_list.gemspec
100
165
  - test/helper.rb
101
166
  - test/test_static_list.rb
102
167
  has_rdoc: true
@@ -134,5 +199,7 @@ signing_key:
134
199
  specification_version: 3
135
200
  summary: This gem is very useful to handle static lists (like enumerations).
136
201
  test_files:
202
+ - spec/spec_helper.rb
203
+ - spec/static_list_spec.rb
137
204
  - test/helper.rb
138
205
  - test/test_static_list.rb