selectable_attr 0.3.11 → 0.3.12
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/.gitignore +2 -0
- data/Rakefile +29 -43
- data/lib/selectable_attr/base.rb +31 -31
- data/lib/selectable_attr/enum.rb +30 -30
- data/lib/selectable_attr.rb +1 -1
- data/selectable_attr.gemspec +6 -3
- data/spec/selectable_attr_base_alias_spec.rb +23 -23
- data/spec/selectable_attr_enum_spec.rb +8 -8
- data/spec/spec_helper.rb +22 -5
- metadata +17 -5
data/.gitignore
CHANGED
data/Rakefile
CHANGED
@@ -1,23 +1,35 @@
|
|
1
|
-
require
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
require
|
6
|
-
require
|
7
|
-
|
1
|
+
require "bundler"
|
2
|
+
Bundler.setup
|
3
|
+
Bundler::GemHelper.install_tasks
|
4
|
+
|
5
|
+
require "rake"
|
6
|
+
require "yaml"
|
7
|
+
|
8
|
+
require "rake/rdoctask"
|
9
|
+
require "rspec/core/rake_task"
|
10
|
+
require "rspec/core/version"
|
11
|
+
# require "cucumber/rake/task"
|
12
|
+
|
8
13
|
desc 'Default: run unit tests.'
|
9
14
|
task :default => :spec
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
15
|
+
# task :default => [:spec, :cucumber]
|
16
|
+
|
17
|
+
desc "Run all examples"
|
18
|
+
RSpec::Core::RakeTask.new(:spec)
|
19
|
+
|
20
|
+
namespace :spec do
|
21
|
+
desc "Run all examples using rcov"
|
22
|
+
RSpec::Core::RakeTask.new(:rcov) do |t|
|
23
|
+
t.rcov = true
|
24
|
+
t.rcov_opts = %[--exclude "gems/*"]
|
25
|
+
# t.rcov_opts << %[--sort]
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
task :cleanup_rcov_files do
|
30
|
+
rm_rf 'coverage.data'
|
19
31
|
end
|
20
|
-
|
32
|
+
|
21
33
|
desc 'Generate documentation for the selectable_attr plugin.'
|
22
34
|
Rake::RDocTask.new(:rdoc) do |rdoc|
|
23
35
|
rdoc.rdoc_dir = 'rdoc'
|
@@ -26,29 +38,3 @@ Rake::RDocTask.new(:rdoc) do |rdoc|
|
|
26
38
|
rdoc.rdoc_files.include('README*')
|
27
39
|
rdoc.rdoc_files.include('lib/**/*.rb')
|
28
40
|
end
|
29
|
-
|
30
|
-
namespace :coverage do
|
31
|
-
desc "Delete aggregate coverage data."
|
32
|
-
task(:clean) { rm_f "coverage" }
|
33
|
-
|
34
|
-
desc "verify coverage threshold via RCov"
|
35
|
-
RCov::VerifyTask.new(:verify => :spec) do |t|
|
36
|
-
t.threshold = 100.0 # Make sure you have rcov 0.7 or higher!
|
37
|
-
t.index_html = 'coverage/index.html'
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
begin
|
42
|
-
require 'jeweler'
|
43
|
-
Jeweler::Tasks.new do |s|
|
44
|
-
s.name = "selectable_attr"
|
45
|
-
s.summary = "selectable_attr generates extra methods dynamically"
|
46
|
-
s.description = "selectable_attr generates extra methods dynamically for attribute which has options"
|
47
|
-
s.email = "akima@gmail.com"
|
48
|
-
s.homepage = "http://github.com/akm/selectable_attr/"
|
49
|
-
s.authors = ["Takeshi Akima"]
|
50
|
-
end
|
51
|
-
rescue LoadError
|
52
|
-
puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
|
53
|
-
end
|
54
|
-
|
data/lib/selectable_attr/base.rb
CHANGED
@@ -4,21 +4,21 @@ module SelectableAttr
|
|
4
4
|
def self.included(base)
|
5
5
|
base.extend(ClassMethods)
|
6
6
|
end
|
7
|
-
|
7
|
+
|
8
8
|
ENUM_ARRAY_METHODS = {
|
9
|
-
:none => {
|
9
|
+
:none => {
|
10
10
|
:to_hash_array => Proc.new do |enum, attr_value|
|
11
11
|
value = (attr_value || []).map(&:to_s)
|
12
12
|
enum.to_hash_array do |hash|
|
13
13
|
hash[:select] = value.include?(hash[:id].to_s)
|
14
14
|
end
|
15
15
|
end,
|
16
|
-
|
16
|
+
|
17
17
|
:to_attr_value => Proc.new do |enum, hash_array|
|
18
18
|
hash_array.select{|hash| hash[:select]}.map{|hash| hash[:id]}
|
19
19
|
end
|
20
20
|
},
|
21
|
-
|
21
|
+
|
22
22
|
:comma_string => {
|
23
23
|
:to_hash_array => Proc.new do |enum, attr_value|
|
24
24
|
values = attr_value.is_a?(Array) ? attr_value.map{|v|v.to_s} :
|
@@ -27,13 +27,13 @@ module SelectableAttr
|
|
27
27
|
hash[:select] = values.include?(hash[:id].to_s)
|
28
28
|
end
|
29
29
|
end,
|
30
|
-
|
30
|
+
|
31
31
|
:to_attr_value => Proc.new do |enum, hash_array|
|
32
32
|
hash_array.select{|hash| hash[:select]}.map{|hash| hash[:id]}.join(',')
|
33
33
|
end
|
34
34
|
},
|
35
|
-
|
36
|
-
|
35
|
+
|
36
|
+
|
37
37
|
:binary_string => {
|
38
38
|
:to_hash_array => Proc.new do |enum, attr_value|
|
39
39
|
value = attr_value || ''
|
@@ -43,7 +43,7 @@ module SelectableAttr
|
|
43
43
|
idx += 1
|
44
44
|
end
|
45
45
|
end,
|
46
|
-
|
46
|
+
|
47
47
|
:to_attr_value => Proc.new do |enum, hash_array|
|
48
48
|
result = ''
|
49
49
|
hash_map = hash_array.inject({}){|dest, hash| dest[hash[:id]] = hash; dest}
|
@@ -55,23 +55,23 @@ module SelectableAttr
|
|
55
55
|
end
|
56
56
|
}
|
57
57
|
}
|
58
|
-
|
58
|
+
|
59
59
|
module ClassMethods
|
60
|
-
def single_selectable_attrs
|
60
|
+
def single_selectable_attrs
|
61
61
|
@single_selectable_attrs_hash ||= {};
|
62
62
|
@single_selectable_attrs_hash[self] ||= []
|
63
63
|
end
|
64
|
-
|
65
|
-
def multi_selectable_attrs
|
64
|
+
|
65
|
+
def multi_selectable_attrs
|
66
66
|
@multi_selectable_attrs_hash ||= {};
|
67
67
|
@multi_selectable_attrs_hash[self] ||= []
|
68
68
|
end
|
69
|
-
|
69
|
+
|
70
70
|
def selectable_attr_type_for(attr)
|
71
71
|
single_selectable_attrs.include?(attr.to_s) ? :single :
|
72
72
|
multi_selectable_attrs.include?(attr.to_s) ? :multi : nil
|
73
73
|
end
|
74
|
-
|
74
|
+
|
75
75
|
def enum(*args, &block)
|
76
76
|
process_definition(block, *args) do |enum, context|
|
77
77
|
self.single_selectable_attrs << context[:attr].to_s
|
@@ -81,8 +81,8 @@ module SelectableAttr
|
|
81
81
|
end
|
82
82
|
alias_method :single_selectable_attr, :enum
|
83
83
|
alias_method :selectable_attr, :enum
|
84
|
-
|
85
|
-
|
84
|
+
|
85
|
+
|
86
86
|
def enum_array(*args, &block)
|
87
87
|
base_options = args.last.is_a?(Hash) ? args.pop : {}
|
88
88
|
args << base_options # .update({:attr_accessor => false})
|
@@ -93,7 +93,7 @@ module SelectableAttr
|
|
93
93
|
end
|
94
94
|
end
|
95
95
|
alias_method :multi_selectable_attr, :enum_array
|
96
|
-
|
96
|
+
|
97
97
|
def process_definition(block, *args)
|
98
98
|
base_options = args.last.is_a?(Hash) ? args.pop : {}
|
99
99
|
enum = base_options[:enum] || create_enum(&block)
|
@@ -115,7 +115,7 @@ module SelectableAttr
|
|
115
115
|
end
|
116
116
|
enum
|
117
117
|
end
|
118
|
-
|
118
|
+
|
119
119
|
def has_attr(attr)
|
120
120
|
return true if self.method_defined?(attr)
|
121
121
|
return false unless self.respond_to?(:columns)
|
@@ -129,11 +129,11 @@ module SelectableAttr
|
|
129
129
|
(respond_to?(:table_exists?) && self.table_exists?) ?
|
130
130
|
(self.columns || []).any?{|col|col.name.to_s == attr.to_s} : false
|
131
131
|
end
|
132
|
-
|
132
|
+
|
133
133
|
def attr_enumeable_base(*args, &block)
|
134
134
|
@base_name_processor = block
|
135
135
|
end
|
136
|
-
|
136
|
+
|
137
137
|
def enum_base_name(attr)
|
138
138
|
if @base_name_processor
|
139
139
|
@base_name_processor.call(attr).to_s
|
@@ -141,40 +141,40 @@ module SelectableAttr
|
|
141
141
|
attr.to_s.gsub(selectable_attr_name_pattern, '')
|
142
142
|
end
|
143
143
|
end
|
144
|
-
|
144
|
+
|
145
145
|
DEFAULT_SELECTABLE_ATTR_NAME_PATTERN = /(_cd$|_code$|_cds$|_codes$)/
|
146
|
-
|
146
|
+
|
147
147
|
def selectable_attr_name_pattern
|
148
148
|
@selectable_attr_name_pattern ||= DEFAULT_SELECTABLE_ATTR_NAME_PATTERN
|
149
149
|
end
|
150
150
|
alias_method :enum_name_pattern, :selectable_attr_name_pattern
|
151
|
-
|
151
|
+
|
152
152
|
def selectable_attr_name_pattern=(value)
|
153
153
|
@selectable_attr_name_pattern = value
|
154
154
|
end
|
155
155
|
alias_method :enum_name_pattern=, :selectable_attr_name_pattern=
|
156
|
-
|
156
|
+
|
157
157
|
def create_enum(&block)
|
158
158
|
result = Enum.new
|
159
159
|
result.instance_eval(&block)
|
160
160
|
result
|
161
161
|
end
|
162
|
-
|
162
|
+
|
163
163
|
def define_enum(context)
|
164
164
|
base_name = context[:base_name]
|
165
165
|
const_name = "#{base_name.upcase}_ENUM"
|
166
166
|
const_set(const_name, context[:enum]) unless const_defined?(const_name)
|
167
167
|
end
|
168
|
-
|
168
|
+
|
169
169
|
def enum_for(attr)
|
170
170
|
base_name = enum_base_name(attr)
|
171
171
|
name = "#{base_name.upcase}_ENUM"
|
172
172
|
const_defined?(name) ? const_get(name) : nil
|
173
173
|
end
|
174
|
-
|
174
|
+
|
175
175
|
def define_accessor(context)
|
176
176
|
attr = context[:attr]
|
177
|
-
return unless (instance_methods & [attr, "#{attr}="]).empty?
|
177
|
+
return unless (instance_methods & [attr, "#{attr}="]).empty?
|
178
178
|
if context[:attr_accessor]
|
179
179
|
if context[:default]
|
180
180
|
if respond_to?(:attr_accessor_with_default)
|
@@ -197,7 +197,7 @@ module SelectableAttr
|
|
197
197
|
end
|
198
198
|
end
|
199
199
|
end
|
200
|
-
|
200
|
+
|
201
201
|
def define_enum_class_methods(context)
|
202
202
|
base_name = context[:base_name]
|
203
203
|
enum = context[:enum]
|
@@ -225,7 +225,7 @@ module SelectableAttr
|
|
225
225
|
end
|
226
226
|
self.extend(mod)
|
227
227
|
end
|
228
|
-
|
228
|
+
|
229
229
|
def define_enum_instance_methods(context)
|
230
230
|
attr = context[:attr]
|
231
231
|
base_name = context[:base_name]
|
@@ -248,7 +248,7 @@ module SelectableAttr
|
|
248
248
|
EOS
|
249
249
|
self.module_eval(instance_methods)
|
250
250
|
end
|
251
|
-
|
251
|
+
|
252
252
|
def define_enum_array_instance_methods(context)
|
253
253
|
attr = context[:attr]
|
254
254
|
base_name = context[:base_name]
|
data/lib/selectable_attr/enum.rb
CHANGED
@@ -9,33 +9,33 @@ module SelectableAttr
|
|
9
9
|
@@instances ||= []
|
10
10
|
end
|
11
11
|
end
|
12
|
-
|
12
|
+
|
13
13
|
def initialize(&block)
|
14
14
|
@entries = []
|
15
15
|
instance_eval(&block) if block_given?
|
16
16
|
SelectableAttr::Enum.instances << self
|
17
17
|
end
|
18
|
-
|
18
|
+
|
19
19
|
def entries
|
20
20
|
@entries
|
21
21
|
end
|
22
|
-
|
22
|
+
|
23
23
|
def each(&block)
|
24
24
|
entries.each(&block)
|
25
25
|
end
|
26
|
-
|
26
|
+
|
27
27
|
def define(id, key, name, options = nil, &block)
|
28
28
|
entry = Entry.new(self, id, key, name, options, &block)
|
29
29
|
entry.instance_variable_set(:@defined_in_code, true)
|
30
30
|
@entries << entry
|
31
31
|
end
|
32
32
|
alias_method :entry, :define
|
33
|
-
|
33
|
+
|
34
34
|
def i18n_scope(*path)
|
35
35
|
@i18n_scope = path unless path.empty?
|
36
36
|
@i18n_scope
|
37
37
|
end
|
38
|
-
|
38
|
+
|
39
39
|
def match_entry(entry, value, *attrs)
|
40
40
|
attrs.any?{|attr| entry[attr].to_s == value.to_s}
|
41
41
|
end
|
@@ -43,49 +43,49 @@ module SelectableAttr
|
|
43
43
|
def entry_by(value, *attrs)
|
44
44
|
entries.detect{|entry| match_entry(entry, value, *attrs)} || Entry::NULL
|
45
45
|
end
|
46
|
-
|
46
|
+
|
47
47
|
def entry_by_id(id)
|
48
48
|
entry_by(id, :id)
|
49
49
|
end
|
50
|
-
|
50
|
+
|
51
51
|
def entry_by_key(key)
|
52
52
|
entry_by(key, :key)
|
53
53
|
end
|
54
|
-
|
54
|
+
|
55
55
|
def entry_by_id_or_key(id_or_key)
|
56
56
|
entry_by(id_or_key, :id, :key)
|
57
57
|
end
|
58
|
-
|
58
|
+
|
59
59
|
def entry_by_hash(attrs)
|
60
60
|
entries.detect{|entry| attrs.all?{|(attr, value)| entry[attr].to_s == value.to_s }} || Entry::NULL
|
61
61
|
end
|
62
|
-
|
62
|
+
|
63
63
|
def [](arg)
|
64
64
|
arg.is_a?(Hash) ? entry_by_hash(arg) : entry_by_id_or_key(arg)
|
65
65
|
end
|
66
|
-
|
66
|
+
|
67
67
|
def values(*args)
|
68
68
|
args = args.empty? ? [:name, :id] : args
|
69
69
|
result = entries.collect{|entry| args.collect{|arg| entry.send(arg) }}
|
70
70
|
(args.length == 1) ? result.flatten : result
|
71
71
|
end
|
72
|
-
|
72
|
+
|
73
73
|
def map_attrs(attrs, *ids_or_keys)
|
74
74
|
if attrs.is_a?(Array)
|
75
|
-
ids_or_keys.empty? ?
|
76
|
-
entries.map{|entry| attrs.map{|attr|entry.send(attr)}} :
|
75
|
+
ids_or_keys.empty? ?
|
76
|
+
entries.map{|entry| attrs.map{|attr|entry.send(attr)}} :
|
77
77
|
ids_or_keys.map do |id_or_key|
|
78
78
|
entry = entry_by_id_or_key(id_or_key)
|
79
79
|
attrs.map{|attr|entry.send(attr)}
|
80
80
|
end
|
81
81
|
else
|
82
82
|
attr = attrs
|
83
|
-
ids_or_keys.empty? ?
|
84
|
-
entries.map(&attr.to_sym) :
|
83
|
+
ids_or_keys.empty? ?
|
84
|
+
entries.map(&attr.to_sym) :
|
85
85
|
ids_or_keys.map{|id_or_key|entry_by_id_or_key(id_or_key).send(attr)}
|
86
86
|
end
|
87
87
|
end
|
88
|
-
|
88
|
+
|
89
89
|
def ids(*ids_or_keys); map_attrs(:id, *ids_or_keys); end
|
90
90
|
def keys(*ids_or_keys); map_attrs(:key, *ids_or_keys); end
|
91
91
|
def names(*ids_or_keys); map_attrs(:name, *ids_or_keys); end
|
@@ -95,13 +95,13 @@ module SelectableAttr
|
|
95
95
|
def id_by_key(key); entry_by_key(key).id; end
|
96
96
|
def name_by_id(id); entry_by_id(id).name; end
|
97
97
|
def name_by_key(key); entry_by_key(key).name; end
|
98
|
-
|
98
|
+
|
99
99
|
def find(options = nil, &block)
|
100
|
-
entries.detect{|entry|
|
101
|
-
block_given? ? yield(entry) : entry.match?(options)
|
100
|
+
entries.detect{|entry|
|
101
|
+
block_given? ? yield(entry) : entry.match?(options)
|
102
102
|
} || Entry::NULL
|
103
103
|
end
|
104
|
-
|
104
|
+
|
105
105
|
def to_hash_array
|
106
106
|
entries.map do |entry|
|
107
107
|
result = entry.to_hash
|
@@ -109,7 +109,7 @@ module SelectableAttr
|
|
109
109
|
result
|
110
110
|
end
|
111
111
|
end
|
112
|
-
|
112
|
+
|
113
113
|
def length
|
114
114
|
entries.length
|
115
115
|
end
|
@@ -127,26 +127,26 @@ module SelectableAttr
|
|
127
127
|
@options = options
|
128
128
|
self.instance_eval(&block) if block
|
129
129
|
end
|
130
|
-
|
130
|
+
|
131
131
|
attr_reader :name
|
132
|
-
|
132
|
+
|
133
133
|
def [](option_key)
|
134
134
|
BASE_ATTRS.include?(option_key) ? send(option_key) :
|
135
135
|
@options ? @options[option_key] : nil
|
136
136
|
end
|
137
|
-
|
137
|
+
|
138
138
|
def match?(options)
|
139
139
|
@options === options
|
140
140
|
end
|
141
|
-
|
141
|
+
|
142
142
|
def null?
|
143
143
|
false
|
144
144
|
end
|
145
|
-
|
145
|
+
|
146
146
|
def null_object?
|
147
147
|
self.null?
|
148
148
|
end
|
149
|
-
|
149
|
+
|
150
150
|
def to_hash
|
151
151
|
(@options || {}).merge(:id => @id, :key => @key, :name => name)
|
152
152
|
end
|
@@ -162,7 +162,7 @@ module SelectableAttr
|
|
162
162
|
def name; nil; end
|
163
163
|
end
|
164
164
|
end
|
165
|
-
|
165
|
+
|
166
166
|
end
|
167
167
|
|
168
168
|
end
|
data/lib/selectable_attr.rb
CHANGED
data/selectable_attr.gemspec
CHANGED
@@ -1,12 +1,15 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
|
1
4
|
# -*- encoding: utf-8 -*-
|
2
5
|
|
3
6
|
Gem::Specification.new do |s|
|
4
7
|
s.name = %q{selectable_attr}
|
5
|
-
s.version = "0.3.
|
8
|
+
s.version = "0.3.12"
|
6
9
|
|
7
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
11
|
s.authors = ["Takeshi Akima"]
|
9
|
-
s.date = %q{
|
12
|
+
s.date = %q{2010-11-06}
|
10
13
|
s.description = %q{selectable_attr generates extra methods dynamically for attribute which has options}
|
11
14
|
s.email = %q{akima@gmail.com}
|
12
15
|
s.extra_rdoc_files = [
|
@@ -34,7 +37,7 @@ Gem::Specification.new do |s|
|
|
34
37
|
s.homepage = %q{http://github.com/akm/selectable_attr/}
|
35
38
|
s.rdoc_options = ["--charset=UTF-8"]
|
36
39
|
s.require_paths = ["lib"]
|
37
|
-
s.rubygems_version = %q{1.3.
|
40
|
+
s.rubygems_version = %q{1.3.6}
|
38
41
|
s.summary = %q{selectable_attr generates extra methods dynamically}
|
39
42
|
s.test_files = [
|
40
43
|
"spec/selectable_attr_base_alias_spec.rb",
|
@@ -3,7 +3,7 @@ require File.join(File.dirname(__FILE__), 'spec_helper')
|
|
3
3
|
require 'stringio'
|
4
4
|
|
5
5
|
describe SelectableAttr do
|
6
|
-
|
6
|
+
|
7
7
|
def assert_enum_class_methods(klass, attr = :enum1)
|
8
8
|
klass.send("#{attr}_enum").length.should == 3
|
9
9
|
expected_hash_array = [
|
@@ -46,27 +46,27 @@ describe SelectableAttr do
|
|
46
46
|
klass.send("#{attr}_name_by_id", 2).should == "エントリ2"
|
47
47
|
klass.send("#{attr}_name_by_id", 3).should == "エントリ3"
|
48
48
|
end
|
49
|
-
|
49
|
+
|
50
50
|
def assert_single_enum_instance_methods(obj, attr = :enum1)
|
51
51
|
obj.send("#{attr}=", 1)
|
52
52
|
obj.send(attr).should == 1
|
53
|
-
obj.enum1_key.should == :entry1
|
53
|
+
obj.enum1_key.should == :entry1
|
54
54
|
obj.enum1_name.should == "エントリ1"
|
55
55
|
obj.enum1_entry.to_hash.should == {:id => 1, :key => :entry1, :name => "エントリ1"}
|
56
|
-
|
56
|
+
|
57
57
|
obj.enum1_key = :entry2
|
58
58
|
obj.send(attr).should == 2
|
59
59
|
obj.enum1_key.should == :entry2
|
60
60
|
obj.enum1_name.should == "エントリ2"
|
61
61
|
obj.enum1_entry.to_hash.should == {:id => 2, :key => :entry2, :name => "エントリ2"}
|
62
|
-
|
62
|
+
|
63
63
|
obj.send("#{attr}=", 3)
|
64
64
|
obj.send(attr).should == 3
|
65
65
|
obj.enum1_key.should == :entry3
|
66
66
|
obj.enum1_name.should == "エントリ3"
|
67
67
|
obj.enum1_entry.to_hash.should == {:id => 3, :key => :entry3, :name => "エントリ3"}
|
68
68
|
end
|
69
|
-
|
69
|
+
|
70
70
|
class EnumBase
|
71
71
|
include ::SelectableAttr::Base
|
72
72
|
end
|
@@ -103,7 +103,7 @@ describe SelectableAttr do
|
|
103
103
|
EnumMock1WithEnum.selectable_attr_type_for(:enum1).should == :single
|
104
104
|
end
|
105
105
|
end
|
106
|
-
|
106
|
+
|
107
107
|
|
108
108
|
describe "attr_enumeable_base" do
|
109
109
|
class EnumMock2 < EnumBase
|
@@ -138,7 +138,7 @@ describe SelectableAttr do
|
|
138
138
|
end
|
139
139
|
end
|
140
140
|
|
141
|
-
|
141
|
+
|
142
142
|
def assert_multi_enum_instance_methods(obj, patterns)
|
143
143
|
obj.enum_array1_hash_array.should == [
|
144
144
|
{:id => 1, :key => :entry1, :name => "エントリ1", :select => false},
|
@@ -150,7 +150,7 @@ describe SelectableAttr do
|
|
150
150
|
obj.enum_array1_entries.should == []
|
151
151
|
obj.enum_array1_keys.should == []
|
152
152
|
obj.enum_array1_names.should == []
|
153
|
-
|
153
|
+
|
154
154
|
obj.enum_array1 = patterns[0]
|
155
155
|
obj.enum_array1.should == patterns[0]
|
156
156
|
obj.enum_array1_hash_array.should == [
|
@@ -174,7 +174,7 @@ describe SelectableAttr do
|
|
174
174
|
obj.enum_array1_entries.map(&:id).should == [3]
|
175
175
|
obj.enum_array1_keys.should == [:entry3]
|
176
176
|
obj.enum_array1_names.should == ['エントリ3']
|
177
|
-
|
177
|
+
|
178
178
|
obj.enum_array1 = patterns[3]
|
179
179
|
obj.enum_array1.should == patterns[3]
|
180
180
|
obj.enum_array1_hash_array.should == [
|
@@ -199,7 +199,7 @@ describe SelectableAttr do
|
|
199
199
|
obj.enum_array1_entries.map(&:id).should == [1, 2, 3]
|
200
200
|
obj.enum_array1_keys.should == [:entry1, :entry2, :entry3]
|
201
201
|
obj.enum_array1_names.should == ['エントリ1', 'エントリ2', 'エントリ3']
|
202
|
-
|
202
|
+
|
203
203
|
obj.enum_array1_ids = [1,3]; obj.enum_array1.should == patterns[5]
|
204
204
|
obj.enum_array1_ids = [1,2]; obj.enum_array1.should == patterns[6]
|
205
205
|
obj.enum_array1_ids = [2]; obj.enum_array1.should == patterns[2]
|
@@ -216,7 +216,7 @@ describe SelectableAttr do
|
|
216
216
|
obj.enum_array1_ids = "1,2"; obj.enum_array1.should == patterns[6]
|
217
217
|
obj.enum_array1_ids = "2"; obj.enum_array1.should == patterns[2]
|
218
218
|
end
|
219
|
-
|
219
|
+
|
220
220
|
describe ":convert_with => :binary_string" do
|
221
221
|
class EnumMock3 < EnumBase
|
222
222
|
multi_selectable_attr :enum_array1, :convert_with => :binary_string do
|
@@ -235,7 +235,7 @@ describe SelectableAttr do
|
|
235
235
|
end
|
236
236
|
|
237
237
|
it "test_multi_selectable_attr_with_binary_string" do
|
238
|
-
expected = (0..7).map{|i| '
|
238
|
+
expected = (0..7).map{|i| '%03b' % i} # ["000", "001", "010", "011", "100", "101", "110", "111"]
|
239
239
|
assert_enum_class_methods(EnumMock3, :enum_array1)
|
240
240
|
assert_multi_enum_instance_methods(EnumMock3.new, expected)
|
241
241
|
assert_enum_class_methods(EnumMock3WithEnumArray, :enum_array1)
|
@@ -263,7 +263,7 @@ describe SelectableAttr do
|
|
263
263
|
|
264
264
|
it "test_multi_selectable_attr2" do
|
265
265
|
# [[], [3], [2], [2, 3], [1], [1, 3], [1, 2], [1, 2, 3]]
|
266
|
-
expected =
|
266
|
+
expected =
|
267
267
|
(0..7).map do |i|
|
268
268
|
s = '%03b' % i
|
269
269
|
a = s.split('').map{|v| v.to_i}
|
@@ -297,7 +297,7 @@ describe SelectableAttr do
|
|
297
297
|
|
298
298
|
it "test_multi_selectable_attr_with_comma_string" do
|
299
299
|
# ["", "3", "2", "2,3", "1", "1,3", "1,2", "1,2,3"]
|
300
|
-
expected =
|
300
|
+
expected =
|
301
301
|
(0..7).map do |i|
|
302
302
|
s = '%03b' % i
|
303
303
|
a = s.split('').map{|v| v.to_i}
|
@@ -311,11 +311,11 @@ describe SelectableAttr do
|
|
311
311
|
assert_multi_enum_instance_methods(EnumMock5WithEnumArray.new, expected)
|
312
312
|
end
|
313
313
|
end
|
314
|
-
|
314
|
+
|
315
315
|
describe "selectable_attr_name_pattern" do
|
316
316
|
class EnumMock6 < EnumBase
|
317
317
|
# self.selectable_attr_name_pattern = /(_cd$|_code$|_cds$|_codes$)/
|
318
|
-
selectable_attr :category_id do
|
318
|
+
selectable_attr :category_id do
|
319
319
|
entry "01", :category1, "カテゴリ1"
|
320
320
|
entry "02", :category2, "カテゴリ2"
|
321
321
|
end
|
@@ -323,7 +323,7 @@ describe SelectableAttr do
|
|
323
323
|
|
324
324
|
class EnumMock7 < EnumBase
|
325
325
|
self.selectable_attr_name_pattern = /(_cd$|_id$|_cds$|_ids$)/
|
326
|
-
selectable_attr :category_id do
|
326
|
+
selectable_attr :category_id do
|
327
327
|
entry "01", :category1, "カテゴリ1"
|
328
328
|
entry "02", :category2, "カテゴリ2"
|
329
329
|
end
|
@@ -353,14 +353,14 @@ describe SelectableAttr do
|
|
353
353
|
def table_exists?; end
|
354
354
|
end
|
355
355
|
end
|
356
|
-
|
356
|
+
|
357
357
|
it "should return false if column does exist" do
|
358
358
|
ConnectableMock1.should_receive(:connected?).and_return(true)
|
359
359
|
ConnectableMock1.should_receive(:table_exists?).and_return(true)
|
360
360
|
ConnectableMock1.should_receive(:columns).and_return([mock(:column1, :name => :column1)])
|
361
361
|
ConnectableMock1.has_attr(:column1).should == true
|
362
362
|
end
|
363
|
-
|
363
|
+
|
364
364
|
it "should return false if column doesn't exist" do
|
365
365
|
ConnectableMock1.should_receive(:connected?).and_return(true)
|
366
366
|
ConnectableMock1.should_receive(:table_exists?).and_return(true)
|
@@ -389,12 +389,12 @@ describe SelectableAttr do
|
|
389
389
|
entry 3, :entry3, "エントリ3"
|
390
390
|
end
|
391
391
|
end
|
392
|
-
|
392
|
+
|
393
393
|
it "return constant by Symbol access" do
|
394
394
|
enum1 = EnumMock10.enum_for(:enum1)
|
395
395
|
enum1.class.should == SelectableAttr::Enum
|
396
396
|
end
|
397
|
-
|
397
|
+
|
398
398
|
it "return constant by String access" do
|
399
399
|
enum1 = EnumMock10.enum_for('enum1')
|
400
400
|
enum1.class.should == SelectableAttr::Enum
|
@@ -413,7 +413,7 @@ describe SelectableAttr do
|
|
413
413
|
def attr_accessor_with_default(*args); end
|
414
414
|
end
|
415
415
|
end
|
416
|
-
|
416
|
+
|
417
417
|
it "should call attr_accessor_with_default when both of attr_accessor and default are given" do
|
418
418
|
DefiningMock1.should_receive(:attr_accessor_with_default).with(:enum1, 1)
|
419
419
|
DefiningMock1.define_accessor(:attr => :enum1, :attr_accessor => true, :default => 1)
|
@@ -23,7 +23,7 @@ describe SelectableAttr::Enum do
|
|
23
23
|
Enum1[2].name.should == 'DVD'
|
24
24
|
Enum1[3].name.should == 'CD'
|
25
25
|
Enum1[4].name.should == 'VHS'
|
26
|
-
|
26
|
+
|
27
27
|
Enum1[:book].id.should == 1
|
28
28
|
Enum1[:dvd ].id.should == 2
|
29
29
|
Enum1[:cd ].id.should == 3
|
@@ -36,7 +36,7 @@ describe SelectableAttr::Enum do
|
|
36
36
|
Enum1[:dvd].name.should == 'DVD'
|
37
37
|
Enum1[:cd].name.should == 'CD'
|
38
38
|
Enum1[:vhs].name.should == 'VHS'
|
39
|
-
|
39
|
+
|
40
40
|
Enum1.values.should == [['書籍', 1], ['DVD', 2], ['CD', 3], ['VHS', 4]]
|
41
41
|
Enum1.values(:name, :id).should == [['書籍', 1], ['DVD', 2], ['CD', 3], ['VHS', 4]]
|
42
42
|
Enum1.values(:name, :key).should == [['書籍', :book], ['DVD', :dvd], ['CD', :cd], ['VHS', :vhs]]
|
@@ -55,8 +55,8 @@ describe SelectableAttr::Enum do
|
|
55
55
|
InetAccess[1].key.should == :email
|
56
56
|
InetAccess[2].key.should == :website
|
57
57
|
InetAccess[3].key.should == :ftp
|
58
|
-
|
59
|
-
|
58
|
+
|
59
|
+
|
60
60
|
InetAccess[1].name.should == 'Eメール'
|
61
61
|
InetAccess[2].name.should == 'ウェブサイト'
|
62
62
|
InetAccess[3].name.should == 'FTP'
|
@@ -76,7 +76,7 @@ describe SelectableAttr::Enum do
|
|
76
76
|
InetAccess[:protocol => 'http://'].should == InetAccess[2]
|
77
77
|
InetAccess[:protocol => 'ftp://'].should == InetAccess[3]
|
78
78
|
end
|
79
|
-
|
79
|
+
|
80
80
|
it "test_null?" do
|
81
81
|
InetAccess[1].null?.should == false
|
82
82
|
InetAccess[2].null?.should == false
|
@@ -87,7 +87,7 @@ describe SelectableAttr::Enum do
|
|
87
87
|
InetAccess[:protocol => 'ftp://'].null?.should == false
|
88
88
|
InetAccess[:protocol => 'svn://'].null?.should == true
|
89
89
|
end
|
90
|
-
|
90
|
+
|
91
91
|
it "test_null_object?" do
|
92
92
|
InetAccess[1].null_object?.should == false
|
93
93
|
InetAccess[2].null_object?.should == false
|
@@ -98,7 +98,7 @@ describe SelectableAttr::Enum do
|
|
98
98
|
InetAccess[:protocol => 'ftp://'].null_object?.should == false
|
99
99
|
InetAccess[:protocol => 'svn://'].null_object?.should == true
|
100
100
|
end
|
101
|
-
|
101
|
+
|
102
102
|
it "test_to_hash_array" do
|
103
103
|
Enum1.to_hash_array.should == [
|
104
104
|
{:id => 1, :key => :book, :name => '書籍'},
|
@@ -106,7 +106,7 @@ describe SelectableAttr::Enum do
|
|
106
106
|
{:id => 3, :key => :cd, :name => 'CD'},
|
107
107
|
{:id => 4, :key => :vhs, :name => 'VHS'}
|
108
108
|
]
|
109
|
-
|
109
|
+
|
110
110
|
InetAccess.to_hash_array.should == [
|
111
111
|
{:id => 1, :key => :email, :name => 'Eメール', :protocol => 'mailto:'},
|
112
112
|
{:id => 2, :key => :website, :name => 'ウェブサイト', :protocol => 'http://'},
|
data/spec/spec_helper.rb
CHANGED
@@ -1,11 +1,28 @@
|
|
1
|
-
$KCODE='u'
|
1
|
+
$KCODE='u' if RUBY_VERSION =~ /^1\.8/
|
2
|
+
|
3
|
+
require 'rspec/core'
|
4
|
+
require 'autotest/rspec2'
|
5
|
+
|
6
|
+
Dir['./spec/support/**/*.rb'].map {|f| require f}
|
2
7
|
|
3
8
|
$LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'lib')
|
4
9
|
require File.join(File.dirname(__FILE__), '..', 'init')
|
5
10
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
11
|
+
# see http://d.hatena.ne.jp/nedate/20101004/1286183882
|
12
|
+
#
|
13
|
+
# quick monkey patch for rcov
|
14
|
+
#
|
15
|
+
# http://codefluency.com/post/1023734493/a-bandaid-for-rcov-on-ruby-1-9
|
16
|
+
#
|
17
|
+
if defined?(Rcov)
|
18
|
+
class Rcov::CodeCoverageAnalyzer
|
19
|
+
def update_script_lines__
|
20
|
+
if '1.9'.respond_to?(:force_encoding)
|
21
|
+
SCRIPT_LINES__.each do |k,v|
|
22
|
+
v.each { |src| src.force_encoding('utf-8') }
|
23
|
+
end
|
24
|
+
end
|
25
|
+
@script_lines__ = @script_lines__.merge(SCRIPT_LINES__)
|
26
|
+
end
|
10
27
|
end
|
11
28
|
end
|
metadata
CHANGED
@@ -1,7 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: selectable_attr
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
hash: 11
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 3
|
9
|
+
- 12
|
10
|
+
version: 0.3.12
|
5
11
|
platform: ruby
|
6
12
|
authors:
|
7
13
|
- Takeshi Akima
|
@@ -9,7 +15,7 @@ autorequire:
|
|
9
15
|
bindir: bin
|
10
16
|
cert_chain: []
|
11
17
|
|
12
|
-
date: 2010-
|
18
|
+
date: 2010-11-06 00:00:00 +09:00
|
13
19
|
default_executable:
|
14
20
|
dependencies: []
|
15
21
|
|
@@ -49,21 +55,27 @@ rdoc_options:
|
|
49
55
|
require_paths:
|
50
56
|
- lib
|
51
57
|
required_ruby_version: !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
52
59
|
requirements:
|
53
60
|
- - ">="
|
54
61
|
- !ruby/object:Gem::Version
|
62
|
+
hash: 3
|
63
|
+
segments:
|
64
|
+
- 0
|
55
65
|
version: "0"
|
56
|
-
version:
|
57
66
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
67
|
+
none: false
|
58
68
|
requirements:
|
59
69
|
- - ">="
|
60
70
|
- !ruby/object:Gem::Version
|
71
|
+
hash: 3
|
72
|
+
segments:
|
73
|
+
- 0
|
61
74
|
version: "0"
|
62
|
-
version:
|
63
75
|
requirements: []
|
64
76
|
|
65
77
|
rubyforge_project:
|
66
|
-
rubygems_version: 1.3.
|
78
|
+
rubygems_version: 1.3.7
|
67
79
|
signing_key:
|
68
80
|
specification_version: 3
|
69
81
|
summary: selectable_attr generates extra methods dynamically
|