Class: Sitepress::Path

Inherits:
Object
  • Object
show all
Defined in:
lib/sitepress/path.rb

Constant Summary collapse

HANDLER_EXTENSIONS =

Default handler extensions. Handlers are anything that render or manipulate the contents of the file into a different output, like ERB or HAML.

i[haml erb md markdown]
ROOT_NODE_NAME =

The root node name is a blank string.

"".freeze
ROOT_PATH =

The name of the root path

"/".freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, path_seperator: File::SEPARATOR, handler_extensions: self.class.handler_extensions) ⇒ Path

Returns a new instance of Path.



39
40
41
42
43
44
# File 'lib/sitepress/path.rb', line 39

def initialize(path, path_seperator: File::SEPARATOR, handler_extensions: self.class.handler_extensions)
  @path = path.to_s
  @path_seperator = Regexp.new(path_seperator)
  @handler_extensions = handler_extensions
  parse
end

Instance Attribute Details

#basenameObject (readonly)

Returns the value of attribute basename.



17
18
19
# File 'lib/sitepress/path.rb', line 17

def basename
  @basename
end

#dirnameObject (readonly)

Returns the value of attribute dirname.



17
18
19
# File 'lib/sitepress/path.rb', line 17

def dirname
  @dirname
end

#formatObject (readonly)

Returns the value of attribute format.



17
18
19
# File 'lib/sitepress/path.rb', line 17

def format
  @format
end

#handlerObject (readonly)

Returns the value of attribute handler.



17
18
19
# File 'lib/sitepress/path.rb', line 17

def handler
  @handler
end

#node_nameObject (readonly)

Returns the value of attribute node_name.



17
18
19
# File 'lib/sitepress/path.rb', line 17

def node_name
  @node_name
end

#pathObject (readonly)

Returns the value of attribute path.



17
18
19
# File 'lib/sitepress/path.rb', line 17

def path
  @path
end

Class Method Details

.action_view_template_handlers_extensionsObject

I tried to hook this into Rails engines in the ‘config.after_initialize` block, but the way template handlers register their extensions is across the board.

config.after_initialize do

Sitepress::Path.handler_extensions = ActionView::Template::Handlers.method(:extensions)

ends

I couldn’t get that working, instead I do this check to find the handlers.



34
35
36
# File 'lib/sitepress/path.rb', line 34

def action_view_template_handlers_extensions
  ActionView::Template::Handlers.extensions if defined?(ActionView::Template::Handlers)
end

.handler_extensionsObject



22
23
24
# File 'lib/sitepress/path.rb', line 22

def handler_extensions
  action_view_template_handlers_extensions || HANDLER_EXTENSIONS
end

Instance Method Details

#==(path) ⇒ Object



57
58
59
# File 'lib/sitepress/path.rb', line 57

def ==(path)
  to_s == path.to_s
end

#exists?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/sitepress/path.rb', line 61

def exists?
  File.exist? path
end

#expand_pathObject



65
66
67
# File 'lib/sitepress/path.rb', line 65

def expand_path
  File.expand_path path
end

#node_namesObject



46
47
48
# File 'lib/sitepress/path.rb', line 46

def node_names
  @node_names ||= node_name_ancestors.push(node_name)
end

#relative_path_from(target) ⇒ Object



73
74
75
# File 'lib/sitepress/path.rb', line 73

def relative_path_from(target)
  Pathname.new(@path).relative_path_from(target).to_s
end

#to_strObject Also known as: to_s

Necessary for operations like ‘File.read path` where `path` is an instance of this object.



52
53
54
# File 'lib/sitepress/path.rb', line 52

def to_str
  @path
end