Sha256: 38fe842530789744f39e9831e371444029ec5a0718bbcb67d8d15353845c499e

Contents?: true

Size: 1.01 KB

Versions: 3

Compression:

Stored size: 1.01 KB

Contents

class SnippetFile < Struct.new(:name, :content)
  class << self
    def find_by_name(name)
      if file(name)
        self.new(name, self.read(name))
      end
    end
    alias find find_by_name

    def all
      Dir.glob([root, '/','*',suffix].join('')).collect{ |file_path|
        name = File.basename(file_path, suffix)
        content = File.read(file_path)
        self.new(name, content)
      }
    end

    def paginate(options)
      all
    end

    def root
      @root ||= Rails.root.to_s + '/app/templates/snippets'
    end

    def suffix
      '.radius'
    end

    def path(name)
      [root, '/', name, suffix].join('')
    end

    def file(name, collection=Dir.glob(path(name)))
      @files ||= {}
      return @files[name] if @files[name]
      @files[name] = collection.first
    end

    def read(name, reader=File)
      reader.read(file(name))
    end
  end

  def to_param
    name
  end

  def updated_at
    time = File.mtime(self.class.file(name))
    I18n.localize(time, :format => :timestamp)
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
radiant-snippets-extension-1.1.3 app/models/snippet_file.rb
radiant-snippets-extension-1.1.2 app/models/snippet_file.rb
radiant-snippets-extension-1.1.1 app/models/snippet_file.rb