Class: Ichiban::ProjectFile

Inherits:
Object
  • Object
show all
Defined in:
lib/ichiban/project_file.rb,
lib/ichiban/project_file.rb

Overview

Re-open this class to register all default file types

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rel) ⇒ ProjectFile

Returns a new instance of ProjectFile.



33
34
35
36
# File 'lib/ichiban/project_file.rb', line 33

def initialize(rel)
  @rel = rel
  @abs = File.join(Ichiban.project_root, rel)
end

Instance Attribute Details

#absObject (readonly)

Returns the value of attribute abs.



5
6
7
# File 'lib/ichiban/project_file.rb', line 5

def abs
  @abs
end

#relObject (readonly)

Returns the value of attribute rel.



47
48
49
# File 'lib/ichiban/project_file.rb', line 47

def rel
  @rel
end

Class Method Details

.from_abs(abs) ⇒ Object

Returns a new instance based on an absolute path. Will automatically pick the right subclass. Return nil if the file is not recognized.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ichiban/project_file.rb', line 14

def self.from_abs(abs)
  return nil if File.directory?(abs)
  rel = abs.slice(Ichiban.project_root.length..-1) # Relative to project root
  rel.sub!(/^\//, '') # Remove leading slash
  handler = @types.detect do |_, proc|
    proc.call rel
  end
  if handler
    klass = handler.first
    klass.new rel
  else
    nil
  end
end

.register_type(klass, &block) ⇒ Object

Pass in a subclass of Ichiban::ProjectFile and a block. The block accepts one param: a file path relative to the Ichiban project root. For example: ‘assets/css/main.css’. Each time the watcher detects a change to a file, the file’s path will be passed to the block. If the block returns true, an instance of klass will be created to compile the changed file.



43
44
45
# File 'lib/ichiban/project_file.rb', line 43

def self.register_type(klass, &block)
  @types << [klass, block]
end

Instance Method Details

#destObject

Returns an absolute path in the compiled directory



8
9
10
# File 'lib/ichiban/project_file.rb', line 8

def dest
  File.join(Ichiban.project_root, 'compiled', dest_rel_to_compiled)
end

#has_dest?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/ichiban/project_file.rb', line 29

def has_dest?
  respond_to?(:dest_rel_to_compiled)
end

#rel_to(path) ⇒ Object

Pass in a path relative to Ichiban.projet_root. Returns the path relative to the one you passed in. E.g. if the file is assets/js/example.js, then rel_to(‘assets/js’) returns ‘example.js’.



52
53
54
55
56
57
58
59
# File 'lib/ichiban/project_file.rb', line 52

def rel_to(path)
  path = path + '/' unless path.end_with?('/')
  if rel.start_with?(path)
    rel.sub(path, '')
  else
    raise "#{@abs} is not in #{path}"
  end
end

#replace_ext(path, new_ext) ⇒ Object

Returns a new path where the old extension is replaced with new_ext



62
63
64
# File 'lib/ichiban/project_file.rb', line 62

def replace_ext(path, new_ext)
  path.sub(/\..+$/, '.' + new_ext)
end