comma 3.1.1 → 3.2.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.
- data/Appraisals +6 -1
- data/Gemfile.lock +1 -1
- data/README.markdown +42 -2
- data/gemfiles/{active3.2.11.gemfile → active3.0.20.gemfile} +2 -2
- data/gemfiles/{active3.0.9.gemfile.lock → active3.0.20.gemfile.lock} +17 -17
- data/gemfiles/active3.1.12.gemfile.lock +1 -1
- data/gemfiles/{active3.0.9.gemfile → active3.2.14.gemfile} +2 -2
- data/gemfiles/{active3.2.11.gemfile.lock → active3.2.14.gemfile.lock} +14 -14
- data/gemfiles/active4.0.0.gemfile.lock +1 -1
- data/gemfiles/{rails3.0.9.gemfile → data_mapper1.2.0.gemfile} +2 -2
- data/gemfiles/data_mapper1.2.0.gemfile.lock +101 -0
- data/gemfiles/mongoid3.1.4.gemfile.lock +2 -2
- data/gemfiles/{rails3.1.1.gemfile → rails3.0.20.gemfile} +1 -1
- data/gemfiles/{rails3.0.9.gemfile.lock → rails3.0.20.gemfile.lock} +41 -41
- data/gemfiles/rails3.1.12.gemfile.lock +1 -1
- data/gemfiles/{rails3.2.11.gemfile → rails3.2.14.gemfile} +1 -1
- data/gemfiles/{rails3.2.11.gemfile.lock → rails3.2.14.gemfile.lock} +42 -43
- data/gemfiles/rails4.0.0.gemfile.lock +1 -1
- data/lib/comma.rb +3 -1
- data/lib/comma/data_extractor.rb +78 -0
- data/lib/comma/data_mapper_collection.rb +7 -0
- data/lib/comma/extractor.rb +36 -0
- data/lib/comma/header_extractor.rb +48 -0
- data/lib/comma/object.rb +4 -0
- data/lib/comma/version.rb +1 -1
- data/spec/comma/{extractors_spec.rb → data_extractor_spec.rb} +2 -60
- data/spec/comma/header_extractor_spec.rb +68 -0
- data/spec/comma/rails/data_mapper_collection_spec.rb +49 -0
- metadata +138 -102
- checksums.yaml +0 -15
- data/gemfiles/active3.1.1.gemfile +0 -9
- data/gemfiles/active3.1.1.gemfile.lock +0 -58
- data/gemfiles/rails3.1.1.gemfile.lock +0 -124
- data/lib/comma/extractors.rb +0 -131
@@ -1,38 +1,38 @@
|
|
1
1
|
PATH
|
2
2
|
remote: ..
|
3
3
|
specs:
|
4
|
-
comma (3.
|
4
|
+
comma (3.2.0)
|
5
5
|
activesupport (>= 3.0.0)
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: http://rubygems.org/
|
9
9
|
specs:
|
10
|
-
actionmailer (3.2.
|
11
|
-
actionpack (= 3.2.
|
12
|
-
mail (~> 2.
|
13
|
-
actionpack (3.2.
|
14
|
-
activemodel (= 3.2.
|
15
|
-
activesupport (= 3.2.
|
10
|
+
actionmailer (3.2.14)
|
11
|
+
actionpack (= 3.2.14)
|
12
|
+
mail (~> 2.5.4)
|
13
|
+
actionpack (3.2.14)
|
14
|
+
activemodel (= 3.2.14)
|
15
|
+
activesupport (= 3.2.14)
|
16
16
|
builder (~> 3.0.0)
|
17
17
|
erubis (~> 2.7.0)
|
18
18
|
journey (~> 1.0.4)
|
19
|
-
rack (~> 1.4.
|
19
|
+
rack (~> 1.4.5)
|
20
20
|
rack-cache (~> 1.2)
|
21
21
|
rack-test (~> 0.6.1)
|
22
22
|
sprockets (~> 2.2.1)
|
23
|
-
activemodel (3.2.
|
24
|
-
activesupport (= 3.2.
|
23
|
+
activemodel (3.2.14)
|
24
|
+
activesupport (= 3.2.14)
|
25
25
|
builder (~> 3.0.0)
|
26
|
-
activerecord (3.2.
|
27
|
-
activemodel (= 3.2.
|
28
|
-
activesupport (= 3.2.
|
26
|
+
activerecord (3.2.14)
|
27
|
+
activemodel (= 3.2.14)
|
28
|
+
activesupport (= 3.2.14)
|
29
29
|
arel (~> 3.0.2)
|
30
30
|
tzinfo (~> 0.3.29)
|
31
|
-
activeresource (3.2.
|
32
|
-
activemodel (= 3.2.
|
33
|
-
activesupport (= 3.2.
|
34
|
-
activesupport (3.2.
|
35
|
-
i18n (~> 0.6)
|
31
|
+
activeresource (3.2.14)
|
32
|
+
activemodel (= 3.2.14)
|
33
|
+
activesupport (= 3.2.14)
|
34
|
+
activesupport (3.2.14)
|
35
|
+
i18n (~> 0.6, >= 0.6.4)
|
36
36
|
multi_json (~> 1.0)
|
37
37
|
appraisal (0.4.1)
|
38
38
|
bundler
|
@@ -42,41 +42,40 @@ GEM
|
|
42
42
|
diff-lcs (1.1.3)
|
43
43
|
erubis (2.7.0)
|
44
44
|
fastercsv (1.5.5)
|
45
|
-
hike (1.2.
|
46
|
-
i18n (0.6.
|
45
|
+
hike (1.2.3)
|
46
|
+
i18n (0.6.5)
|
47
47
|
journey (1.0.4)
|
48
|
-
json (1.
|
49
|
-
mail (2.
|
50
|
-
i18n (>= 0.4.0)
|
48
|
+
json (1.8.0)
|
49
|
+
mail (2.5.4)
|
51
50
|
mime-types (~> 1.16)
|
52
51
|
treetop (~> 1.4.8)
|
53
|
-
mime-types (1.
|
54
|
-
multi_json (1.
|
52
|
+
mime-types (1.25)
|
53
|
+
multi_json (1.7.9)
|
55
54
|
polyglot (0.3.3)
|
56
|
-
rack (1.4.
|
55
|
+
rack (1.4.5)
|
57
56
|
rack-cache (1.2)
|
58
57
|
rack (>= 0.4)
|
59
58
|
rack-ssl (1.3.3)
|
60
59
|
rack
|
61
60
|
rack-test (0.6.2)
|
62
61
|
rack (>= 1.0)
|
63
|
-
rails (3.2.
|
64
|
-
actionmailer (= 3.2.
|
65
|
-
actionpack (= 3.2.
|
66
|
-
activerecord (= 3.2.
|
67
|
-
activeresource (= 3.2.
|
68
|
-
activesupport (= 3.2.
|
62
|
+
rails (3.2.14)
|
63
|
+
actionmailer (= 3.2.14)
|
64
|
+
actionpack (= 3.2.14)
|
65
|
+
activerecord (= 3.2.14)
|
66
|
+
activeresource (= 3.2.14)
|
67
|
+
activesupport (= 3.2.14)
|
69
68
|
bundler (~> 1.0)
|
70
|
-
railties (= 3.2.
|
71
|
-
railties (3.2.
|
72
|
-
actionpack (= 3.2.
|
73
|
-
activesupport (= 3.2.
|
69
|
+
railties (= 3.2.14)
|
70
|
+
railties (3.2.14)
|
71
|
+
actionpack (= 3.2.14)
|
72
|
+
activesupport (= 3.2.14)
|
74
73
|
rack-ssl (~> 1.3.2)
|
75
74
|
rake (>= 0.8.7)
|
76
75
|
rdoc (~> 3.4)
|
77
76
|
thor (>= 0.14.6, < 2.0)
|
78
77
|
rake (0.9.6)
|
79
|
-
rdoc (3.12)
|
78
|
+
rdoc (3.12.2)
|
80
79
|
json (~> 1.4)
|
81
80
|
rspec (2.8.0)
|
82
81
|
rspec-core (~> 2.8.0)
|
@@ -100,13 +99,13 @@ GEM
|
|
100
99
|
multi_json (~> 1.0)
|
101
100
|
rack (~> 1.0)
|
102
101
|
tilt (~> 1.1, != 1.3.0)
|
103
|
-
sqlite3 (1.3.
|
104
|
-
thor (0.
|
105
|
-
tilt (1.
|
106
|
-
treetop (1.4.
|
102
|
+
sqlite3 (1.3.8)
|
103
|
+
thor (0.18.1)
|
104
|
+
tilt (1.4.1)
|
105
|
+
treetop (1.4.15)
|
107
106
|
polyglot
|
108
107
|
polyglot (>= 0.3.1)
|
109
|
-
tzinfo (0.3.
|
108
|
+
tzinfo (0.3.37)
|
110
109
|
|
111
110
|
PLATFORMS
|
112
111
|
ruby
|
@@ -115,7 +114,7 @@ DEPENDENCIES
|
|
115
114
|
appraisal (~> 0.4.1)
|
116
115
|
comma!
|
117
116
|
fastercsv
|
118
|
-
rails (= 3.2.
|
117
|
+
rails (= 3.2.14)
|
119
118
|
rake (~> 0.9.2)
|
120
119
|
rspec (~> 2.8.0)
|
121
120
|
rspec-rails
|
data/lib/comma.rb
CHANGED
@@ -28,11 +28,13 @@ require 'active_support/lazy_load_hooks'
|
|
28
28
|
ActiveSupport.on_load(:active_record) do
|
29
29
|
require 'comma/relation' if defined?(ActiveRecord::Relation)
|
30
30
|
end
|
31
|
+
|
31
32
|
ActiveSupport.on_load(:mongoid) do
|
32
33
|
require 'comma/mongoid'
|
33
34
|
end
|
34
35
|
|
35
|
-
require 'comma/
|
36
|
+
require 'comma/data_mapper_collection' if defined? DataMapper
|
37
|
+
|
36
38
|
require 'comma/generator'
|
37
39
|
require 'comma/array'
|
38
40
|
require 'comma/object'
|
@@ -0,0 +1,78 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
require 'comma/extractor'
|
3
|
+
|
4
|
+
module Comma
|
5
|
+
|
6
|
+
class DataExtractor < Extractor
|
7
|
+
|
8
|
+
class ExtractValueFromInstance
|
9
|
+
def initialize(instance)
|
10
|
+
@instance = instance
|
11
|
+
end
|
12
|
+
|
13
|
+
def extract(sym, &block)
|
14
|
+
yield_block_with_value(extract_value(sym), &block)
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def yield_block_with_value(value, &block)
|
20
|
+
block ? yield(value) : value
|
21
|
+
end
|
22
|
+
|
23
|
+
def extract_value(method)
|
24
|
+
extraction_object.send(method)
|
25
|
+
end
|
26
|
+
|
27
|
+
def extraction_object
|
28
|
+
@instance
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
class ExtractValueFromAssociationOfInstance < ExtractValueFromInstance
|
33
|
+
def initialize(instance, association_name)
|
34
|
+
super(instance)
|
35
|
+
@association_name = association_name
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
def extraction_object
|
41
|
+
@instance.send(@association_name) || null_association
|
42
|
+
end
|
43
|
+
|
44
|
+
def null_association
|
45
|
+
@null_association ||= Class.new(Class.const_defined?(:BasicObject) ? ::BasicObject : ::Object) do
|
46
|
+
def method_missing(symbol, *args, &block)
|
47
|
+
nil
|
48
|
+
end
|
49
|
+
end.new
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def method_missing(sym, *args, &block)
|
54
|
+
if args.blank?
|
55
|
+
@results << ExtractValueFromInstance.new(@instance).extract(sym, &block)
|
56
|
+
end
|
57
|
+
|
58
|
+
args.each do |arg|
|
59
|
+
case arg
|
60
|
+
when Hash
|
61
|
+
arg.each do |k, v|
|
62
|
+
@results << ExtractValueFromAssociationOfInstance.new(@instance, sym).extract(k, &block)
|
63
|
+
end
|
64
|
+
when Symbol
|
65
|
+
@results << ExtractValueFromAssociationOfInstance.new(@instance, sym).extract(arg, &block)
|
66
|
+
when String
|
67
|
+
@results << ExtractValueFromInstance.new(@instance).extract(sym, &block)
|
68
|
+
else
|
69
|
+
raise "Unknown data symbol #{arg.inspect}"
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
def __static_column__(header = nil, &block)
|
75
|
+
@results << (block ? yield(@instance) : nil)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
module Comma
|
4
|
+
|
5
|
+
class Extractor
|
6
|
+
|
7
|
+
def initialize(instance, style, formats)
|
8
|
+
@instance = instance
|
9
|
+
@style = style
|
10
|
+
@formats = formats
|
11
|
+
@results = []
|
12
|
+
end
|
13
|
+
|
14
|
+
def results
|
15
|
+
instance_eval &@formats[@style]
|
16
|
+
@results.map { |r| convert_to_data_value(r) }
|
17
|
+
end
|
18
|
+
|
19
|
+
def id(*args, &block)
|
20
|
+
method_missing(:id, *args, &block)
|
21
|
+
end
|
22
|
+
|
23
|
+
def __use__(style)
|
24
|
+
# TODO: prevent infinite recursion
|
25
|
+
instance_eval(&@formats[style])
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def convert_to_data_value(result)
|
31
|
+
result.nil? ? result : result.to_s
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
require 'comma/extractor'
|
3
|
+
require 'active_support/core_ext/class/attribute'
|
4
|
+
require 'active_support/core_ext/date_time/conversions'
|
5
|
+
require 'active_support/core_ext/object/blank'
|
6
|
+
require 'active_support/core_ext/string/inflections'
|
7
|
+
|
8
|
+
module Comma
|
9
|
+
class HeaderExtractor < Extractor
|
10
|
+
|
11
|
+
class_attribute :value_humanizer
|
12
|
+
|
13
|
+
DEFAULT_VALUE_HUMANIZER = lambda do |value, model_class|
|
14
|
+
value.is_a?(String) ? value : value.to_s.humanize
|
15
|
+
end
|
16
|
+
self.value_humanizer = DEFAULT_VALUE_HUMANIZER
|
17
|
+
|
18
|
+
def method_missing(sym, *args, &block)
|
19
|
+
model_class = @instance.class
|
20
|
+
@results << self.value_humanizer.call(sym, model_class) if args.blank?
|
21
|
+
args.each do |arg|
|
22
|
+
case arg
|
23
|
+
when Hash
|
24
|
+
arg.each do |k, v|
|
25
|
+
@results << self.value_humanizer.call(v, get_association_class(model_class, sym))
|
26
|
+
end
|
27
|
+
when Symbol
|
28
|
+
@results << self.value_humanizer.call(arg, get_association_class(model_class, sym))
|
29
|
+
when String
|
30
|
+
@results << self.value_humanizer.call(arg, model_class)
|
31
|
+
else
|
32
|
+
raise "Unknown header symbol #{arg.inspect}"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def __static_column__(header = '', &block)
|
38
|
+
@results << header
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
def get_association_class(model_class, association)
|
44
|
+
model_class.respond_to?(:reflect_on_association) ?
|
45
|
+
model_class.reflect_on_association(association).klass : nil
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
data/lib/comma/object.rb
CHANGED
data/lib/comma/version.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
|
-
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
require 'spec_helper'
|
2
3
|
|
3
4
|
# comma do
|
4
5
|
# name 'Title'
|
@@ -7,65 +8,6 @@ require File.dirname(__FILE__) + '/../spec_helper'
|
|
7
8
|
# isbn :number_10 => 'ISBN-10', :number_13 => 'ISBN-13'
|
8
9
|
# end
|
9
10
|
|
10
|
-
describe Comma::HeaderExtractor do
|
11
|
-
|
12
|
-
before do
|
13
|
-
@isbn = Isbn.new('123123123', '321321321')
|
14
|
-
@book = Book.new('Smalltalk-80', 'Language and Implementation', @isbn)
|
15
|
-
|
16
|
-
@headers = @book.to_comma_headers
|
17
|
-
end
|
18
|
-
|
19
|
-
describe 'when no parameters are provided' do
|
20
|
-
|
21
|
-
it 'should use the method name as the header name, humanized' do
|
22
|
-
@headers.should include('Description')
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
describe 'when given a string description as a parameter' do
|
27
|
-
|
28
|
-
it 'should use the string value, unmodified' do
|
29
|
-
@headers.should include('Title')
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
describe 'when an hash is passed as a parameter' do
|
34
|
-
|
35
|
-
describe 'with a string value' do
|
36
|
-
|
37
|
-
it 'should use the string value, unmodified' do
|
38
|
-
@headers.should include('ISBN-10')
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
describe 'with a non-string value' do
|
43
|
-
|
44
|
-
it 'should use the non string value converted to a string, humanized' do
|
45
|
-
@headers.should include('Issuer')
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
end
|
50
|
-
|
51
|
-
end
|
52
|
-
|
53
|
-
describe Comma::HeaderExtractor, 'with static column method' do
|
54
|
-
before do
|
55
|
-
@headers = Class.new(Struct.new(:id, :name)) do
|
56
|
-
comma do
|
57
|
-
__static_column__
|
58
|
-
__static_column__ 'STATIC'
|
59
|
-
__static_column__ 'STATIC' do '' end
|
60
|
-
end
|
61
|
-
end.new(1, 'John Doe').to_comma_headers
|
62
|
-
end
|
63
|
-
|
64
|
-
it 'should extract headers' do
|
65
|
-
@headers.should eq(['', 'STATIC', 'STATIC'])
|
66
|
-
end
|
67
|
-
end
|
68
|
-
|
69
11
|
describe Comma::DataExtractor do
|
70
12
|
|
71
13
|
before do
|