attr-csv 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.document +5 -0
- data/.rspec +1 -0
- data/Gemfile +13 -0
- data/Gemfile.lock +88 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +62 -0
- data/Rakefile +49 -0
- data/VERSION +1 -0
- data/lib/attr-csv.rb +121 -0
- data/lib/attr-csv/active-record.rb +32 -0
- data/spec/attr-csv_spec.rb +28 -0
- data/spec/rails_extensions_spec.rb +119 -0
- data/spec/spec_helper.rb +12 -0
- data/spec/support/csv_model.rb +4 -0
- data/spec/support/csv_poro.rb +5 -0
- metadata +144 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b1f159e6e579fc4b24f60aff98f8d0eb34588ae0
|
4
|
+
data.tar.gz: 71ddd3e47db2d9cb6f9e7c7dfb443f965d52fc12
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5a6a8914e085489a3b032032650b1686621a2ac353b1721af59890bb789cd280f30826518e115a05034fd2f0d04ed6b66ca0fd81b2c39b9c845fdd941f8807f1
|
7
|
+
data.tar.gz: 1c4acfafcf6082d89a93e6b4ad2235e9e74137fff31604de9283979b5d81faee5a66f5ca38eda2633c4c93019cd48a45700961cfff2a84d0bd52654746baadff
|
data/.document
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Gemfile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
|
3
|
+
gem "activerecord", ">= 3.0.0"
|
4
|
+
|
5
|
+
# Add dependencies to develop your gem here.
|
6
|
+
# Include everything needed to run rake, tests, features, etc.
|
7
|
+
group :development do
|
8
|
+
gem "rspec", "~> 2.12.0"
|
9
|
+
gem "rdoc", "~> 3.12"
|
10
|
+
gem "bundler", "~> 1.0"
|
11
|
+
gem "jeweler", "~> 1.8.7"
|
12
|
+
gem "sqlite3" # This is used for the spec tests
|
13
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
activemodel (4.0.1)
|
5
|
+
activesupport (= 4.0.1)
|
6
|
+
builder (~> 3.1.0)
|
7
|
+
activerecord (4.0.1)
|
8
|
+
activemodel (= 4.0.1)
|
9
|
+
activerecord-deprecated_finders (~> 1.0.2)
|
10
|
+
activesupport (= 4.0.1)
|
11
|
+
arel (~> 4.0.0)
|
12
|
+
activerecord-deprecated_finders (1.0.3)
|
13
|
+
activesupport (4.0.1)
|
14
|
+
i18n (~> 0.6, >= 0.6.4)
|
15
|
+
minitest (~> 4.2)
|
16
|
+
multi_json (~> 1.3)
|
17
|
+
thread_safe (~> 0.1)
|
18
|
+
tzinfo (~> 0.3.37)
|
19
|
+
addressable (2.3.5)
|
20
|
+
arel (4.0.1)
|
21
|
+
atomic (1.1.14)
|
22
|
+
builder (3.1.4)
|
23
|
+
diff-lcs (1.1.3)
|
24
|
+
faraday (0.8.8)
|
25
|
+
multipart-post (~> 1.2.0)
|
26
|
+
git (1.2.6)
|
27
|
+
github_api (0.10.1)
|
28
|
+
addressable
|
29
|
+
faraday (~> 0.8.1)
|
30
|
+
hashie (>= 1.2)
|
31
|
+
multi_json (~> 1.4)
|
32
|
+
nokogiri (~> 1.5.2)
|
33
|
+
oauth2
|
34
|
+
hashie (2.0.5)
|
35
|
+
highline (1.6.20)
|
36
|
+
httpauth (0.2.0)
|
37
|
+
i18n (0.6.5)
|
38
|
+
jeweler (1.8.8)
|
39
|
+
builder
|
40
|
+
bundler (~> 1.0)
|
41
|
+
git (>= 1.2.5)
|
42
|
+
github_api (= 0.10.1)
|
43
|
+
highline (>= 1.6.15)
|
44
|
+
nokogiri (= 1.5.10)
|
45
|
+
rake
|
46
|
+
rdoc
|
47
|
+
json (1.8.1)
|
48
|
+
jwt (0.1.8)
|
49
|
+
multi_json (>= 1.5)
|
50
|
+
minitest (4.7.5)
|
51
|
+
multi_json (1.8.2)
|
52
|
+
multi_xml (0.5.5)
|
53
|
+
multipart-post (1.2.0)
|
54
|
+
nokogiri (1.5.10)
|
55
|
+
oauth2 (0.9.2)
|
56
|
+
faraday (~> 0.8)
|
57
|
+
httpauth (~> 0.2)
|
58
|
+
jwt (~> 0.1.4)
|
59
|
+
multi_json (~> 1.0)
|
60
|
+
multi_xml (~> 0.5)
|
61
|
+
rack (~> 1.2)
|
62
|
+
rack (1.5.2)
|
63
|
+
rake (10.1.0)
|
64
|
+
rdoc (3.12.2)
|
65
|
+
json (~> 1.4)
|
66
|
+
rspec (2.12.0)
|
67
|
+
rspec-core (~> 2.12.0)
|
68
|
+
rspec-expectations (~> 2.12.0)
|
69
|
+
rspec-mocks (~> 2.12.0)
|
70
|
+
rspec-core (2.12.2)
|
71
|
+
rspec-expectations (2.12.1)
|
72
|
+
diff-lcs (~> 1.1.3)
|
73
|
+
rspec-mocks (2.12.2)
|
74
|
+
sqlite3 (1.3.8)
|
75
|
+
thread_safe (0.1.3)
|
76
|
+
atomic
|
77
|
+
tzinfo (0.3.38)
|
78
|
+
|
79
|
+
PLATFORMS
|
80
|
+
ruby
|
81
|
+
|
82
|
+
DEPENDENCIES
|
83
|
+
activerecord (>= 3.0.0)
|
84
|
+
bundler (~> 1.0)
|
85
|
+
jeweler (~> 1.8.7)
|
86
|
+
rdoc (~> 3.12)
|
87
|
+
rspec (~> 2.12.0)
|
88
|
+
sqlite3
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2013 Dave
|
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,62 @@
|
|
1
|
+
= attr-csv
|
2
|
+
|
3
|
+
This gem allows you to define CSV-style attributes for your ActiveRecord models.
|
4
|
+
This is similar to serializing an ActiveRecord attribute except that it is
|
5
|
+
stored in the database column as comma-separated-values rather than a JSON or
|
6
|
+
YAML.
|
7
|
+
|
8
|
+
The advantage of using the CSV format in the database is that you can use the
|
9
|
+
FIND_IN_SET() function (MySQL) to perform queries that can filter based on
|
10
|
+
a particular value found in the set. If you don't use MySQL, it can still be
|
11
|
+
used as just another way to serialize an array into a database column. It sure
|
12
|
+
prints better in a select result set.
|
13
|
+
|
14
|
+
If these reasons aren't compelling enough for you, don't use it.
|
15
|
+
|
16
|
+
Here is an example usage:
|
17
|
+
|
18
|
+
class MyCsvModel < ActiveRecord::Base
|
19
|
+
attr_csv :multivalue
|
20
|
+
end
|
21
|
+
|
22
|
+
# The schema
|
23
|
+
CREATE TABLE my_csv_models (
|
24
|
+
id INTEGER PRIMARY KEY,
|
25
|
+
multivalue_csv VARCHAR(255)
|
26
|
+
)
|
27
|
+
|
28
|
+
# Use it
|
29
|
+
m = MyCsvModel.create! multivalue: [ 'a', 'b', 'c' ]
|
30
|
+
|
31
|
+
# Query for a value in the set...
|
32
|
+
ms = MyCsvModel.where "FIND_IN_SET('a', multivalue_csv) > 0"
|
33
|
+
|
34
|
+
ms.first.multivalue.inspect
|
35
|
+
|
36
|
+
# => [ 'a', 'b', 'c' ]
|
37
|
+
|
38
|
+
The default behavior is to assume that the database column will be named
|
39
|
+
the same as the attribute with an appended '_csv'.
|
40
|
+
|
41
|
+
If the CSV attribute is set to an empty array (or nil), the database column
|
42
|
+
will be set to nil.
|
43
|
+
|
44
|
+
You can actually use the 'attr_csv' function in a Plain Old Ruby Object, but
|
45
|
+
I have yet to figure out the use for it (unless it was to be used with a
|
46
|
+
different ORM - I'm not that adventurous).
|
47
|
+
|
48
|
+
== Contributing to attr-csv
|
49
|
+
|
50
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
|
51
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
|
52
|
+
* Fork the project.
|
53
|
+
* Start a feature/bugfix branch.
|
54
|
+
* Commit and push until you are happy with your contribution.
|
55
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
56
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
57
|
+
|
58
|
+
== Copyright
|
59
|
+
|
60
|
+
Copyright (c) 2013 Dave. See LICENSE.txt for
|
61
|
+
further details.
|
62
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
17
|
+
gem.name = "attr-csv"
|
18
|
+
gem.homepage = "http://github.com/j0hnds/attr-csv"
|
19
|
+
gem.license = "MIT"
|
20
|
+
gem.summary = %Q{Provides CSV-style attributes for ActiveRecord models}
|
21
|
+
gem.description = %Q{Allows you to define CSV-style attributes for your ActiveRecord models}
|
22
|
+
gem.email = "j0hnds@gmail.com"
|
23
|
+
gem.authors = ["Dave Sieh"]
|
24
|
+
# dependencies defined in Gemfile
|
25
|
+
end
|
26
|
+
Jeweler::RubygemsDotOrgTasks.new
|
27
|
+
|
28
|
+
require 'rspec/core'
|
29
|
+
require 'rspec/core/rake_task'
|
30
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
31
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
32
|
+
end
|
33
|
+
|
34
|
+
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
35
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
36
|
+
spec.rcov = true
|
37
|
+
end
|
38
|
+
|
39
|
+
task :default => :spec
|
40
|
+
|
41
|
+
require 'rdoc/task'
|
42
|
+
Rake::RDocTask.new do |rdoc|
|
43
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
44
|
+
|
45
|
+
rdoc.rdoc_dir = 'rdoc'
|
46
|
+
rdoc.title = "attr-csv #{version}"
|
47
|
+
rdoc.rdoc_files.include('README*')
|
48
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
49
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
data/lib/attr-csv.rb
ADDED
@@ -0,0 +1,121 @@
|
|
1
|
+
require 'set'
|
2
|
+
require 'csv'
|
3
|
+
|
4
|
+
module AttrCsv
|
5
|
+
|
6
|
+
CSV_ATTR_EXTENSION = '_csv'
|
7
|
+
|
8
|
+
def self.extended(base)
|
9
|
+
base.class_eval do
|
10
|
+
extend ClassMethods
|
11
|
+
include InstanceMethods
|
12
|
+
@csved_attributes = Set.new
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
module ClassMethods
|
17
|
+
|
18
|
+
def attr_csv(*attributes)
|
19
|
+
instance_methods_as_symbols = instance_methods.map { | mthd | mthd.to_sym }
|
20
|
+
|
21
|
+
attributes.each do | attribute |
|
22
|
+
# Construct the name to be used for the CSV version
|
23
|
+
# of the attribute
|
24
|
+
csv_attribute_name = [ attribute.to_s, CSV_ATTR_EXTENSION ].join.to_sym
|
25
|
+
|
26
|
+
# Create the accessors for the csv attribute
|
27
|
+
unless is_active_record_model?(self)
|
28
|
+
attr_reader csv_attribute_name unless instance_methods_as_symbols.include?(csv_attribute_name)
|
29
|
+
attr_writer csv_attribute_name unless instance_methods_as_symbols.include?("#{csv_attribute_name}=".to_sym)
|
30
|
+
end
|
31
|
+
|
32
|
+
# Now, define the array attribute reader
|
33
|
+
define_method(attribute) do
|
34
|
+
(instance_variable_get("@#{attribute}") ||
|
35
|
+
instance_variable_set("@#{attribute}",
|
36
|
+
decode(attribute, send(csv_attribute_name))))
|
37
|
+
end
|
38
|
+
|
39
|
+
# and the array attribute writer
|
40
|
+
define_method("#{attribute}=") do | value |
|
41
|
+
send("#{csv_attribute_name}=", encode(attribute, value))
|
42
|
+
instance_variable_set("@#{attribute}", value)
|
43
|
+
end
|
44
|
+
|
45
|
+
define_method("#{attribute}?") do
|
46
|
+
value = send(attribute)
|
47
|
+
value.respond_to?(:empty?) ? !value.empty? : !!value
|
48
|
+
end
|
49
|
+
|
50
|
+
csved_attributes << attribute.to_sym
|
51
|
+
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
|
56
|
+
def is_active_record_model?(clazz)
|
57
|
+
return false if clazz.nil?
|
58
|
+
return true if clazz.to_s == 'ActiveRecord::Base'
|
59
|
+
is_active_record_model?(clazz.superclass)
|
60
|
+
end
|
61
|
+
|
62
|
+
def attr_csved?(attribute)
|
63
|
+
csved_attributes.include?(attribute.to_sym)
|
64
|
+
end
|
65
|
+
|
66
|
+
def csved_attributes
|
67
|
+
@csved_attributes ||= superclass.csved_attributes.dup
|
68
|
+
end
|
69
|
+
|
70
|
+
def decode(attribute, csv_value)
|
71
|
+
return [] if csv_value.nil? || csv_value.empty?
|
72
|
+
CSV.parse(csv_value).first
|
73
|
+
end
|
74
|
+
|
75
|
+
def encode(attribute, array_value)
|
76
|
+
return nil if array_value.nil? || array_value.empty?
|
77
|
+
array_value.to_csv.chomp
|
78
|
+
end
|
79
|
+
|
80
|
+
def method_missing(method, *arguments, &block)
|
81
|
+
if method.to_s =~ /^((en|de)code)_(.+)$/ && attr_csved?($3)
|
82
|
+
send($1, $3, *arguments)
|
83
|
+
else
|
84
|
+
super
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
end
|
89
|
+
|
90
|
+
module InstanceMethods
|
91
|
+
|
92
|
+
def decode(attribute, csv_value)
|
93
|
+
self.class.decode(attribute, csv_value)
|
94
|
+
end
|
95
|
+
|
96
|
+
def encode(attribute, array_value)
|
97
|
+
self.class.encode(attribute, array_value)
|
98
|
+
end
|
99
|
+
|
100
|
+
#
|
101
|
+
# For ActiveRecord models, this method is called as a before_validation
|
102
|
+
# callback to ensure that the CSV values have been update to the '_csv'
|
103
|
+
# columns appropriately. The user might have used an array modifier
|
104
|
+
# directly (e.g. << or push), which we wouldn't see. So, to make sure
|
105
|
+
# the database gets updated appropriately, we do this.
|
106
|
+
#
|
107
|
+
def update_csved_attributes
|
108
|
+
self.class.csved_attributes.each do | attribute |
|
109
|
+
csv_attribute_name = [ attribute.to_s, CSV_ATTR_EXTENSION ].join.to_sym
|
110
|
+
send("#{csv_attribute_name}=", encode(attribute, send(attribute)))
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
end
|
115
|
+
|
116
|
+
end
|
117
|
+
|
118
|
+
Object.extend AttrCsv
|
119
|
+
|
120
|
+
require 'active_record'
|
121
|
+
require 'attr-csv/active-record'
|
@@ -0,0 +1,32 @@
|
|
1
|
+
if defined?(ActiveRecord::Base)
|
2
|
+
|
3
|
+
module AttrCsv
|
4
|
+
|
5
|
+
module Adapters
|
6
|
+
|
7
|
+
module ActiveRecord
|
8
|
+
|
9
|
+
protected
|
10
|
+
|
11
|
+
#
|
12
|
+
# Ensures the attribute methods for db fields have been defined
|
13
|
+
# before calling the original attr_encrypted method
|
14
|
+
#
|
15
|
+
def attr_csv(*attributes)
|
16
|
+
define_attribute_methods rescue nil
|
17
|
+
super
|
18
|
+
attributes.reject { | attr | attr.is_a?(Hash) }.each { | attr | alias_method "#{attr}_before_type_cast", attr }
|
19
|
+
|
20
|
+
# Register before_validate to update the csv fields
|
21
|
+
before_validation :update_csved_attributes
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
ActiveRecord::Base.extend AttrCsv::Adapters::ActiveRecord
|
31
|
+
|
32
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "AttrCsv" do
|
4
|
+
|
5
|
+
context 'CsvPoro' do
|
6
|
+
let(:csv_poro) { CsvPoro.new }
|
7
|
+
|
8
|
+
it "should have a legit multivalue accessor" do
|
9
|
+
csv_poro.multivalue = [ 'a', 'b' ]
|
10
|
+
expect(csv_poro.multivalue).to eq([ 'a', 'b' ])
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should set the csv attribute when the attribute is set" do
|
14
|
+
csv_poro.multivalue = [ 'a', 'b' ]
|
15
|
+
expect(csv_poro.multivalue_csv).to eq('a,b')
|
16
|
+
csv_poro.multivalue = [ 'a', 'b', 'c' ]
|
17
|
+
expect(csv_poro.multivalue_csv).to eq('a,b,c')
|
18
|
+
csv_poro.multivalue = [ ]
|
19
|
+
expect(csv_poro.multivalue_csv).to be_nil
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should reflect the csv value if the multivalue field is nil" do
|
23
|
+
csv_poro.multivalue_csv = 'a,b,c'
|
24
|
+
expect(csv_poro.multivalue).to eq([ 'a', 'b', 'c' ])
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,119 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "Rails Extensions" do
|
4
|
+
|
5
|
+
before(:each) do
|
6
|
+
ActiveRecord::Base.establish_connection(adapter: 'sqlite3',
|
7
|
+
database: ':memory:')
|
8
|
+
|
9
|
+
ActiveRecord::Schema.define do
|
10
|
+
create_table :csv_models do | t |
|
11
|
+
t.column :name, :string
|
12
|
+
t.column :multivalue_csv, :string
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
context 'CsvModel' do
|
19
|
+
|
20
|
+
it "should allow the csv column to be saved with a null, but the field should show as an empty array" do
|
21
|
+
CsvModel.create! name: 'Joe'
|
22
|
+
csv_model = CsvModel.first
|
23
|
+
expect(csv_model).not_to be_nil
|
24
|
+
expect(csv_model.multivalue).to eq([])
|
25
|
+
expect(csv_model.name).to eq('Joe')
|
26
|
+
expect(csv_model.multivalue_csv).to be_nil
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should allow the csv column to be initialized in the constructor" do
|
30
|
+
m = CsvModel.new name: 'Harry', multivalue: [ 'val1', 'val2' ]
|
31
|
+
|
32
|
+
expect(m.multivalue).to eq([ 'val1', 'val2' ])
|
33
|
+
expect(m.multivalue_csv).to eq('val1,val2')
|
34
|
+
|
35
|
+
m.save!
|
36
|
+
csv_model = CsvModel.first
|
37
|
+
|
38
|
+
expect(csv_model).not_to be_nil
|
39
|
+
expect(csv_model.name).to eq('Harry')
|
40
|
+
expect(csv_model.multivalue_csv).to eq('val1,val2')
|
41
|
+
expect(csv_model.multivalue).to eq([ 'val1', 'val2' ])
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should allow the csv column to be initialized as an attribute" do
|
45
|
+
m = CsvModel.new name: 'Harry'
|
46
|
+
m.multivalue = [ 'val1', 'val2' ]
|
47
|
+
|
48
|
+
expect(m.multivalue).to eq([ 'val1', 'val2' ])
|
49
|
+
expect(m.multivalue_csv).to eq('val1,val2')
|
50
|
+
|
51
|
+
m.save!
|
52
|
+
csv_model = CsvModel.first
|
53
|
+
|
54
|
+
expect(csv_model).not_to be_nil
|
55
|
+
expect(csv_model.name).to eq('Harry')
|
56
|
+
expect(csv_model.multivalue_csv).to eq('val1,val2')
|
57
|
+
expect(csv_model.multivalue).to eq([ 'val1', 'val2' ])
|
58
|
+
end
|
59
|
+
|
60
|
+
it "should allow the csv column to be initialized with an empty array" do
|
61
|
+
m = CsvModel.new name: 'Harry', multivalue: [ ]
|
62
|
+
|
63
|
+
expect(m.multivalue).to eq([ ])
|
64
|
+
expect(m.multivalue_csv).to be_nil
|
65
|
+
|
66
|
+
m.save!
|
67
|
+
csv_model = CsvModel.first
|
68
|
+
|
69
|
+
expect(csv_model).not_to be_nil
|
70
|
+
expect(csv_model.name).to eq('Harry')
|
71
|
+
expect(csv_model.multivalue_csv).to be_nil
|
72
|
+
expect(csv_model.multivalue).to eq([])
|
73
|
+
end
|
74
|
+
|
75
|
+
it "should allow the csv column to be incremented with +=" do
|
76
|
+
m = CsvModel.new name: 'Harry', multivalue: [ 'c' ]
|
77
|
+
|
78
|
+
expect(m.multivalue).to eq([ 'c' ])
|
79
|
+
expect(m.multivalue_csv).to eq('c')
|
80
|
+
|
81
|
+
m.save!
|
82
|
+
csv_model = CsvModel.first
|
83
|
+
|
84
|
+
expect(csv_model).not_to be_nil
|
85
|
+
expect(csv_model.name).to eq('Harry')
|
86
|
+
expect(csv_model.multivalue_csv).to eq('c')
|
87
|
+
expect(csv_model.multivalue).to eq([ 'c' ])
|
88
|
+
|
89
|
+
csv_model.multivalue += [ 'a', 'b' ]
|
90
|
+
csv_model.save!
|
91
|
+
csv_model = CsvModel.first
|
92
|
+
|
93
|
+
expect(csv_model.multivalue).to eq([ 'c', 'a', 'b' ])
|
94
|
+
end
|
95
|
+
|
96
|
+
it "should allow a single value to be appended to the column with <<" do
|
97
|
+
m = CsvModel.new name: 'Harry', multivalue: [ 'c' ]
|
98
|
+
|
99
|
+
expect(m.multivalue).to eq([ 'c' ])
|
100
|
+
expect(m.multivalue_csv).to eq('c')
|
101
|
+
|
102
|
+
m.save!
|
103
|
+
csv_model = CsvModel.first
|
104
|
+
|
105
|
+
expect(csv_model).not_to be_nil
|
106
|
+
expect(csv_model.name).to eq('Harry')
|
107
|
+
expect(csv_model.multivalue_csv).to eq('c')
|
108
|
+
expect(csv_model.multivalue).to eq([ 'c' ])
|
109
|
+
|
110
|
+
csv_model.multivalue << 'a'
|
111
|
+
csv_model.save!
|
112
|
+
csv_model = CsvModel.first
|
113
|
+
|
114
|
+
expect(csv_model.multivalue).to eq([ 'c', 'a' ])
|
115
|
+
end
|
116
|
+
|
117
|
+
end
|
118
|
+
|
119
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
|
+
require 'rspec'
|
4
|
+
require 'attr-csv'
|
5
|
+
|
6
|
+
# Requires supporting files with custom matchers and macros, etc,
|
7
|
+
# in ./support/ and its subdirectories.
|
8
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
9
|
+
|
10
|
+
RSpec.configure do |config|
|
11
|
+
|
12
|
+
end
|
metadata
ADDED
@@ -0,0 +1,144 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: attr-csv
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Dave Sieh
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-11-02 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activerecord
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 3.0.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 3.0.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 2.12.0
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 2.12.0
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rdoc
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.12'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.12'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: bundler
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ~>
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: jeweler
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ~>
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 1.8.7
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ~>
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 1.8.7
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: sqlite3
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - '>='
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
description: Allows you to define CSV-style attributes for your ActiveRecord models
|
98
|
+
email: j0hnds@gmail.com
|
99
|
+
executables: []
|
100
|
+
extensions: []
|
101
|
+
extra_rdoc_files:
|
102
|
+
- LICENSE.txt
|
103
|
+
- README.rdoc
|
104
|
+
files:
|
105
|
+
- .document
|
106
|
+
- .rspec
|
107
|
+
- Gemfile
|
108
|
+
- Gemfile.lock
|
109
|
+
- LICENSE.txt
|
110
|
+
- README.rdoc
|
111
|
+
- Rakefile
|
112
|
+
- VERSION
|
113
|
+
- lib/attr-csv.rb
|
114
|
+
- lib/attr-csv/active-record.rb
|
115
|
+
- spec/attr-csv_spec.rb
|
116
|
+
- spec/rails_extensions_spec.rb
|
117
|
+
- spec/spec_helper.rb
|
118
|
+
- spec/support/csv_model.rb
|
119
|
+
- spec/support/csv_poro.rb
|
120
|
+
homepage: http://github.com/j0hnds/attr-csv
|
121
|
+
licenses:
|
122
|
+
- MIT
|
123
|
+
metadata: {}
|
124
|
+
post_install_message:
|
125
|
+
rdoc_options: []
|
126
|
+
require_paths:
|
127
|
+
- lib
|
128
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
129
|
+
requirements:
|
130
|
+
- - '>='
|
131
|
+
- !ruby/object:Gem::Version
|
132
|
+
version: '0'
|
133
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
134
|
+
requirements:
|
135
|
+
- - '>='
|
136
|
+
- !ruby/object:Gem::Version
|
137
|
+
version: '0'
|
138
|
+
requirements: []
|
139
|
+
rubyforge_project:
|
140
|
+
rubygems_version: 2.0.2
|
141
|
+
signing_key:
|
142
|
+
specification_version: 4
|
143
|
+
summary: Provides CSV-style attributes for ActiveRecord models
|
144
|
+
test_files: []
|