cascade-rb 0.1.2 → 0.1.4
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 +4 -4
- data/README.md +72 -8
- data/lib/cascade/version.rb +1 -1
- metadata +1 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fcc191b61c12c0765f47269758020be4aee60980
|
4
|
+
data.tar.gz: b5112f6db4896cf045c1b000c895c6b570ffb59b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dd6417644ca77d59c98ef1a90328753ac78e1e446fd598a4ac8325370adf7a96b3df37e2ef2dd1b77cc5348b9b4f86b44e7ce35f86908870e66e9df419eb9e17
|
7
|
+
data.tar.gz: 72ecd47503ee586f47f1ca5727ad500e36be2c12bed35a8fef63eb6d521f81733275a12e7952ea00fcf1d49c04f85dff901ad39c1c5844bce94209ec541ed65e
|
data/README.md
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
# [Cascade]
|
2
2
|
|
3
|
-
[](https://codeship.com/projects/63625) [](https://codeclimate.com/github/ignat-zakrevsky/cascade) [](https://codeclimate.com/github/ignat-zakrevsky/cascade)
|
3
|
+
[](https://codeship.com/projects/63625) [](https://codeclimate.com/github/ignat-zakrevsky/cascade) [](https://codeclimate.com/github/ignat-zakrevsky/cascade) [](http://badge.fury.io/rb/cascade-rb)
|
4
4
|
|
5
|
-
The main aim of this
|
5
|
+
The main aim of this gem is to provide some kind of template for parsing files.
|
6
6
|
Usually, parsing file process contains next steps:
|
7
7
|
|
8
8
|
1. Retreiving info from file
|
@@ -15,11 +15,75 @@ Usually, parsing file process contains next steps:
|
|
15
15
|
|
16
16
|
Cascade pretends to simplify main part of this step to save your time.
|
17
17
|
|
18
|
+
## Example
|
19
|
+
|
20
|
+
[Working Example](https://github.com/ignat-zakrevsky/cascade-example)
|
21
|
+
|
22
|
+
## Installation
|
23
|
+
Install the cascade-rb package from Rubygems:
|
24
|
+
```
|
25
|
+
gem install cascade-rb
|
26
|
+
```
|
27
|
+
|
28
|
+
or add it to your Gemfile for Bundler:
|
29
|
+
```ruby
|
30
|
+
gem 'cascade-rb'
|
31
|
+
```
|
32
|
+
|
18
33
|
## Usage
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
34
|
+
Require gem files
|
35
|
+
```ruby
|
36
|
+
require 'cascade'
|
37
|
+
```
|
38
|
+
|
39
|
+
Configure it!
|
40
|
+
```ruby
|
41
|
+
Cascade.configuration do
|
42
|
+
Cascade::RowProcessor.use_default_presenter = false # will throw exception in case of unavailable presenter
|
43
|
+
Cascade::ColumnsMatching.mapping_file = "columns_mapping.yml" # file with columns mapping, see below
|
44
|
+
end
|
45
|
+
```
|
46
|
+
|
47
|
+
Provide file for parsing and run it!
|
48
|
+
```ruby
|
49
|
+
Cascade::DataParser.new("data_test.txt").call
|
50
|
+
```
|
51
|
+
|
52
|
+
## Columns mapping
|
53
|
+
Parsing file description should have next structure [(example)](https://github.com/ignat-zakrevsky/cascade-example/blob/master/columns_mapping.yml)
|
54
|
+
```yaml
|
55
|
+
mapping:
|
56
|
+
name: type
|
57
|
+
```
|
25
58
|
|
59
|
+
## Components replaceability
|
60
|
+
There is a lot of DI in this gem, so, you can replace each component of the parser. Let's assume you want to parse JSON files instead of CSV and save this to ActiveRecord model, ok!
|
61
|
+
Writing new data provider:
|
62
|
+
```ruby
|
63
|
+
class ParserJSON
|
64
|
+
def open(file)
|
65
|
+
JSON.parse(File.read(file))["rows"]
|
66
|
+
end
|
67
|
+
end
|
68
|
+
```
|
69
|
+
Writing new data saver:
|
70
|
+
```ruby
|
71
|
+
class PersonDataSaver
|
72
|
+
def call(person_data)
|
73
|
+
Person.create!(person_data)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
```
|
77
|
+
considering that there is no much logic even better
|
78
|
+
```ruby
|
79
|
+
PERSON_SAVER = -> (person_data) { Person.create!(person_data) }
|
80
|
+
```
|
81
|
+
Provide it for data providing for data parser
|
82
|
+
```ruby
|
83
|
+
Cascade::DataParser.new("data_test.json", data_saver: PERSON_SAVER,
|
84
|
+
data_provider: ParserJSON.new).call
|
85
|
+
```
|
86
|
+
And that's all!
|
87
|
+
[Example](https://github.com/ignat-zakrevsky/cascade-example/blob/json-example/main.rb)
|
88
|
+
## Conventions
|
89
|
+
I'm fan of callable object as consequence I prefer #call methods for classes with one responsibility. [Nice video](http://www.rubytapas.com/episodes/35-Callable) that describes benefits of such approach
|
data/lib/cascade/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cascade-rb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ignat Zakrevsky
|
@@ -189,7 +189,6 @@ files:
|
|
189
189
|
- ".hound.yml"
|
190
190
|
- ".ruby-style.yml"
|
191
191
|
- Gemfile
|
192
|
-
- Gemfile.lock
|
193
192
|
- README.md
|
194
193
|
- Rakefile
|
195
194
|
- cascade.gemspec
|