Class: SnippetFile

Inherits:
Struct
  • Object
show all
Defined in:
app/models/snippet_file.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#contentObject

Returns the value of attribute content

Returns:

  • (Object)

    the current value of content



1
2
3
# File 'app/models/snippet_file.rb', line 1

def content
  @content
end

#nameObject

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



1
2
3
# File 'app/models/snippet_file.rb', line 1

def name
  @name
end

Class Method Details

.allObject



10
11
12
13
14
15
16
# File 'app/models/snippet_file.rb', line 10

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

.file(name, collection = Dir.glob(path(name))) ⇒ Object



34
35
36
37
38
# File 'app/models/snippet_file.rb', line 34

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

.find_by_name(name) ⇒ Object Also known as: find



3
4
5
6
7
# File 'app/models/snippet_file.rb', line 3

def find_by_name(name)
  if file(name)
    self.new(name, self.read(name))
  end
end

.paginate(options) ⇒ Object



18
19
20
# File 'app/models/snippet_file.rb', line 18

def paginate(options)
  all
end

.path(name) ⇒ Object



30
31
32
# File 'app/models/snippet_file.rb', line 30

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

.read(name, reader = File) ⇒ Object



40
41
42
# File 'app/models/snippet_file.rb', line 40

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

.rootObject



22
23
24
# File 'app/models/snippet_file.rb', line 22

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

.suffixObject



26
27
28
# File 'app/models/snippet_file.rb', line 26

def suffix
  '.radius'
end

Instance Method Details

#to_paramObject



45
46
47
# File 'app/models/snippet_file.rb', line 45

def to_param
  name
end

#updated_atObject



49
50
51
52
# File 'app/models/snippet_file.rb', line 49

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