Sha256: cca801e4d2f174fcce83bd8e92adb98770203682d52514f3873e930fd664125e

Contents?: true

Size: 896 Bytes

Versions: 36

Compression:

Stored size: 896 Bytes

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?)
    presenters
  end
end

Version data entries

36 entries across 36 versions & 1 rubygems

Version Path
woople-theme-0.8.1 app/presenters/theme_presentation.rb
woople-theme-0.8.0 app/presenters/theme_presentation.rb
woople-theme-0.6.27 app/presenters/theme_presentation.rb
woople-theme-0.6.26 app/presenters/theme_presentation.rb
woople-theme-0.6.25 app/presenters/theme_presentation.rb
woople-theme-0.6.24 app/presenters/theme_presentation.rb
woople-theme-0.6.23 app/presenters/theme_presentation.rb
woople-theme-0.6.22 app/presenters/theme_presentation.rb
woople-theme-0.6.21 app/presenters/theme_presentation.rb
woople-theme-0.6.20 app/presenters/theme_presentation.rb
woople-theme-0.6.19 app/presenters/theme_presentation.rb
woople-theme-0.6.18 app/presenters/theme_presentation.rb
woople-theme-0.6.17 app/presenters/theme_presentation.rb
woople-theme-0.6.14 app/presenters/theme_presentation.rb
woople-theme-0.6.13 app/presenters/theme_presentation.rb
woople-theme-0.6.12 app/presenters/theme_presentation.rb