comma 3.0.6 → 3.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a41ecd8c05f23d5a0cdded2eccc399525d54526f
4
+ data.tar.gz: 9ea499094bd3469e2b731e646118bb56f9d6924b
5
+ SHA512:
6
+ metadata.gz: 57cd52eada9c08da15d212f49bcb457d664ab56d01f88767f7574c0fc9d2e30c4bd7ce241b175553648d0c0810cad641ffee2e5aeebd27727a7dd1c5f2e30b33
7
+ data.tar.gz: 85657ca9f963219a41ad8b8e8cf47faee67bc8aaa5f4b7f700fcd44832874d4570af9d6c6ba556f0c4da0da8ee65d91203394a5a9150f9ca969a8c3592a5f14c
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- comma (3.0.6)
4
+ comma (3.1.0)
5
5
  activesupport (>= 3.0.0)
6
6
 
7
7
  GEM
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- comma (3.0.6)
4
+ comma (3.1.0)
5
5
  activesupport (>= 3.0.0)
6
6
 
7
7
  GEM
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- comma (3.0.6)
4
+ comma (3.1.0)
5
5
  activesupport (>= 3.0.0)
6
6
 
7
7
  GEM
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- comma (3.0.6)
4
+ comma (3.1.0)
5
5
  activesupport (>= 3.0.0)
6
6
 
7
7
  GEM
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- comma (3.0.6)
4
+ comma (3.1.0)
5
5
  activesupport (>= 3.0.0)
6
6
 
7
7
  GEM
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- comma (3.0.5)
4
+ comma (3.1.0)
5
5
  activesupport (>= 3.0.0)
6
6
 
7
7
  GEM
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- comma (3.0.6)
4
+ comma (3.1.0)
5
5
  activesupport (>= 3.0.0)
6
6
 
7
7
  GEM
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- comma (3.0.6)
4
+ comma (3.1.0)
5
5
  activesupport (>= 3.0.0)
6
6
 
7
7
  GEM
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- comma (3.0.6)
4
+ comma (3.1.0)
5
5
  activesupport (>= 3.0.0)
6
6
 
7
7
  GEM
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- comma (3.0.6)
4
+ comma (3.1.0)
5
5
  activesupport (>= 3.0.0)
6
6
 
7
7
  GEM
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- comma (3.0.5)
4
+ comma (3.1.0)
5
5
  activesupport (>= 3.0.0)
6
6
 
7
7
  GEM
@@ -1,15 +1,18 @@
1
+ require 'active_support/core_ext/class/attribute'
2
+
1
3
  module Comma
2
4
 
3
5
  class Extractor
4
6
 
5
- def initialize(instance, &block)
7
+ def initialize(instance, style, formats)
6
8
  @instance = instance
7
- @block = block
9
+ @style = style
10
+ @formats = formats
8
11
  @results = []
9
12
  end
10
13
 
11
14
  def results
12
- instance_eval &@block
15
+ instance_eval &@formats[@style]
13
16
  @results.map { |r| convert_to_data_value(r) }
14
17
  end
15
18
 
@@ -17,6 +20,11 @@ module Comma
17
20
  method_missing(:id, *args, &block)
18
21
  end
19
22
 
23
+ def __use__(style)
24
+ # TODO: prevent infinite recursion
25
+ instance_eval(&@formats[style])
26
+ end
27
+
20
28
  private
21
29
 
22
30
  def convert_to_data_value(result)
@@ -27,18 +35,26 @@ module Comma
27
35
 
28
36
  class HeaderExtractor < Extractor
29
37
 
38
+ class_attribute :value_humanizer
39
+
40
+ DEFAULT_VALUE_HUMANIZER = lambda do |value, model_class|
41
+ value.is_a?(String) ? value : value.to_s.humanize
42
+ end
43
+ self.value_humanizer = DEFAULT_VALUE_HUMANIZER
44
+
30
45
  def method_missing(sym, *args, &block)
31
- @results << sym.to_s.humanize if args.blank?
46
+ model_class = @instance.class
47
+ @results << self.value_humanizer.call(sym, model_class) if args.blank?
32
48
  args.each do |arg|
33
49
  case arg
34
50
  when Hash
35
51
  arg.each do |k, v|
36
- @results << ((v.is_a? String) ? v : v.to_s.humanize)
52
+ @results << self.value_humanizer.call(v, get_association_class(model_class, sym))
37
53
  end
38
54
  when Symbol
39
- @results << arg.to_s.humanize
55
+ @results << self.value_humanizer.call(arg, get_association_class(model_class, sym))
40
56
  when String
41
- @results << arg
57
+ @results << self.value_humanizer.call(arg, model_class)
42
58
  else
43
59
  raise "Unknown header symbol #{arg.inspect}"
44
60
  end
@@ -48,6 +64,13 @@ module Comma
48
64
  def __static_column__(header = '', &block)
49
65
  @results << header
50
66
  end
67
+
68
+ private
69
+
70
+ def get_association_class(model_class, association)
71
+ model_class.respond_to?(:reflect_on_association) ?
72
+ model_class.reflect_on_association(arg).klass : nil
73
+ end
51
74
  end
52
75
 
53
76
  class DataExtractor < Extractor
@@ -80,7 +103,7 @@ module Comma
80
103
  end
81
104
 
82
105
  def __static_column__(header = nil, &block)
83
- @results << (block ? yield : nil)
106
+ @results << (block ? yield(@instance) : nil)
84
107
  end
85
108
 
86
109
  private
data/lib/comma/object.rb CHANGED
@@ -7,11 +7,11 @@ class Object
7
7
 
8
8
  def to_comma(style = :default)
9
9
  raise "No comma format for class #{self.class} defined for style #{style}" unless self.comma_formats and self.comma_formats[style]
10
- Comma::DataExtractor.new(self, &self.comma_formats[style]).results
10
+ Comma::DataExtractor.new(self, style, self.class.comma_formats).results
11
11
  end
12
12
 
13
13
  def to_comma_headers(style = :default)
14
14
  raise "No comma format for class #{self.class} defined for style #{style}" unless self.comma_formats and self.comma_formats[style]
15
- Comma::HeaderExtractor.new(self, &self.comma_formats[style]).results
15
+ Comma::HeaderExtractor.new(self, style, self.class.comma_formats).results
16
16
  end
17
17
  end
data/lib/comma/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Comma
2
- VERSION = "3.0.6"
2
+ VERSION = "3.1.0"
3
3
  end
@@ -225,3 +225,27 @@ describe Comma, 'to_comma data/headers object extensions' do
225
225
  end
226
226
 
227
227
  end
228
+
229
+ describe Comma, '__use__ keyword' do
230
+ before(:all) do
231
+ @obj = Class.new(Struct.new(:id, :title, :description)) do
232
+ comma do
233
+ title
234
+ __use__ :description
235
+ end
236
+
237
+ comma :description do
238
+ __use__ :static
239
+ description
240
+ end
241
+
242
+ comma :static do
243
+ __static_column__ do 'Foo, Inc.' end
244
+ end
245
+ end.new(1, 'Programming Ruby', 'The Pickaxe book')
246
+ end
247
+
248
+ subject { @obj.to_comma }
249
+ its(:size) { should eq(3) }
250
+ it { should eq(['Programming Ruby', 'Foo, Inc.', 'The Pickaxe book']) }
251
+ end
@@ -127,12 +127,13 @@ describe Comma::DataExtractor, 'with static column method' do
127
127
  __static_column__
128
128
  __static_column__ 'STATIC'
129
129
  __static_column__ 'STATIC' do '' end
130
+ __static_column__ 'STATIC' do |o| o.name end
130
131
  end
131
132
  end.new(1, 'John Doe').to_comma
132
133
  end
133
134
 
134
135
  it 'should extract headers' do
135
- @data.should eq([nil, nil, ''])
136
+ @data.should eq([nil, nil, '', 'John Doe'])
136
137
  end
137
138
  end
138
139
 
@@ -1,3 +1,4 @@
1
+ # -*- coding: utf-8 -*-
1
2
  require 'spec_helper'
2
3
 
3
4
  describe Comma, 'generating CSV from an ActiveRecord object' do
@@ -35,4 +36,34 @@ describe Comma, 'generating CSV from an ActiveRecord object' do
35
36
  Person.teenagers.to_comma
36
37
  end
37
38
  end
39
+
40
+ describe 'with custom value_humanizer' do
41
+ before do
42
+ Comma::HeaderExtractor.value_humanizer =
43
+ lambda do |value, model_class|
44
+ if model_class.respond_to?(:human_attribute_name)
45
+ model_class.human_attribute_name(value)
46
+ else
47
+ value.is_a?(String) ? value : value.to_s.humanize
48
+ end
49
+ end
50
+
51
+ I18n.config.backend.store_translations(:ja, {:activerecord => {:attributes => {:person => {:age => '年齢', :name => '名前'}}}})
52
+ @original_locale = I18n.locale
53
+ I18n.locale = :ja
54
+
55
+ Person.create(:age => 19, :name => 'John')
56
+ end
57
+
58
+ after do
59
+ I18n.locale = @original_locale
60
+
61
+ Comma::HeaderExtractor.value_humanizer =
62
+ Comma::HeaderExtractor::DEFAULT_VALUE_HUMANIZER
63
+ end
64
+
65
+ it 'should i18n-ize header values' do
66
+ Person.teenagers.to_comma.should match(/^名前,年齢/)
67
+ end
68
+ end
38
69
  end
metadata CHANGED
@@ -1,128 +1,108 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: comma
3
- version: !ruby/object:Gem::Version
4
- hash: 11
5
- prerelease:
6
- segments:
7
- - 3
8
- - 0
9
- - 6
10
- version: 3.0.6
3
+ version: !ruby/object:Gem::Version
4
+ version: 3.1.0
11
5
  platform: ruby
12
- authors:
6
+ authors:
13
7
  - Marcus Crafter
14
8
  - Tom Meier
15
9
  autorequire:
16
10
  bindir: bin
17
11
  cert_chain: []
18
-
19
- date: 2013-08-15 00:00:00 Z
20
- dependencies:
21
- - !ruby/object:Gem::Dependency
12
+ date: 2013-08-17 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
22
15
  name: activesupport
23
- prerelease: false
24
- requirement: &id001 !ruby/object:Gem::Requirement
25
- none: false
26
- requirements:
27
- - - ">="
28
- - !ruby/object:Gem::Version
29
- hash: 7
30
- segments:
31
- - 3
32
- - 0
33
- - 0
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - '>='
19
+ - !ruby/object:Gem::Version
34
20
  version: 3.0.0
35
21
  type: :runtime
36
- version_requirements: *id001
37
- - !ruby/object:Gem::Dependency
38
- name: rake
39
22
  prerelease: false
40
- requirement: &id002 !ruby/object:Gem::Requirement
41
- none: false
42
- requirements:
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - '>='
26
+ - !ruby/object:Gem::Version
27
+ version: 3.0.0
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
43
32
  - - ~>
44
- - !ruby/object:Gem::Version
45
- hash: 63
46
- segments:
47
- - 0
48
- - 9
49
- - 2
33
+ - !ruby/object:Gem::Version
50
34
  version: 0.9.2
51
35
  type: :development
52
- version_requirements: *id002
53
- - !ruby/object:Gem::Dependency
54
- name: sqlite3
55
36
  prerelease: false
56
- requirement: &id003 !ruby/object:Gem::Requirement
57
- none: false
58
- requirements:
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
59
39
  - - ~>
60
- - !ruby/object:Gem::Version
61
- hash: 19
62
- segments:
63
- - 1
64
- - 3
65
- - 4
40
+ - !ruby/object:Gem::Version
41
+ version: 0.9.2
42
+ - !ruby/object:Gem::Dependency
43
+ name: sqlite3
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ~>
47
+ - !ruby/object:Gem::Version
66
48
  version: 1.3.4
67
49
  type: :development
68
- version_requirements: *id003
69
- - !ruby/object:Gem::Dependency
70
- name: appraisal
71
50
  prerelease: false
72
- requirement: &id004 !ruby/object:Gem::Requirement
73
- none: false
74
- requirements:
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
75
53
  - - ~>
76
- - !ruby/object:Gem::Version
77
- hash: 13
78
- segments:
79
- - 0
80
- - 4
81
- - 1
54
+ - !ruby/object:Gem::Version
55
+ version: 1.3.4
56
+ - !ruby/object:Gem::Dependency
57
+ name: appraisal
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ~>
61
+ - !ruby/object:Gem::Version
82
62
  version: 0.4.1
83
63
  type: :development
84
- version_requirements: *id004
85
- - !ruby/object:Gem::Dependency
86
- name: rspec
87
64
  prerelease: false
88
- requirement: &id005 !ruby/object:Gem::Requirement
89
- none: false
90
- requirements:
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 0.4.1
70
+ - !ruby/object:Gem::Dependency
71
+ name: rspec
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
91
74
  - - ~>
92
- - !ruby/object:Gem::Version
93
- hash: 47
94
- segments:
95
- - 2
96
- - 8
97
- - 0
75
+ - !ruby/object:Gem::Version
98
76
  version: 2.8.0
99
77
  type: :development
100
- version_requirements: *id005
101
- - !ruby/object:Gem::Dependency
102
- name: simplecov
103
78
  prerelease: false
104
- requirement: &id006 !ruby/object:Gem::Requirement
105
- none: false
106
- requirements:
107
- - - ">="
108
- - !ruby/object:Gem::Version
109
- hash: 3
110
- segments:
111
- - 0
112
- version: "0"
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ~>
82
+ - !ruby/object:Gem::Version
83
+ version: 2.8.0
84
+ - !ruby/object:Gem::Dependency
85
+ name: simplecov
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
113
91
  type: :development
114
- version_requirements: *id006
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - '>='
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
115
98
  description: Ruby Comma Seperated Values generation library
116
- email:
99
+ email:
117
100
  - crafterm@redartisan.com
118
101
  - tom@venombytes.com
119
102
  executables: []
120
-
121
103
  extensions: []
122
-
123
104
  extra_rdoc_files: []
124
-
125
- files:
105
+ files:
126
106
  - .gitignore
127
107
  - .travis.yml
128
108
  - Appraisals
@@ -196,39 +176,30 @@ files:
196
176
  - spec/spec.opts
197
177
  - spec/spec_helper.rb
198
178
  homepage: http://github.com/crafterm/comma
199
- licenses:
179
+ licenses:
200
180
  - MIT
181
+ metadata: {}
201
182
  post_install_message:
202
183
  rdoc_options: []
203
-
204
- require_paths:
184
+ require_paths:
205
185
  - lib
206
- required_ruby_version: !ruby/object:Gem::Requirement
207
- none: false
208
- requirements:
209
- - - ">="
210
- - !ruby/object:Gem::Version
211
- hash: 3
212
- segments:
213
- - 0
214
- version: "0"
215
- required_rubygems_version: !ruby/object:Gem::Requirement
216
- none: false
217
- requirements:
218
- - - ">="
219
- - !ruby/object:Gem::Version
220
- hash: 3
221
- segments:
222
- - 0
223
- version: "0"
186
+ required_ruby_version: !ruby/object:Gem::Requirement
187
+ requirements:
188
+ - - '>='
189
+ - !ruby/object:Gem::Version
190
+ version: '0'
191
+ required_rubygems_version: !ruby/object:Gem::Requirement
192
+ requirements:
193
+ - - '>='
194
+ - !ruby/object:Gem::Version
195
+ version: '0'
224
196
  requirements: []
225
-
226
197
  rubyforge_project: comma
227
- rubygems_version: 1.8.24
198
+ rubygems_version: 2.0.3
228
199
  signing_key:
229
- specification_version: 3
200
+ specification_version: 4
230
201
  summary: Ruby Comma Seperated Values generation library
231
- test_files:
202
+ test_files:
232
203
  - spec/comma/comma_spec.rb
233
204
  - spec/comma/extractors_spec.rb
234
205
  - spec/comma/rails/active_record_spec.rb
@@ -263,4 +234,3 @@ test_files:
263
234
  - spec/rails_app/script/rails
264
235
  - spec/spec.opts
265
236
  - spec/spec_helper.rb
266
- has_rdoc: