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.10.0 app/presenters/theme_presentation.rb
woople-theme-0.9.0 app/presenters/theme_presentation.rb
woople-theme-0.8.19 app/presenters/theme_presentation.rb
woople-theme-0.8.18 app/presenters/theme_presentation.rb
woople-theme-0.8.17 app/presenters/theme_presentation.rb
woople-theme-0.8.16 app/presenters/theme_presentation.rb
woople-theme-0.8.15 app/presenters/theme_presentation.rb
woople-theme-0.8.14 app/presenters/theme_presentation.rb
woople-theme-0.8.13 app/presenters/theme_presentation.rb
woople-theme-0.8.12 app/presenters/theme_presentation.rb
woople-theme-0.8.11 app/presenters/theme_presentation.rb
woople-theme-0.8.10 app/presenters/theme_presentation.rb
woople-theme-0.8.9 app/presenters/theme_presentation.rb
woople-theme-0.8.8 app/presenters/theme_presentation.rb
woople-theme-0.8.7 app/presenters/theme_presentation.rb
woople-theme-0.8.6 app/presenters/theme_presentation.rb
woople-theme-0.8.5 app/presenters/theme_presentation.rb
woople-theme-0.8.4 app/presenters/theme_presentation.rb
woople-theme-0.8.3 app/presenters/theme_presentation.rb
woople-theme-0.8.2 app/presenters/theme_presentation.rb