Class: Quandl::Command::Presenter
- Inherits:
-
Object
- Object
- Quandl::Command::Presenter
- Defined in:
- lib/quandl/command/presenter.rb,
lib/quandl/command/presenter/helper.rb,
lib/quandl/command/presenter/record.rb
Defined Under Namespace
Classes: Record
Instance Attribute Summary collapse
-
#options ⇒ Object
Returns the value of attribute options.
-
#type ⇒ Object
Returns the value of attribute type.
Class Method Summary collapse
- .find_presenter_for_record(record, options = {}) ⇒ Object
- .pretty_errors(messages) ⇒ Object
- .wrap_record_in_presenter(record, options) ⇒ Object
Instance Method Summary collapse
- #collection ⇒ Object
- #collection=(records) ⇒ Object
- #each(&block) ⇒ Object
- #each_to_format(type, &block) ⇒ Object
-
#initialize(object, opts = {}) ⇒ Presenter
constructor
A new instance of Presenter.
- #to_format(type) ⇒ Object
Constructor Details
#initialize(object, opts = {}) ⇒ Presenter
Returns a new instance of Presenter.
31 32 33 34 35 |
# File 'lib/quandl/command/presenter.rb', line 31 def initialize( object, opts={} ) self.type = ( object.try(:class) == Her::Collection ) ? :docs : :doc self. = { type: type }.merge( opts.symbolize_keys! ) self.collection = object end |
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
29 30 31 |
# File 'lib/quandl/command/presenter.rb', line 29 def @options end |
#type ⇒ Object
Returns the value of attribute type.
29 30 31 |
# File 'lib/quandl/command/presenter.rb', line 29 def type @type end |
Class Method Details
.find_presenter_for_record(record, options = {}) ⇒ Object
21 22 23 24 25 |
# File 'lib/quandl/command/presenter.rb', line 21 def find_presenter_for_record(record, ={}) name = 'Error' if record.kind_of?(Exception) name = [:as] || record.class.name.to_s.split("::").last unless name.present? "Quandl::Command::Presenters::#{name}Presenter".constantize rescue Quandl::Command::Presenter::Record end |
.pretty_errors(messages) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 |
# File 'lib/quandl/command/presenter/helper.rb', line 5 def pretty_errors() result = [] re = .delete(:response_errors) if .is_a?(Hash) && .has_key?(:response_errors) re.each{|k,v| [k] = v } if re.present? && re.is_a?(Hash) # format .each do |k,v| v = v.join(" & ") if v.respond_to?(:join) result << "#{k}: #{v}" end result.join(", ") end |
.wrap_record_in_presenter(record, options) ⇒ Object
16 17 18 19 |
# File 'lib/quandl/command/presenter.rb', line 16 def wrap_record_in_presenter(record, ) klass = find_presenter_for_record(record, ) klass.new(record, ) end |
Instance Method Details
#collection ⇒ Object
69 70 71 |
# File 'lib/quandl/command/presenter.rb', line 69 def collection @collection ||= [] end |
#collection=(records) ⇒ Object
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/quandl/command/presenter.rb', line 58 def collection=(records) @collection = [] records = Array(records).flatten records.each do |record| @collection << self.class.wrap_record_in_presenter(record, ) end # present nil if the collection is empty @collection = [self.class.wrap_record_in_presenter( nil, )] if @collection.blank? @collection end |
#each(&block) ⇒ Object
37 38 39 40 41 |
# File 'lib/quandl/command/presenter.rb', line 37 def each(&block) collection.each do |presenter| block.call( presenter ) end end |
#each_to_format(type, &block) ⇒ Object
43 44 45 46 47 48 |
# File 'lib/quandl/command/presenter.rb', line 43 def each_to_format(type, &block) collection.each do |record| output = record.send("to_#{type}") if record.respond_to?("to_#{type}") block.call( output, record.object ) end end |
#to_format(type) ⇒ Object
50 51 52 53 54 55 56 |
# File 'lib/quandl/command/presenter.rb', line 50 def to_format(type) output = [] collection.each do |record| output << record.send("to_#{type}") if record.respond_to?("to_#{type}") end output.join("\n") end |