Sha256: cc8f987fe6a29403d83183798cc00a8fa23a3d6bbac06d43017abb22a0c5e4b7

Contents?: true

Size: 1.31 KB

Versions: 1

Compression:

Stored size: 1.31 KB

Contents

require 'digest/md5'

module Docushin
  class Route

    extend ActiveModel::Naming
    include ActiveModel::Conversion

    attr_accessor :name
    attr_accessor :verb
    attr_accessor :path
    attr_accessor :controller
    attr_accessor :requirements
    attr_accessor :base
    attr_accessor :data
    attr_accessor :file_name
    attr_accessor :content
    attr_accessor :description
    
    def initialize(route)
      @controller = route.defaults[:controller]
      @base = File.join(Rails.root, 'doc', 'docushin')
      @name = route.name
      @verb = route.verb.source.empty? ? "MATCH" : route.verb.source.gsub(/[$^]/, '')
      @path = route.path.spec.to_s.sub('(.:format)', '')
      @requirements = route.requirements
      @file_name = Digest::MD5.hexdigest(@verb.to_s + @path)
      rtfm(@base, @file_name) if File.exists?(File.join(@base, @file_name)  + '.md')
    end

    def persisted?
      true
    end

    alias :id :file_name

    def rtfm(base, name)
      @content = File.read(File.join(base, name) + '.md')

      begin
        if @content =~ /^(---\s*\n.*?\n?)^(---\s*$\n?)/m
          @content = $'
          @data = YAML.load($1)
          @description = @data['description']
        end
      rescue => e
        # puts "Exception reading #{name}: #{e.message}"
      end
      @data ||= {}
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
docushin-0.0.1 lib/docushin/route.rb