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

Version Path
woople-theme-0.6.11 app/presenters/theme_presentation.rb
woople-theme-0.6.10 app/presenters/theme_presentation.rb
woople-theme-0.6.9 app/presenters/theme_presentation.rb
woople-theme-0.6.8 app/presenters/theme_presentation.rb
woople-theme-0.6.7 app/presenters/theme_presentation.rb
woople-theme-0.6.6 app/presenters/theme_presentation.rb
woople-theme-0.6.5 app/presenters/theme_presentation.rb
woople-theme-0.6.4 app/presenters/theme_presentation.rb
woople-theme-0.6.3 app/presenters/theme_presentation.rb
woople-theme-0.6.2 app/presenters/theme_presentation.rb
woople-theme-0.6.1 app/presenters/theme_presentation.rb
woople-theme-0.6 app/presenters/theme_presentation.rb
woople-theme-0.5.17 app/presenters/theme_presentation.rb
woople-theme-0.5.16 app/presenters/theme_presentation.rb
woople-theme-0.5.15 app/presenters/theme_presentation.rb
woople-theme-0.5.14 app/presenters/theme_presentation.rb
woople-theme-0.5.13 app/presenters/theme_presentation.rb
woople-theme-0.5.12 app/presenters/theme_presentation.rb
woople-theme-0.5.11 app/presenters/theme_presentation.rb
woople-theme-0.5.10 app/presenters/theme_presentation.rb