crafterm-comma 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  == DESCRIPTION:
6
6
 
7
- Comma is a CSV (ie. comma separated values) generation extension for Ruby objects, that lets you seamlessly define a CSV output format via a small DSL. Comma works well on pure Ruby objects with attributes, as well as complex ones such as ActiveRecord objects with associations, extensions, etc. It doesnt distinguish between attributes, methods, associations, extensions, etc - they all are considered equal and invoked identically via the Comma DSL description.
7
+ Comma is a CSV (ie. comma separated values) generation extension for Ruby objects, that lets you seamlessly define a CSV output format via a small DSL. Comma works well on pure Ruby objects with attributes, as well as complex ones such as ActiveRecord objects with associations, extensions, etc. It doesn't distinguish between attributes, methods, associations, extensions, etc - they all are considered equal and invoked identically via the Comma DSL description. Multiple different CSV output descriptions can also be defined.
8
8
 
9
9
  When multiple objects in an Array are converted to CSV, the output includes generation of a header row reflected from names of the properties requested, or specified via the DSL.
10
10
 
@@ -32,7 +32,8 @@ An example Comma CSV enabled ActiveRecord class:
32
32
  pages :size => 'Pages'
33
33
  publisher :name
34
34
  isbn :number_10 => 'ISBN-10', :number_13 => 'ISBN-13'
35
-
35
+ blurb 'Summary'
36
+
36
37
  end
37
38
 
38
39
  end
@@ -54,13 +55,60 @@ Annotated, the comma description is as follows:
54
55
 
55
56
  # isbn is an association returning an object, :number_10 and :number_13 are called on the object with the specified headers 'ISBN-10' and 'ISBN-13'
56
57
  isbn :number_10 => 'ISBN-10', :number_13 => 'ISBN-13'
58
+
59
+ # blurb is an attribute of Book, with the header being specified directly as 'Summary'
60
+ blurb 'Summary'
57
61
 
58
62
  end
59
63
 
60
- In the above example, any of the declarations (name, description, pages, publisher, isbn, etc), could be methods, attributes, associations, etc - no distinction during configuration is required, as everything is invoked via Ruby's #send method.
64
+ In the above example, any of the declarations (name, description, pages, publisher, isbn, blurb, etc), could be methods, attributes, associations, etc - no distinction during configuration is required, as everything is invoked via Ruby's #send method.
65
+
66
+ You can get the CSV representation of any object by calling the to_comma method, optionally providing a CSV description name to use.
61
67
 
62
68
  Object values are automatically converted to strings via to_s allowing you to reuse any existing to_s methods on your objects (instead of having to call particular properties or define CSV specific output methods). Header names are also automatically humanized when reflected (eg. replacing _ characters with whitespace). The 'isbn' example above shows how multiple values can be added to the CSV output.
63
69
 
70
+ Multiple CSV descriptions can also be specified for the same class, eg:
71
+
72
+ class Book < ActiveRecord::Base
73
+
74
+ # ================
75
+ # = Associations =
76
+ # ================
77
+ has_many :pages
78
+ has_one :isbn
79
+ belongs_to :publisher
80
+
81
+ # ===============
82
+ # = CSV support =
83
+ # ===============
84
+ comma do
85
+
86
+ name
87
+ description
88
+
89
+ pages :size => 'Pages'
90
+ publisher :name
91
+ isbn :number_10 => 'ISBN-10', :number_13 => 'ISBN-13'
92
+ blurb 'Summary'
93
+
94
+ end
95
+
96
+ comma :brief do
97
+
98
+ name
99
+ description
100
+ blurb 'Summary'
101
+
102
+ end
103
+
104
+ end
105
+
106
+ You can specify which output format you would like to use via an optional parameter to to_comma:
107
+
108
+ Book.limited(10).to_comma(:brief)
109
+
110
+ Specifying no description name to to_comma is equivalent to specifying :default as the description name.
111
+
64
112
  When used with Rails (ie. add 'comma' as a gem dependency), Comma automatically adds support for rendering CSV output in your controllers:
65
113
 
66
114
  class BooksController < ApplicationController
@@ -72,5 +120,5 @@ When used with Rails (ie. add 'comma' as a gem dependency), Comma automatically
72
120
  end
73
121
 
74
122
  end
75
-
123
+
76
124
  If you have any questions or suggestions for Comma, please feel free to contact me at crafterm@redartisan.com, all feedback welcome!
@@ -23,7 +23,7 @@ module Comma
23
23
  case arg
24
24
  when Hash
25
25
  arg.each do |k, v|
26
- @results << v.to_s.humanize
26
+ @results << ((v.is_a? String) ? v : v.to_s.humanize)
27
27
  end
28
28
  when Symbol
29
29
  @results << arg.to_s.humanize
data/lib/comma.rb CHANGED
@@ -3,25 +3,31 @@ require 'fastercsv'
3
3
  require 'comma/extractors'
4
4
 
5
5
  class Array
6
- def to_comma
6
+ def to_comma(style = :default)
7
7
  FasterCSV.generate do |csv|
8
- csv << first.to_comma_headers
8
+ csv << first.to_comma_headers(style)
9
9
  each do |object|
10
- csv << object.to_comma
10
+ csv << object.to_comma(style)
11
11
  end
12
12
  end
13
13
  end
14
14
  end
15
15
 
16
16
  class Object
17
- def self.comma(&block)
18
- define_method :to_comma do
19
- Comma::DataExtractor.new(self, &block).results
20
- end
21
-
22
- define_method :to_comma_headers do
23
- Comma::HeaderExtractor.new(self, &block).results
24
- end
17
+ class_inheritable_accessor :comma_formats
18
+
19
+ def self.comma(style = :default, &block)
20
+ (self.comma_formats ||= {})[style] = block
21
+ end
22
+
23
+ def to_comma(style = :default)
24
+ raise "No comma format defined for style #{style}" unless self.comma_formats[style]
25
+ Comma::DataExtractor.new(self, &self.comma_formats[style]).results
26
+ end
27
+
28
+ def to_comma_headers(style = :default)
29
+ raise "No comma format defined for style #{style}" unless self.comma_formats[style]
30
+ Comma::HeaderExtractor.new(self, &self.comma_formats[style]).results
25
31
  end
26
32
  end
27
33
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: crafterm-comma
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marcus Crafter