module Shift

SHIFT

Compiler and compressor interface framework

© 2011 Jostein Berre Eliassen - MIT licence

Constants

MAP
RedCarpet
VERSION

Attributes

global[W]

Public Class Methods

[](name, action=:default) click to toggle source

Get the preferred available class mapped to the given format or path and action.

# File lib/shift/mapper.rb, line 34
def [](name, action=:default)
  try_to_match(name) do |fmt|

    if action_map = MAP[fmt]
      if iface_list = action_map[action]
        return iface_list.pick
      else
        raise UnknownActionError,
          "#{action.inspect} with format #{name.inspect}."
      end
    end
    return global[action].pick if global[action]
  end
  raise UnknownFormatError, "no mappings for #{name}"
end
action_overview(globals=false) click to toggle source
# File lib/shift/mapper.rb, line 59
def action_overview(globals=false)
  result = {}
  synonyms.each do |group|
    result[group] = group.map do |fmt|
      MAP[fmt].atoms(globals).keys
    end.uniq
  end
  result
end
cat(*globs)
Alias for: concat
concat(*globs) click to toggle source

Read and concatenate several files. (see Shift.read)

TODO: glob

# File lib/shift.rb, line 46
def concat(*globs)
  buf = new('', File.extname(globs.first))
  globs.each do |glob|
    Dir[glob].each do |file|
      buf.read_append(file)
    end
  end
  buf
end
Also aliased as: cat
global() click to toggle source

Global actions that apply to all types

# File lib/shift/mapper.rb, line 16
def global
  @global ||= ActionMap.new(nil)
end
inspect_actions() click to toggle source
# File lib/shift/mapper.rb, line 69
def inspect_actions
  buf = []
  buf << "GLOBAL: #{global.atoms(true).keys.join(', ')}"

  action_overview.each do |types, actions|
    buf << "#{types.join(', ')}: #{actions.join(', ')}"
  end

  buf.join("\n")
end
map(*synonyms) click to toggle source

Register mappings for file types.

# File lib/shift/mapper.rb, line 23
def map(*synonyms)
  actions = synonyms.pop

  synonyms.each do |syn|
    am = MAP[syn.to_s] ||= ActionMap.new(global, actions)
  end
end
new(*args) click to toggle source

(see Shift::String.new)

# File lib/shift.rb, line 26
def new(*args)
  Shift::String.new(*args)
end
read(path, new_path=nil) click to toggle source

Read a file, returning a Shift string. @param [String] path the file to read from. @param [String] new_path treat the shift string as if it came

from this path. Can be used to override file type behavior.

(see Shift.new)

# File lib/shift.rb, line 37
def read(path, new_path=nil)
  new(File.read(path), new_path || path)
end
synonyms() click to toggle source
# File lib/shift/mapper.rb, line 51
def synonyms
  MAP.group_by do |name, actions|
    actions
  end.map do |action_map, hsh_ary|
    hsh_ary.map {|k,v| k }.flatten.uniq
  end
end

Private Class Methods

try_to_match(name) { |name| ... } click to toggle source

Given a file name, yield all the formats it could match.

# File lib/shift/mapper.rb, line 84
def try_to_match(name)
  return yield name if name.nil?

  pattern = File.basename(name.to_s.downcase)
  until pattern.empty?
    yield pattern
    pattern.sub!(/^[^.]*\.?/, '')
  end
end