class Grass::Key

Key parses path info into template meta data

Constants

ATTR_TYPES
FILE_REGEX
FORMATS
KEY_REGEX

Public Class Methods

new(attributes = {}) click to toggle source

before_validation :parse

Calls superclass method
# File lib/grass/key.rb, line 46
def initialize attributes = {}
  super(attributes)
  parse()
end

Public Instance Methods

fullpath() click to toggle source
# File lib/grass/key.rb, line 59
def fullpath
  @fullpath ||= @dir =~ /pages/ ? "/#{@locale}/#{@dir}/#{@path}" : "/#{@locale}/#{@dir}/#{@path}.#{@format}"
end
serializable_hash() click to toggle source
# File lib/grass/key.rb, line 51
def serializable_hash
  {id: self.id, dir: dir, path: path, locale: locale, format: format, handler: handler, filepath: filepath}
end
to_s() click to toggle source
# File lib/grass/key.rb, line 55
def to_s
  self.id
end

Private Instance Methods

filepath_to_id() click to toggle source

parsing based on file path

# File lib/grass/key.rb, line 109
def filepath_to_id
  @filepath = @filepath[@filepath.index(FILE_REGEX[:dir])..-1]
  FILE_REGEX.each do |attr,regex|
    if match = @filepath.scan(regex).try(:flatten).try(:compact)
      if attr == :ext
        @format = match.shift
        @handler = match.join(".")
      else
        self.instance_variable_set "@#{attr}", match=match[0]
        unless match.nil?
          attr == :locale ? @filepath.gsub!(/\.#{match}/,"") : @filepath.gsub!(/\/#{match}/,"")
        end
      end
    end
  end
end
id_to_filepath() click to toggle source

parsing based on uri path

# File lib/grass/key.rb, line 91
def id_to_filepath
  # parse attrs
  KEY_REGEX.each do |attr,regex|
    if match = @id.scan(regex).try(:flatten).try(:compact)
      if attr == :ext
        @format = match.shift
        @handler = match.join(".")
      else
        self.instance_variable_set "@#{attr}", match=match[0]
        @id.gsub!(/\/#{match}/,"") unless match.nil?
      end
    end
  end
end
is_asset?() click to toggle source
# File lib/grass/key.rb, line 127
def is_asset?
  @dir =~ /scripts|stylesheets|images|fonts|statics/
end
is_content?() click to toggle source
# File lib/grass/key.rb, line 131
def is_content?
  @dir =~ /pages|texts/
end
parse() click to toggle source

parses file or uri path to meta data

# File lib/grass/key.rb, line 68
def parse
  # we need one of them at least
  return nil if !@id.present? && !@filepath.present? 
  # get key from filepath if needs
  @id.nil? ? filepath_to_id : id_to_filepath
  # set default locale if null
  @locale ||= I18n.locale.to_s
  # cleanup blanks
  @handler = nil if @handler.blank?
  # set default format
  @format ||= FORMATS[@dir]
  # rebuild key
  @id = ["",@dir,@path].compact.join("/")
  # rebuild filepath
  dir = (is_asset? ? "assets/#{@dir}" : (is_content? ? "content/#{@dir}" : @dir))

  @filepath = Grass.app_root + ["",dir,@path].compact.join("/") + ["",@locale,@format,@handler].compact.join(".")      

end