Class: Quandl::Command::Presenter

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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.options = { type: type }.merge( opts.symbolize_keys! )
  self.collection = object
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



29
30
31
# File 'lib/quandl/command/presenter.rb', line 29

def options
  @options
end

#typeObject

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, options={})
  name = 'Error' if record.kind_of?(Exception)
  name = options[: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(messages)
  result = []
  re = messages.delete(:response_errors) if messages.is_a?(Hash) && messages.has_key?(:response_errors)
  re.each{|k,v| messages[k] = v } if re.present? && re.is_a?(Hash)
  # format
  messages.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, options)
  klass = find_presenter_for_record(record, options)
  klass.new(record, options)
end

Instance Method Details

#collectionObject



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, options)
  end
  # present nil if the collection is empty
  @collection = [self.class.wrap_record_in_presenter( nil, options )] 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