julia_builder 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3a2de7c2e13d9b6cb667b6affe5d171a07285107
4
- data.tar.gz: 298fc6f95a0c6389c8645dc155a92e1088bacb51
3
+ metadata.gz: 80ca945894a0a74b6723e1fd75350bceb5b9e612
4
+ data.tar.gz: 53d1c573ec4c9769eed995b8a62df974e9c12f00
5
5
  SHA512:
6
- metadata.gz: aee7e124cc61f466ad0f506c0c650a391f798b7659f7e8efde4b3b48bdc0866e99f6e9c4242e2397ee6255dd7c8c61797eec8ce7f7ff5a9c3bbfb50882075578
7
- data.tar.gz: 60fd6cd0a169b1dbf188ef27c8cb44e3cb4cb05cd40399b33616ba7cf3a12570ec3cebc623820fd7df9e4210807e753f4ea4c81e94f13a58cf9e17131df813de
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.- Create your own builder class, inherit from `Julia::Builder` and configure your csv columns.
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
- # specify column's header and value
35
- column 'Birthday', :dob
36
- # header equals 'Birthday' and the value will be on `user.dbo`
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
- 2.- Now you can use your builder to generate your csv out of a query like:
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
- ```ruby
50
- users = User.all
51
- UserCsv.new(users).build(<csv options>)
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
- Csv opions 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.
52
+ 2. Now you can use your builder to generate your csv out of a query like:
55
53
 
56
- 3.- Enjoy
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 easy than ever}
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
 
@@ -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 |key, action|
25
- get_value(record, key, action)
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
@@ -1,3 +1,3 @@
1
1
  module Julia
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
data/lib/julia.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require "julia/version"
2
2
  require 'julia/builder'
3
+ require 'julia/action'
3
4
 
4
5
  module Julia
5
6
  end
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.2
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-10-26 00:00:00.000000000 Z
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 easy than ever
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