loady 0.4.1 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/loady/array.rb +23 -21
- data/lib/loady/csv_loader.rb +1 -1
- data/lib/loady/version.rb +1 -1
- data/test/test_loady.rb +3 -1
- metadata +4 -4
data/lib/loady/array.rb
CHANGED
@@ -1,30 +1,32 @@
|
|
1
|
-
|
1
|
+
module Loady
|
2
|
+
module Array
|
2
3
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
4
|
+
# usage:
|
5
|
+
# ['john', 'doe'].to_attributes([:first, :last])
|
6
|
+
# => { first: 'john', last: 'doe' }
|
7
|
+
#
|
8
|
+
# options:
|
9
|
+
# strip: false -- default = true
|
10
|
+
# -- array values must be strings if :strip is true
|
11
|
+
def to_attributes(names, options={})
|
12
|
+
options = { strip: true }.merge(options)
|
12
13
|
|
13
|
-
|
14
|
+
h = {}
|
14
15
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
16
|
+
names.each_with_index do |name, i|
|
17
|
+
if i < self.size
|
18
|
+
if options[:strip] && self[i]
|
19
|
+
h[name] = self[i].strip
|
20
|
+
else
|
21
|
+
h[name] = self[i]
|
22
|
+
end
|
19
23
|
else
|
20
|
-
h[name] =
|
24
|
+
h[name] = nil
|
21
25
|
end
|
22
|
-
else
|
23
|
-
h[name] = nil
|
24
26
|
end
|
27
|
+
|
28
|
+
h
|
25
29
|
end
|
26
30
|
|
27
|
-
h
|
28
31
|
end
|
29
|
-
|
30
|
-
end
|
32
|
+
end
|
data/lib/loady/csv_loader.rb
CHANGED
data/lib/loady/version.rb
CHANGED
data/test/test_loady.rb
CHANGED
@@ -1,10 +1,11 @@
|
|
1
1
|
require File.expand_path('test_helper.rb', File.dirname(__FILE__))
|
2
2
|
|
3
3
|
class LoadyTest < Test::Unit::TestCase
|
4
|
-
context "
|
4
|
+
context "#to_attributes" do
|
5
5
|
|
6
6
|
should "return named attributes" do
|
7
7
|
row = ['Bubbles ', '2000', ' King Kong ']
|
8
|
+
row.extend Loady::Array
|
8
9
|
attrs = row.to_attributes [:name, :year, :mom]
|
9
10
|
|
10
11
|
assert_equal attrs.size, 3
|
@@ -15,6 +16,7 @@ class LoadyTest < Test::Unit::TestCase
|
|
15
16
|
|
16
17
|
should "return named attributes when missing values" do
|
17
18
|
row = ['Bubbles ', '2000']
|
19
|
+
row.extend Loady::Array
|
18
20
|
attrs = row.to_attributes [:name, :year, :mom]
|
19
21
|
|
20
22
|
assert_equal attrs.size, 3
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: loady
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-11-03 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: test-unit
|
@@ -98,7 +98,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
98
98
|
version: '0'
|
99
99
|
segments:
|
100
100
|
- 0
|
101
|
-
hash:
|
101
|
+
hash: 3713324824844609253
|
102
102
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
103
103
|
none: false
|
104
104
|
requirements:
|
@@ -107,7 +107,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
107
107
|
version: '0'
|
108
108
|
segments:
|
109
109
|
- 0
|
110
|
-
hash:
|
110
|
+
hash: 3713324824844609253
|
111
111
|
requirements: []
|
112
112
|
rubyforge_project: loady
|
113
113
|
rubygems_version: 1.8.24
|