Module: Shift

Defined in:
lib/shift.rb,
lib/shift/cli.rb,
lib/shift/errors.rb,
lib/shift/i/echo.rb,
lib/shift/i/sass.rb,
lib/shift/mapper.rb,
lib/shift/string.rb,
lib/shift/mappings.rb,
lib/shift/interface.rb,
lib/shift/action_map.rb,
lib/shift/interfaces.rb,
lib/shift/i/rdiscount.rb,
lib/shift/i/redcarpet.rb,
lib/shift/i/uglify_js.rb,
lib/shift/i/zlib_reader.rb,
lib/shift/i/zlib_writer.rb,
lib/shift/interface_list.rb,
lib/shift/i/coffee_script.rb,
lib/shift/i/yui_compressor.rb,
lib/shift/i/closure_compiler.rb

Defined Under Namespace

Modules: Transformable Classes: ActionMap, CLI, ClosureCompiler, CoffeeScript, DependencyError, Echo, Error, Interface, InterfaceList, LookupError, MappingError, RDiscount, Redcarpet, Sass, String, UglifyJS, UnknownActionError, UnknownFormatError, YUICompressor, ZlibReader, ZlibWriter

Constant Summary collapse

VERSION =
'0.4.0'
MAP =
{}
RedCarpet =
Redcarpet

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.globalObject

Global actions that apply to all types



16
17
18
# File 'lib/shift/mapper.rb', line 16

def global
  @global ||= ActionMap.new(nil)
end

Class Method Details

.[](name, action = :default) ⇒ Object

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

Raises:



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# 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) ⇒ Object



59
60
61
62
63
64
65
66
67
# 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

.concat(*globs) ⇒ Object Also known as: cat

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

TODO: glob



46
47
48
49
50
51
52
53
54
# 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

.inspect_actionsObject



69
70
71
72
73
74
75
76
77
78
# 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) ⇒ Object

Register mappings for file types.



23
24
25
26
27
28
29
# 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) ⇒ Object



26
27
28
# File 'lib/shift.rb', line 26

def new(*args)
  Shift::String.new(*args)
end

.read(path, new_path = nil) ⇒ Object

Read a file, returning a Shift string. (see Shift.new)

Parameters:

  • path (String)

    the file to read from.

  • new_path (String) (defaults to: nil)

    treat the shift string as if it came from this path. Can be used to override file type behavior.



37
38
39
# File 'lib/shift.rb', line 37

def read(path, new_path=nil)
  new(File.read(path), new_path || path)
end

.synonymsObject



51
52
53
54
55
56
57
# 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