lib/bumblebee/bumblebee.rb in bumblebee-2.0.1 vs lib/bumblebee/bumblebee.rb in bumblebee-2.1.0
- old
+ new
@@ -17,14 +17,58 @@
# The top-level module provides the two main methods for convenience.
# You can also consume these in a more OOP way using the Template class or a more
# procedural way using these.
module Bumblebee
class << self
- def generate_csv(columns, objects, options = {})
- ::Bumblebee::Template.new(columns).generate_csv(objects, options)
+ # Two signatures for consumption:
+ #
+ # ::Bumblebee.generate_csv(columns = [], objects = [], options = {})
+ #
+ # or
+ #
+ # ::Bumblebee.generate_csv(objects = [], options = {}) do |t|
+ # t.column :id, header: 'ID #'
+ # t.column :first, header: 'First Name'
+ # end
+ def generate_csv(*args, &block)
+ if block_given?
+ objects = args[0] || []
+ options = args[1] || {}
+ else
+ objects = args[1] || []
+ options = args[2] || {}
+ end
+
+ template(args, &block).generate_csv(objects, options)
end
- def parse_csv(columns, string, options = {})
- ::Bumblebee::Template.new(columns).parse_csv(string, options)
+ # Two signatures for consumption:
+ #
+ # ::Bumblebee.parse_csv(columns = [], string = '', options = {})
+ #
+ # or
+ #
+ # ::Bumblebee.parse_csv(string = '', options = {}) do |t|
+ # t.column :id, header: 'ID #'
+ # t.column :first, header: 'First Name'
+ # end
+ def parse_csv(*args, &block)
+ if block_given?
+ string = args[0] || ''
+ options = args[1] || {}
+ else
+ string = args[1] || ''
+ options = args[2] || {}
+ end
+
+ template(args, &block).parse_csv(string, options)
+ end
+
+ private
+
+ def template(args, &block)
+ columns = block_given? ? [] : (args[0] || [])
+
+ ::Bumblebee::Template.new(columns, &block)
end
end
end