ar_serialized_array 0.1.2 → 0.2.4
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +3 -0
- data/CHANGELOG +2 -0
- data/README.markdown +57 -0
- data/Rakefile +68 -5
- data/VERSION +1 -1
- data/cache/file_data_provider/countries-AF.txt +246 -0
- data/cache/file_data_provider/countries-AM.txt +246 -0
- data/cache/file_data_provider/countries-AR.txt +246 -0
- data/cache/file_data_provider/countries-AZ.txt +246 -0
- data/cache/file_data_provider/countries-BE.txt +246 -0
- data/cache/file_data_provider/countries-BG.txt +246 -0
- data/cache/file_data_provider/countries-BN.txt +246 -0
- data/cache/file_data_provider/countries-BR.txt +246 -0
- data/cache/file_data_provider/countries-BS.txt +246 -0
- data/cache/file_data_provider/countries-CA.txt +246 -0
- data/cache/file_data_provider/countries-CS.txt +246 -0
- data/cache/file_data_provider/countries-CY.txt +246 -0
- data/cache/file_data_provider/countries-DA.txt +246 -0
- data/cache/file_data_provider/countries-DE.txt +246 -0
- data/cache/file_data_provider/countries-DZ.txt +246 -0
- data/cache/file_data_provider/countries-EL.txt +246 -0
- data/cache/file_data_provider/countries-EN.txt +246 -0
- data/cache/file_data_provider/countries-EO.txt +246 -0
- data/cache/file_data_provider/countries-ES.txt +246 -0
- data/cache/file_data_provider/countries-ET.txt +246 -0
- data/cache/file_data_provider/countries-EU.txt +246 -0
- data/cache/file_data_provider/countries-FA.txt +246 -0
- data/cache/file_data_provider/countries-FI.txt +246 -0
- data/cache/file_data_provider/countries-FO.txt +246 -0
- data/cache/file_data_provider/countries-FR.txt +246 -0
- data/cache/file_data_provider/countries-GA.txt +246 -0
- data/cache/file_data_provider/countries-GL.txt +246 -0
- data/cache/file_data_provider/countries-GU.txt +246 -0
- data/cache/file_data_provider/countries-HE.txt +246 -0
- data/cache/file_data_provider/countries-HI.txt +246 -0
- data/cache/file_data_provider/countries-HR.txt +246 -0
- data/cache/file_data_provider/countries-HU.txt +246 -0
- data/cache/file_data_provider/countries-HY.txt +246 -0
- data/cache/file_data_provider/countries-ID.txt +246 -0
- data/cache/file_data_provider/countries-IS.txt +246 -0
- data/cache/file_data_provider/countries-IT.txt +246 -0
- data/cache/file_data_provider/countries-JA.txt +246 -0
- data/cache/file_data_provider/countries-KA.txt +246 -0
- data/cache/file_data_provider/countries-KK.txt +246 -0
- data/cache/file_data_provider/countries-KM.txt +246 -0
- data/cache/file_data_provider/countries-KN.txt +246 -0
- data/cache/file_data_provider/countries-KO.txt +246 -0
- data/cache/file_data_provider/countries-KU.txt +246 -0
- data/cache/file_data_provider/countries-LT.txt +246 -0
- data/cache/file_data_provider/countries-LV.txt +246 -0
- data/cache/file_data_provider/countries-MI.txt +246 -0
- data/cache/file_data_provider/countries-MK.txt +246 -0
- data/cache/file_data_provider/countries-ML.txt +246 -0
- data/cache/file_data_provider/countries-MN.txt +246 -0
- data/cache/file_data_provider/countries-MR.txt +246 -0
- data/cache/file_data_provider/countries-MS.txt +246 -0
- data/cache/file_data_provider/countries-MT.txt +246 -0
- data/cache/file_data_provider/countries-NB.txt +246 -0
- data/cache/file_data_provider/countries-NE.txt +246 -0
- data/cache/file_data_provider/countries-NL.txt +246 -0
- data/cache/file_data_provider/countries-NN.txt +246 -0
- data/cache/file_data_provider/countries-OC.txt +246 -0
- data/cache/file_data_provider/countries-PA.txt +246 -0
- data/cache/file_data_provider/countries-PL.txt +246 -0
- data/cache/file_data_provider/countries-PS.txt +246 -0
- data/cache/file_data_provider/countries-PT.txt +246 -0
- data/cache/file_data_provider/countries-RO.txt +246 -0
- data/cache/file_data_provider/countries-RU.txt +246 -0
- data/cache/file_data_provider/countries-RW.txt +246 -0
- data/cache/file_data_provider/countries-SK.txt +246 -0
- data/cache/file_data_provider/countries-SL.txt +246 -0
- data/cache/file_data_provider/countries-SO.txt +246 -0
- data/cache/file_data_provider/countries-SQ.txt +246 -0
- data/cache/file_data_provider/countries-SR.txt +246 -0
- data/cache/file_data_provider/countries-SV.txt +246 -0
- data/cache/file_data_provider/countries-SW.txt +246 -0
- data/cache/file_data_provider/countries-TA.txt +246 -0
- data/cache/file_data_provider/countries-TH.txt +246 -0
- data/cache/file_data_provider/countries-TI.txt +246 -0
- data/cache/file_data_provider/countries-TK.txt +246 -0
- data/cache/file_data_provider/countries-TL.txt +246 -0
- data/cache/file_data_provider/countries-TR.txt +246 -0
- data/cache/file_data_provider/countries-TT.txt +246 -0
- data/cache/file_data_provider/countries-UK.txt +246 -0
- data/cache/file_data_provider/countries-VE.txt +246 -0
- data/cache/file_data_provider/countries-VI.txt +246 -0
- data/cache/file_data_provider/countries-WA.txt +246 -0
- data/cache/file_data_provider/countries-WO.txt +246 -0
- data/cache/file_data_provider/countries-XH.txt +246 -0
- data/cache/file_data_provider/countries-ZU.txt +246 -0
- data/cache/file_data_provider/languages-AF.txt +185 -0
- data/cache/file_data_provider/languages-AM.txt +185 -0
- data/cache/file_data_provider/languages-AR.txt +185 -0
- data/cache/file_data_provider/languages-AZ.txt +185 -0
- data/cache/file_data_provider/languages-BE.txt +185 -0
- data/cache/file_data_provider/languages-BG.txt +185 -0
- data/cache/file_data_provider/languages-BR.txt +185 -0
- data/cache/file_data_provider/languages-BS.txt +185 -0
- data/cache/file_data_provider/languages-CA.txt +185 -0
- data/cache/file_data_provider/languages-CS.txt +185 -0
- data/cache/file_data_provider/languages-CY.txt +185 -0
- data/cache/file_data_provider/languages-DA.txt +185 -0
- data/cache/file_data_provider/languages-DE.txt +185 -0
- data/cache/file_data_provider/languages-EL.txt +185 -0
- data/cache/file_data_provider/languages-EN.txt +185 -0
- data/cache/file_data_provider/languages-EO.txt +185 -0
- data/cache/file_data_provider/languages-ES.txt +185 -0
- data/cache/file_data_provider/languages-ET.txt +185 -0
- data/cache/file_data_provider/languages-EU.txt +185 -0
- data/cache/file_data_provider/languages-FA.txt +185 -0
- data/cache/file_data_provider/languages-FI.txt +185 -0
- data/cache/file_data_provider/languages-FR.txt +185 -0
- data/cache/file_data_provider/languages-GA.txt +185 -0
- data/cache/file_data_provider/languages-GL.txt +185 -0
- data/cache/file_data_provider/languages-HE.txt +185 -0
- data/cache/file_data_provider/languages-HI.txt +185 -0
- data/cache/file_data_provider/languages-HR.txt +185 -0
- data/cache/file_data_provider/languages-HU.txt +185 -0
- data/cache/file_data_provider/languages-ID.txt +185 -0
- data/cache/file_data_provider/languages-IS.txt +185 -0
- data/cache/file_data_provider/languages-IT.txt +185 -0
- data/cache/file_data_provider/languages-JA.txt +185 -0
- data/cache/file_data_provider/languages-KO.txt +185 -0
- data/cache/file_data_provider/languages-LT.txt +185 -0
- data/cache/file_data_provider/languages-LV.txt +185 -0
- data/cache/file_data_provider/languages-MI.txt +185 -0
- data/cache/file_data_provider/languages-MK.txt +185 -0
- data/cache/file_data_provider/languages-MN.txt +185 -0
- data/cache/file_data_provider/languages-MR.txt +185 -0
- data/cache/file_data_provider/languages-MS.txt +185 -0
- data/cache/file_data_provider/languages-MT.txt +185 -0
- data/cache/file_data_provider/languages-NB.txt +185 -0
- data/cache/file_data_provider/languages-NL.txt +185 -0
- data/cache/file_data_provider/languages-NN.txt +185 -0
- data/cache/file_data_provider/languages-OC.txt +185 -0
- data/cache/file_data_provider/languages-PL.txt +185 -0
- data/cache/file_data_provider/languages-PS.txt +185 -0
- data/cache/file_data_provider/languages-PT.txt +185 -0
- data/cache/file_data_provider/languages-RO.txt +185 -0
- data/cache/file_data_provider/languages-RU.txt +185 -0
- data/cache/file_data_provider/languages-RW.txt +185 -0
- data/cache/file_data_provider/languages-SK.txt +185 -0
- data/cache/file_data_provider/languages-SL.txt +185 -0
- data/cache/file_data_provider/languages-SR.txt +185 -0
- data/cache/file_data_provider/languages-SV.txt +185 -0
- data/cache/file_data_provider/languages-TA.txt +185 -0
- data/cache/file_data_provider/languages-TH.txt +185 -0
- data/cache/file_data_provider/languages-TI.txt +185 -0
- data/cache/file_data_provider/languages-TR.txt +185 -0
- data/cache/file_data_provider/languages-TT.txt +185 -0
- data/cache/file_data_provider/languages-UK.txt +185 -0
- data/cache/file_data_provider/languages-VE.txt +185 -0
- data/cache/file_data_provider/languages-VI.txt +185 -0
- data/cache/file_data_provider/languages-WA.txt +185 -0
- data/cache/file_data_provider/languages-XH.txt +185 -0
- data/cache/file_data_provider/languages-ZU.txt +185 -0
- data/example_output/all_names_for_DE.txt +85 -0
- data/example_output/all_names_for_FR.txt +85 -0
- data/example_output/all_names_for_GB.txt +85 -0
- data/example_output/all_names_for_US.txt +85 -0
- data/i18n_data.gemspec +31 -0
- data/lib/i18n_data/file_data_provider.rb +56 -0
- data/lib/i18n_data/live_data_provider.rb +97 -0
- data/lib/i18n_data.rb +34 -0
- data/spec/i18n_data/file_data_provider_spec.rb +23 -0
- data/spec/i18n_data/live_data_provider_spec.rb +44 -0
- data/spec/i18n_data_spec.rb +101 -0
- data/spec/spec_helper.rb +2 -4
- metadata +183 -39
- data/Readme.md +0 -50
- data/ar_serialized_array.gemspec +0 -47
- data/init.rb +0 -1
- data/lib/ar_serialized_array.rb +0 -37
- data/spec/ar_serialized_array_spec.rb +0 -84
- data/spec/setup_test_model.rb +0 -28
data/lib/ar_serialized_array.rb
DELETED
@@ -1,37 +0,0 @@
|
|
1
|
-
require 'active_record'
|
2
|
-
|
3
|
-
module ARSerializedArray
|
4
|
-
VERSION = File.read( File.join(File.dirname(__FILE__),'..','VERSION') ).strip
|
5
|
-
|
6
|
-
def serialized_array(attr_name, options={})
|
7
|
-
options[:on_set] ||= lambda{|x|x}
|
8
|
-
|
9
|
-
# getter
|
10
|
-
define_method attr_name do
|
11
|
-
YAML.load(read_attribute(attr_name).to_s) || []
|
12
|
-
end
|
13
|
-
|
14
|
-
# setter
|
15
|
-
define_method "#{attr_name}=" do |value|
|
16
|
-
value = options[:on_set].call(value) || []
|
17
|
-
raise "Expected an Array, got: #{value.inspect}" unless value.is_a? Array
|
18
|
-
value = (value.empty? ? nil : value.to_yaml)
|
19
|
-
write_attribute attr_name, value
|
20
|
-
end
|
21
|
-
|
22
|
-
# accessors e.g. xxx_as_text
|
23
|
-
if options[:accessor]
|
24
|
-
#getter
|
25
|
-
define_method options[:accessor] do
|
26
|
-
send(attr_name) * ", "
|
27
|
-
end
|
28
|
-
|
29
|
-
#setter
|
30
|
-
define_method "#{options[:accessor]}=" do |value|
|
31
|
-
send "#{attr_name}=", value.to_s.split(',').reject(&:blank?).map(&:strip)
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
ActiveRecord::Base.send(:extend, ARSerializedArray)
|
@@ -1,84 +0,0 @@
|
|
1
|
-
require "spec/spec_helper"
|
2
|
-
|
3
|
-
describe 'ar_serialized_array' do
|
4
|
-
describe 'basics' do
|
5
|
-
it "is an empty array by default" do
|
6
|
-
UserPlain.new.product_ids.should == []
|
7
|
-
end
|
8
|
-
|
9
|
-
it "serializes on set" do
|
10
|
-
user = UserPlain.create!(:product_ids => [1, 2])
|
11
|
-
UserPlain.find(user.id).product_ids.should == [1, 2]
|
12
|
-
end
|
13
|
-
|
14
|
-
it "can save an empty array" do
|
15
|
-
user = UserPlain.create!(:product_ids => [])
|
16
|
-
UserPlain.find(user.id).product_ids.should == []
|
17
|
-
end
|
18
|
-
|
19
|
-
it "can save nil" do
|
20
|
-
user = UserPlain.create!(:product_ids => nil)
|
21
|
-
UserPlain.find(user.id).product_ids.should == []
|
22
|
-
end
|
23
|
-
|
24
|
-
it "stores nil when nil is saved" do
|
25
|
-
user = UserPlain.create!(:product_ids => nil)
|
26
|
-
UserPlain.first(:conditions => {:id => user.id, :product_ids => nil}).should == user
|
27
|
-
end
|
28
|
-
|
29
|
-
it "stores empty arrays as NULL" do
|
30
|
-
user = UserPlain.create!(:product_ids => [])
|
31
|
-
UserPlain.first(:conditions => {:id => user.id, :product_ids => nil}).should == user
|
32
|
-
end
|
33
|
-
|
34
|
-
it "can search by array" do
|
35
|
-
user = UserPlain.create!(:product_ids => [1])
|
36
|
-
UserPlain.first(:conditions => {:id => user.id, :product_ids => [1].to_yaml}).should == user
|
37
|
-
end
|
38
|
-
|
39
|
-
it "raises when non-array is given" do
|
40
|
-
lambda{ UserPlain.create!(:product_ids => '') }.should raise_error
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
describe 'with accessor' do
|
45
|
-
it "can access the array as text" do
|
46
|
-
UserWithAccessor.new(:product_ids => [1,2,3]).product_ids_as_text.should == '1, 2, 3'
|
47
|
-
end
|
48
|
-
|
49
|
-
it "can set arrays via text" do
|
50
|
-
UserWithAccessor.new(:product_ids_as_text => '1, 2, 3').product_ids.should == ['1','2','3']
|
51
|
-
end
|
52
|
-
|
53
|
-
it "is symetric" do
|
54
|
-
text = '1, 2, 3'
|
55
|
-
UserWithAccessor.new(:product_ids_as_text => text).product_ids_as_text.should == text
|
56
|
-
end
|
57
|
-
|
58
|
-
it "accepts nil as empty" do
|
59
|
-
UserWithAccessor.new(:product_ids_as_text => nil).product_ids.should == []
|
60
|
-
end
|
61
|
-
|
62
|
-
it "is blank when unset" do
|
63
|
-
UserWithAccessor.new.product_ids_as_text.should == ''
|
64
|
-
end
|
65
|
-
|
66
|
-
it "is blank when empty" do
|
67
|
-
UserWithAccessor.new(:product_ids => []).product_ids_as_text.should == ''
|
68
|
-
end
|
69
|
-
end
|
70
|
-
|
71
|
-
describe 'with on_set' do
|
72
|
-
it "is used when storing normally" do
|
73
|
-
User.new(:product_ids => ['1','2','2']).product_ids.should == [1,2]
|
74
|
-
end
|
75
|
-
|
76
|
-
it "is used when storing through accessor" do
|
77
|
-
User.new(:product_ids_as_text => '1,2,2').product_ids.should == [1,2]
|
78
|
-
end
|
79
|
-
end
|
80
|
-
|
81
|
-
it "has a VERSION" do
|
82
|
-
ARSerializedArray::VERSION.should =~ /^\d+\.\d+\.\d+$/
|
83
|
-
end
|
84
|
-
end
|
data/spec/setup_test_model.rb
DELETED
@@ -1,28 +0,0 @@
|
|
1
|
-
# connect
|
2
|
-
ActiveRecord::Base.establish_connection(
|
3
|
-
:adapter => "sqlite3",
|
4
|
-
:database => ":memory:"
|
5
|
-
)
|
6
|
-
|
7
|
-
# create tables
|
8
|
-
ActiveRecord::Schema.define(:version => 1) do
|
9
|
-
create_table :users do |t|
|
10
|
-
t.string :product_ids
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
# create models
|
15
|
-
class User < ActiveRecord::Base
|
16
|
-
serialized_array :product_ids, :accessor => :product_ids_as_text, :on_set=>lambda{|x| x.map(&:to_i).uniq}
|
17
|
-
end
|
18
|
-
|
19
|
-
class UserWithAccessor < ActiveRecord::Base
|
20
|
-
self.table_name = 'users'
|
21
|
-
serialized_array :product_ids, :accessor => :product_ids_as_text
|
22
|
-
end
|
23
|
-
|
24
|
-
|
25
|
-
class UserPlain < ActiveRecord::Base
|
26
|
-
self.table_name = 'users'
|
27
|
-
serialized_array :product_ids
|
28
|
-
end
|