Sha256: 74b087079a26fc71393c0508940d6347eb2e110300754d557edda150f5c30609

Contents?: true

Size: 1.78 KB

Versions: 1

Compression:

Stored size: 1.78 KB

Contents

require 'ostruct'

module AboutPage

  class OpenStructWithHashAccess < OpenStruct
    delegate :each, :map, :to => :to_h
    def to_json(options = {})
      Hash[*@table.map { |k, v| [k, (v.to_h if v.respond_to? :to_h || v) ] }.flatten ]
    end

    def reject &block
      self.class.new to_h.reject(&block)
    end

    def select &block
      self.class.new to_h.select(&block)
    end

    def to_xml(options = {})
      @table
    end

    def to_h
      @table
    end
  end

  class Configuration
    attr_accessor :hash
    delegate :to_xml, :to_h, :to_json, :to => :hash
    delegate :each, :map, :to => :to_h

    def initialize hash = nil
      @hash = hash || OpenStructWithHashAccess.new
    end

    def reject &block
      AboutPage::Configuration.new @hash.reject(&block)
    end

    def select &block
      AboutPage::Configuration.new @hash.select(&block)
    end

    def method_missing *args
      @hash.send(*args)
    end

    def preflight request
      self.nodes.each { |key, profile| profile.preflight(request) }
    end

    def ok?
       self.nodes.select { |key, profile| profile.respond_to? :ok? }.all? { |key, profile| profile.ok? }
    end

    def nodes
      self.to_h.select { |key, profile| profile.is_a? AboutPage::Configuration::Node }
    end

    def set_headers! response
      self.nodes.each { |key, profile| profile.set_headers! response }
    end

    class Node
      def preflight request

      end

      def set_headers! response
        messages.each { |m| add_header(response, m) }
      end

      def add_header response, text
        response.headers['X-AboutPage-Warning'] ||= "" 
        response.headers['X-AboutPage-Warning'] += "#{text};"
        
      end

      def ok?
        true
      end

      def messages
        []
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
about_page-0.0.2 lib/about_page/configuration.rb