dumb_delimited 1.0.0 → 1.1.0
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.
- checksums.yaml +5 -5
- data/CHANGELOG.md +10 -0
- data/README.md +19 -9
- data/lib/dumb_delimited.rb +51 -8
- data/lib/dumb_delimited/version.rb +1 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 2a4d7deecbbed56e6940058fdc548436cbdb0f081b43164bc841017601c27726
|
4
|
+
data.tar.gz: b94ae4701ae2dec7cee54befe2e85c11ea20a9ae5906d0f7da6a8da1ef377eb6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9f2994715b89523b10c4fb1375b2a073e306cedfae565f502241a4ad8f82876b1ec02d1b453641842ca331bba2812ea73783c70af7cc1431b0bbaeec83e0ce09
|
7
|
+
data.tar.gz: 36b9db417ddd340f695e1332ea005ad8ec03c3e8d31f5cc395b8479463497bee0e518e89f5916a90d0267ebfe9779abb21995d35e66f8da67b91f282e955b2b1
|
data/CHANGELOG.md
ADDED
data/README.md
CHANGED
@@ -27,8 +27,8 @@ Bob Bobbington|best_bob@bobbers.bob|808 Bounce Lane, Austin, TX 78703
|
|
27
27
|
```
|
28
28
|
|
29
29
|
To interact with these files, we create model classes via the
|
30
|
-
`DumbDelimited
|
31
|
-
as a superclass or simply assigned to a constant.
|
30
|
+
`DumbDelimited::[]` method. Note that a created class can either be
|
31
|
+
used as a superclass or simply assigned to a constant.
|
32
32
|
|
33
33
|
```ruby
|
34
34
|
class Product < DumbDelimited[:sku, :name, :base_price, :sale_price]
|
@@ -79,10 +79,10 @@ end
|
|
79
79
|
|
80
80
|
Let's say the sale is now over, and we want to change our sale prices
|
81
81
|
back to our base prices. *dumb_delimited* includes the
|
82
|
-
[*pleasant_path*](https://
|
83
|
-
|
84
|
-
|
85
|
-
|
82
|
+
[*pleasant_path*](https://rubygems.org/gems/pleasant_path) gem, which
|
83
|
+
offers a fluent API for writing files. To finish our task, we use the
|
84
|
+
`Array#write_to_file` method provided by *pleasant_path*, which in turn
|
85
|
+
invokes `Product#to_s` (provided by *dumb_delimited*) on each model
|
86
86
|
object.
|
87
87
|
|
88
88
|
```ruby
|
@@ -97,10 +97,20 @@ For a more detailed explanation of the *dumb_delimited* API, browse the
|
|
97
97
|
|
98
98
|
## Installation
|
99
99
|
|
100
|
-
|
100
|
+
Install from [Ruby Gems](https://rubygems.org/gems/dumb_delimited):
|
101
|
+
|
102
|
+
```bash
|
103
|
+
$ gem install dumb_delimited
|
104
|
+
```
|
105
|
+
|
106
|
+
Then require in your Ruby script:
|
107
|
+
|
108
|
+
```ruby
|
109
|
+
require "dumb_delimited"
|
110
|
+
```
|
101
111
|
|
102
112
|
|
103
|
-
##
|
113
|
+
## Contributing
|
104
114
|
|
105
115
|
Run `rake test` to run the tests. You can also run `rake irb` for an
|
106
116
|
interactive prompt that pre-loads the project code.
|
@@ -108,4 +118,4 @@ interactive prompt that pre-loads the project code.
|
|
108
118
|
|
109
119
|
## License
|
110
120
|
|
111
|
-
[MIT License](
|
121
|
+
[MIT License](https://opensource.org/licenses/MIT)
|
data/lib/dumb_delimited.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
require "
|
1
|
+
require "csv"
|
2
2
|
require "pleasant_path"
|
3
|
-
|
3
|
+
require_relative "dumb_delimited/version"
|
4
4
|
|
5
5
|
|
6
6
|
module DumbDelimited
|
@@ -45,7 +45,7 @@ module DumbDelimited::ClassMethods
|
|
45
45
|
# @return [Hash]
|
46
46
|
def options
|
47
47
|
@options ||= {
|
48
|
-
col_sep:
|
48
|
+
col_sep: ",",
|
49
49
|
skip_blanks: true,
|
50
50
|
converters: :numeric,
|
51
51
|
}
|
@@ -92,9 +92,7 @@ module DumbDelimited::ClassMethods
|
|
92
92
|
#
|
93
93
|
# @example
|
94
94
|
# Point = DumbDelimited[:x, :y, :z]
|
95
|
-
#
|
96
|
-
# p.is_a?(Point) # == true
|
97
|
-
# p.to_a # == [1, 2, 3]
|
95
|
+
# Point.parse_line("1,2,3") # == Point.new(1, 2, 3)
|
98
96
|
#
|
99
97
|
# @param line [String]
|
100
98
|
# @return [self]
|
@@ -102,6 +100,25 @@ module DumbDelimited::ClassMethods
|
|
102
100
|
self.new(*CSV.parse_line(line, self.options))
|
103
101
|
end
|
104
102
|
|
103
|
+
# Parses a string into an array of model objects.
|
104
|
+
#
|
105
|
+
# @example
|
106
|
+
# Point = DumbDelimited[:x, :y, :z]
|
107
|
+
# Point.parse_text("1,2,3\n4,5,6\n7,8,9\n")
|
108
|
+
# # == [
|
109
|
+
# # Point.new(1, 2, 3),
|
110
|
+
# # Point.new(4, 5, 6),
|
111
|
+
# # Point.new(7, 8, 9)
|
112
|
+
# # ]
|
113
|
+
#
|
114
|
+
# @param text [String]
|
115
|
+
# @return [Array<self>]
|
116
|
+
def parse_text(text)
|
117
|
+
# using CSV.new.each instead of CSV.parse to avoid unnecessary mass
|
118
|
+
# memory allocation and deallocation
|
119
|
+
CSV.new(text, self.options).each.map{|row| self.new(*row) }
|
120
|
+
end
|
121
|
+
|
105
122
|
# Parses an entire delimited file into an array of model objects.
|
106
123
|
# This will load the entire contents of the file into memory, and may
|
107
124
|
# not be suitable for large files. To iterate over file contents
|
@@ -114,8 +131,12 @@ module DumbDelimited::ClassMethods
|
|
114
131
|
# # 7,8,9
|
115
132
|
#
|
116
133
|
# Point = DumbDelimited[:x, :y, :z]
|
117
|
-
#
|
118
|
-
#
|
134
|
+
# Point.parse_file("points.csv")
|
135
|
+
# # == [
|
136
|
+
# # Point.new(1, 2, 3),
|
137
|
+
# # Point.new(4, 5, 6),
|
138
|
+
# # Point.new(7, 8, 9)
|
139
|
+
# # ]
|
119
140
|
#
|
120
141
|
# @param path [String, Pathname]
|
121
142
|
# @return [Array<self>]
|
@@ -155,4 +176,26 @@ module DumbDelimited::InstanceMethods
|
|
155
176
|
CSV.generate_line(self, self.class.options).chomp!
|
156
177
|
end
|
157
178
|
|
179
|
+
# Serializes a model object to a delimited string, using the delimiter
|
180
|
+
# specified by {ClassMethods.delimiter}, and appends the string plus a
|
181
|
+
# row separator to the specified file. Returns the model object.
|
182
|
+
# This method is convenient when working with single model objects,
|
183
|
+
# for example, appending a single entry to a log file. However, it is
|
184
|
+
# not recommended for use with an array of model objects, due to the
|
185
|
+
# overhead of opening and closing the file for each append.
|
186
|
+
#
|
187
|
+
# @example
|
188
|
+
# Point = DumbDelimited[:x, :y, :z]
|
189
|
+
# Point.new(1, 2, 3).append_to_file("out.txt") # == Point.new(1, 2, 3)
|
190
|
+
# File.read("out.txt") # == "1,2,3\n"
|
191
|
+
# Point.new(4, 5, 6).append_to_file("out.txt") # == Point.new(4, 5, 6)
|
192
|
+
# File.read("out.txt") # == "1,2,3\n4,5,6\n"
|
193
|
+
#
|
194
|
+
# @param file [String, Pathname]
|
195
|
+
# @return [self]
|
196
|
+
def append_to_file(file)
|
197
|
+
CSV.generate_line(self, self.class.options).append_to_file(file)
|
198
|
+
self
|
199
|
+
end
|
200
|
+
|
158
201
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dumb_delimited
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jonathan Hefner
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-07-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: pleasant_path
|
@@ -89,6 +89,7 @@ extra_rdoc_files: []
|
|
89
89
|
files:
|
90
90
|
- ".gitignore"
|
91
91
|
- ".travis.yml"
|
92
|
+
- CHANGELOG.md
|
92
93
|
- Gemfile
|
93
94
|
- LICENSE.txt
|
94
95
|
- README.md
|
@@ -116,7 +117,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
116
117
|
version: '0'
|
117
118
|
requirements: []
|
118
119
|
rubyforge_project:
|
119
|
-
rubygems_version: 2.
|
120
|
+
rubygems_version: 2.7.6
|
120
121
|
signing_key:
|
121
122
|
specification_version: 4
|
122
123
|
summary: Library for unsophisticated delimited flat file IO
|