Sha256: d37b03e77f35ea27d13cc43440194d560ae7695b2f950cbd23b5880369fcdcc7
Contents?: true
Size: 1.1 KB
Versions: 51
Compression:
Stored size: 1.1 KB
Contents
require 'ostruct' class ThemePresentation def self.wrap(data, *presenters) normalized = normalize(data) presenters = normalize_presenters(presenters) wrapped_data = presenters.inject(normalized) do |wrapped, presenter| presenter.new(wrapped) end class << wrapped_data attr_accessor :wrapped_by end wrapped_data.wrapped_by = presenters wrapped_data end def self.wrap_collection(collection, *presenters) collection.collect { |item| wrap(item, *presenters) } end private def self.normalize(data) if data.is_a? Hash hash = {} data.each { |key, value| hash[key] = normalize value } OpenStruct.new hash elsif data.is_a? Array data.collect { |datum| normalize datum } else data end end def self.normalize_presenters(presenters) presenters.reject!(&:nil?) add_class_delegation(presenters) presenters end def self.add_class_delegation(presenters) presenters.each do |presenter| presenter.class_eval do def class __getobj__.class end end end end end
Version data entries
51 entries across 51 versions & 1 rubygems