Sha256: efbd3a10c197e74600e11675f18a6d7e9325f923ec64e82d27ca050387db434d

Contents?: true

Size: 1.13 KB

Versions: 4

Compression:

Stored size: 1.13 KB

Contents

module AdminIt
  # Middleware request
  class Request < DelegateClass(Hash)
    ENV_KEY = 'admin_it.request'

    attr_reader :templates

    def initialize(env)
      @templates = {}
      @collections = {}
      @models = {}
      @env = env
      @process = false
      super({})
    end

    def process?
      @process == true
    end

    def process!
      @process = true
    end

    def self.get(request)
      return request if request.is_a?(self)
      fail 'Wrong request' unless request.is_a?(Rack::Request)
      request = request.env[ENV_KEY]
      fail 'rails_script middleware is not mounted' if request.nil?
      request
    end
  end

  # AdminIt middleware
  class Middleware
    SUBST_REGEXP = /<!--\s*([a-zA-Z0-9_]+)\s*-->/

    def initialize(app)
      @app = app
    end

    def call(env)
      request = env[Request::ENV_KEY] = Request.new(env)
      status, headers, body = @app.call(env)
      if request.process?
        body.each do |str|
          # $1 not working here ???
          str.gsub!(SUBST_REGEXP) { |s| request[SUBST_REGEXP.match(s)[1]] }
        end
      end
      [status, headers, body]
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
admin_it-1.2.3 lib/admin_it/middleware.rb
admin_it-1.2.2 lib/admin_it/middleware.rb
admin_it-1.2.1 lib/admin_it/middleware.rb
admin_it-1.2.0 lib/admin_it/middleware.rb