Class: Village::FileModel

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Naming
Includes:
Attributes
Defined in:
lib/village/file_model.rb

Direct Known Subclasses

Article, Page

Instance Attribute Summary collapse

Attributes included from Attributes

#attributes

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ FileModel

Returns a new instance of FileModel.



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/village/file_model.rb', line 10

def initialize(path)
  @content_path, @to_param, @folders, @slug, @extension = path.match(/^#{Rails.root}\/(#{self.class::CONTENT_PATH})\/((.*\/)?(.*))(\.[^.]+)$/).captures
  content = File.read(path)
  @attributes = Village::Config.clone(:title, :subtitle, :author, :layout)
  if content =~ /^(---\s*\n.*?\n?)^(---\s*$\n?)/m
    @attributes.merge! YAML.load($1)
    @attributes[:content] = content[($1.size + $2.size)..-1]
  else
    @attributes[:content] = content
  end
  @attributes = (@attributes || {}).with_indifferent_access
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Village::Attributes

Instance Attribute Details

#content_pathObject

Returns the value of attribute content_path.



8
9
10
# File 'lib/village/file_model.rb', line 8

def content_path
  @content_path
end

#extensionObject

Returns the value of attribute extension.



8
9
10
# File 'lib/village/file_model.rb', line 8

def extension
  @extension
end

#foldersObject

Returns the value of attribute folders.



8
9
10
# File 'lib/village/file_model.rb', line 8

def folders
  @folders
end

#slugObject

Returns the value of attribute slug.



8
9
10
# File 'lib/village/file_model.rb', line 8

def slug
  @slug
end

#to_paramObject

Returns the value of attribute to_param.



8
9
10
# File 'lib/village/file_model.rb', line 8

def to_param
  @to_param
end

Class Method Details

.create_class_methods_on(klass) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/village/file_model.rb', line 23

def self.create_class_methods_on(klass)
  klass.instance_eval do
    def all
     @files ||= Dir.glob("#{Rails.root}/#{self::CONTENT_PATH}/**/*.{#{Village::Config.file_extensions.join(',')}}").map do |filename|
        new filename
      end
    end
    
    def where(conditions = {})
      conditions = conditions.symbolize_keys
      conditions.assert_valid_keys :to_param
      all.select do |article|
        conditions.all? do |key, value| 
          article.send(key) == value
        end
      end
    end
    
    def find(id)
      where(:to_param => id).first or raise_missing_template(id.inspect)
    end
    
    def first
      all.first
    end
  
    def last
      all.last
    end
  
    def reset!
      @files = nil
    end
    
    private
    def raise_missing_template(id)
      if Rails::VERSION::MAJOR == 3 && Rails::VERSION::MINOR >= 1
        raise ActionView::MissingTemplate.new(
                [self::CONTENT_PATH], id, "Invalid ID", false, ActionView::Template.template_handler_extensions)
      else
        raise ActionView::MissingTemplate.new(
                [self::CONTENT_PATH], id, "Invalid ID", false)
      end
    end
  end # end child.instance_eval
end

Instance Method Details

#content_htmlObject

end self.create_class_methods_on



70
71
72
# File 'lib/village/file_model.rb', line 70

def content_html
  Tilt[@extension].new { @attributes[:content] }.render.html_safe if @attributes[:content].present?
end

#to_keyObject



78
79
80
# File 'lib/village/file_model.rb', line 78

def to_key
  [@to_param]
end

#to_sObject



74
75
76
# File 'lib/village/file_model.rb', line 74

def to_s
  "#{@attributes[:title]} (#{@to_param})"
end