tabular_data 0.0.2 → 0.0.21
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/README.md +18 -5
- data/lib/tabular_data.rb +5 -4
- data/lib/tabular_data/version.rb +1 -1
- data/test/integration/test_reader.rb +13 -1
- metadata +2 -2
data/README.md
CHANGED
@@ -18,12 +18,12 @@ Or install it yourself as:
|
|
18
18
|
|
19
19
|
## Usage
|
20
20
|
|
21
|
-
|
22
21
|
```ruby
|
23
22
|
class Person
|
24
|
-
|
25
|
-
|
23
|
+
|
26
24
|
ATTRIBUTES = [:name, :age]
|
25
|
+
|
26
|
+
attr_accessor *ATTRIBUTES
|
27
27
|
end
|
28
28
|
|
29
29
|
reader = TabularData::Reader.new(Person::ATTRIBUTES)
|
@@ -34,6 +34,19 @@ person.name
|
|
34
34
|
|
35
35
|
person.age
|
36
36
|
=> "23"
|
37
|
+
|
38
|
+
person = reader.read(Person.new, "Marcos;23") do |attribute, value|
|
39
|
+
return case attribute
|
40
|
+
when :name then value.upcase
|
41
|
+
when :age then value.to_i
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
person.name
|
46
|
+
=> "MARCOS"
|
47
|
+
|
48
|
+
person.age
|
49
|
+
=> 23
|
37
50
|
```
|
38
51
|
|
39
52
|
### Changing strategies
|
@@ -79,9 +92,9 @@ person.age
|
|
79
92
|
### Useful methods
|
80
93
|
|
81
94
|
```ruby
|
82
|
-
people = TabularData.parse_csv("people_data.csv", Person::ATTRIBUTES,
|
95
|
+
people = TabularData.parse_csv("people_data.csv", Person::ATTRIBUTES, Person.method(:new))
|
83
96
|
|
84
|
-
people = TabularData.parse_lines("Marcos;23\nFelipe;25", Person::ATTRIBUTES,
|
97
|
+
people = TabularData.parse_lines("Marcos;23\nFelipe;25", Person::ATTRIBUTES, Person.method(:new))
|
85
98
|
```
|
86
99
|
|
87
100
|
## Contributing
|
data/lib/tabular_data.rb
CHANGED
@@ -20,10 +20,11 @@ module TabularData
|
|
20
20
|
module Strategies
|
21
21
|
class ReaderStrategy
|
22
22
|
|
23
|
-
def call(context, attributes_to_parse, attributes)
|
23
|
+
def call(context, attributes_to_parse, attributes, &block)
|
24
|
+
block = lambda{ |attribute, value| value } unless block_given?
|
24
25
|
attributes_to_parse = parse_attributes(attributes_to_parse)
|
25
26
|
attributes.each_with_index do |attribute, i|
|
26
|
-
context.send "#{attribute}=", attributes_to_parse[i]
|
27
|
+
context.send "#{attribute}=", block.call(attribute, attributes_to_parse[i])
|
27
28
|
end
|
28
29
|
end
|
29
30
|
|
@@ -79,8 +80,8 @@ module TabularData
|
|
79
80
|
@attributes = attributes
|
80
81
|
end
|
81
82
|
|
82
|
-
def read(entry, attributes_to_parse)
|
83
|
-
strategy.call(entry, attributes_to_parse, @attributes)
|
83
|
+
def read(entry, attributes_to_parse, &block)
|
84
|
+
strategy.call(entry, attributes_to_parse, @attributes, &block)
|
84
85
|
entry
|
85
86
|
end
|
86
87
|
|
data/lib/tabular_data/version.rb
CHANGED
@@ -81,6 +81,18 @@ class TestEntry < Test::Unit::TestCase
|
|
81
81
|
def test_default_strategy
|
82
82
|
@reader.read(@entry, "first;second")
|
83
83
|
assert_equal "first", @entry.a
|
84
|
-
assert_equal "second", @entry.b
|
84
|
+
assert_equal "second", @entry.b
|
85
|
+
end
|
86
|
+
|
87
|
+
def test_process_attributes_callback
|
88
|
+
@reader.read(@entry, "first;2") do |attribute, value|
|
89
|
+
return case attribute
|
90
|
+
when :first then value.upcase
|
91
|
+
when :second then value.to_i
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
assert_equal "FIRST", @entry.a
|
96
|
+
assert_equal 2, @entry.b
|
85
97
|
end
|
86
98
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tabular_data
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.21
|
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-07-01 00:00:00.000000000Z
|
13
13
|
dependencies: []
|
14
14
|
description: A library for parsing tabular data.
|
15
15
|
email:
|