julia_builder 0.1.2 → 0.1.3
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 +32 -24
- data/julia.gemspec +1 -1
- data/lib/julia/action.rb +18 -0
- data/lib/julia/builder.rb +4 -10
- data/lib/julia/version.rb +1 -1
- data/lib/julia.rb +1 -0
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 80ca945894a0a74b6723e1fd75350bceb5b9e612
|
4
|
+
data.tar.gz: 53d1c573ec4c9769eed995b8a62df974e9c12f00
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a1a4e15d9ab6a532a00793cd68a543212d6ae402ccddab15d7ccb6d3ec7cd257d270fc03426b4900f0ed49e6e926c041581794a742b2d1b54bb8e649bfb159ca
|
7
|
+
data.tar.gz: 6edd408e3c2053c71c3ab854bb2f0772ed84ab4c9371a0da701843b96cb944e781de733822896e83db4ba7b408f6b083895e09997bf41075ef098e0149232fab
|
data/README.md
CHANGED
@@ -27,33 +27,42 @@ require 'julia'
|
|
27
27
|
|
28
28
|
## Usage
|
29
29
|
|
30
|
-
1
|
30
|
+
1. Create your own builder class, inherit from `Julia::Builder` and configure your csv columns.
|
31
31
|
|
32
|
-
```ruby
|
33
|
-
class UserCsv < Julia::Builder
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
# when header and value are the same, no need to duplicate it.
|
39
|
-
column :name
|
40
|
-
# header equals 'name', value will be `user.name`
|
41
|
-
|
42
|
-
# when you need to do some extra work on the value you can pass a proc.
|
43
|
-
column 'Full name', -> { "#{ name.capitalize } #{ last_name.capitalize }" }
|
44
|
-
end
|
45
|
-
```
|
32
|
+
```ruby
|
33
|
+
class UserCsv < Julia::Builder
|
34
|
+
# specify column's header and value
|
35
|
+
column 'Birthday', :dob
|
36
|
+
# header equals 'Birthday' and the value will be on `user.dbo`
|
46
37
|
|
47
|
-
|
38
|
+
# when header and value are the same, no need to duplicate it.
|
39
|
+
column :name
|
40
|
+
# header equals 'name', value will be `user.name`
|
48
41
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
42
|
+
# when you need to do some extra work on the value you can pass a proc.
|
43
|
+
column 'Full name', -> { "#{ name.capitalize } #{ last_name.capitalize }" }
|
44
|
+
|
45
|
+
# or you can pass a block
|
46
|
+
column 'Type' do |user|
|
47
|
+
user.class.name
|
48
|
+
end
|
49
|
+
end
|
50
|
+
```
|
53
51
|
|
54
|
-
|
52
|
+
2. Now you can use your builder to generate your csv out of a query like:
|
55
53
|
|
56
|
-
|
54
|
+
```ruby
|
55
|
+
users = User.all
|
56
|
+
UserCsv.new(users).build
|
57
|
+
|
58
|
+
# or
|
59
|
+
|
60
|
+
UserCsv.new(users, <csv options>).build
|
61
|
+
```
|
62
|
+
|
63
|
+
Csv options could be anything [CSV::new](http://ruby-doc.org/stdlib-2.0.0/libdoc/csv/rdoc/CSV.html#method-c-new) understands, but they are optional.
|
64
|
+
|
65
|
+
3. Enjoy
|
57
66
|
|
58
67
|
## Development
|
59
68
|
|
@@ -63,11 +72,10 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
63
72
|
|
64
73
|
## Contributing
|
65
74
|
|
66
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/stevenbarragan/julia_builder. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
|
75
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/stevenbarragan/julia_builder/issues. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org/) code of conduct.
|
67
76
|
|
68
77
|
|
69
78
|
## License
|
70
79
|
|
71
80
|
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
72
81
|
|
73
|
-
|
data/julia.gemspec
CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
|
|
10
10
|
spec.email = ["me@steven.mx"]
|
11
11
|
|
12
12
|
spec.summary = %q{Export your queries easily and fast}
|
13
|
-
spec.description = %q{Create flexible builders to export your queries
|
13
|
+
spec.description = %q{Create flexible builders to export your queries easier than ever}
|
14
14
|
spec.homepage = "https://github.com/stevenbarragan/julia_builder"
|
15
15
|
spec.license = "MIT"
|
16
16
|
|
data/lib/julia/action.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
module Julia
|
2
|
+
class Action
|
3
|
+
attr_reader :key, :action, :block
|
4
|
+
|
5
|
+
def initialize(key, action, &block)
|
6
|
+
@key = key
|
7
|
+
@action = action
|
8
|
+
@block = block
|
9
|
+
end
|
10
|
+
|
11
|
+
def get_value(record)
|
12
|
+
return block.call(record) if block
|
13
|
+
return record.instance_exec(&action) if action.is_a? Proc
|
14
|
+
|
15
|
+
record.send [action, key].compact.first
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
data/lib/julia/builder.rb
CHANGED
@@ -12,8 +12,8 @@ module Julia
|
|
12
12
|
@columns ||= {}
|
13
13
|
end
|
14
14
|
|
15
|
-
def self.column(keyname, action = nil)
|
16
|
-
self.columns[keyname] = action
|
15
|
+
def self.column(keyname, action = nil, &block)
|
16
|
+
self.columns[keyname] = Action.new(keyname, action, &block)
|
17
17
|
end
|
18
18
|
|
19
19
|
def build
|
@@ -21,8 +21,8 @@ module Julia
|
|
21
21
|
csv << columns.keys
|
22
22
|
|
23
23
|
collection.each do |record|
|
24
|
-
csv << columns.map do |
|
25
|
-
get_value(record
|
24
|
+
csv << columns.values.map do |action|
|
25
|
+
action.get_value(record)
|
26
26
|
end
|
27
27
|
end
|
28
28
|
end
|
@@ -30,12 +30,6 @@ module Julia
|
|
30
30
|
|
31
31
|
protected
|
32
32
|
|
33
|
-
def get_value(record, key, action)
|
34
|
-
return record.instance_exec(&action) if action.is_a?(Proc)
|
35
|
-
|
36
|
-
record.send([action, key].compact.first)
|
37
|
-
end
|
38
|
-
|
39
33
|
def columns
|
40
34
|
self.class.columns
|
41
35
|
end
|
data/lib/julia/version.rb
CHANGED
data/lib/julia.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: julia_builder
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Steven Barragán
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-11-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -52,7 +52,7 @@ dependencies:
|
|
52
52
|
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
|
-
description: Create flexible builders to export your queries
|
55
|
+
description: Create flexible builders to export your queries easier than ever
|
56
56
|
email:
|
57
57
|
- me@steven.mx
|
58
58
|
executables: []
|
@@ -72,6 +72,7 @@ files:
|
|
72
72
|
- bin/setup
|
73
73
|
- julia.gemspec
|
74
74
|
- lib/julia.rb
|
75
|
+
- lib/julia/action.rb
|
75
76
|
- lib/julia/builder.rb
|
76
77
|
- lib/julia/version.rb
|
77
78
|
- lib/julia_builder.rb
|