class Island::Creator

Attributes

content[RW]
files[RW]

Public Class Methods

call(files, opts={}) click to toggle source
# File lib/island/creator.rb, line 5
def self.call(files, opts={})
  files = Set.new(files)
  rejects = opts.fetch(:rejects)    { Plugins::REJECTIONS }
  mods = opts.fetch(:modifications) { Plugins::MODIFICATIONS }
  i = new(files)
  h = i.read_content
  h = i.alter_each_file(h)
  ## process each file
  c = i.blend_files(h)
  r = i.requires(c)

  i.ensure_dependencies(r, files)

  c = i.reject_line(c, rejects)
  c = i.modify_lines(c, mods)
  i.join_lines(c)
end
new(files) click to toggle source
# File lib/island/creator.rb, line 77
def initialize(files)
  @files = files
end
requires(files, opts={}) click to toggle source
# File lib/island/creator.rb, line 36
def self.requires(files, opts={})
  # TODO: temporary
  i       = new(files)
  c       = i.read_content
  r       = i.requires(c)
  i.ensure_dependencies(r, files)
end

Public Instance Methods

alter_each_file(h) click to toggle source
# File lib/island/creator.rb, line 23
def alter_each_file(h)
  # Act on the array of lines of each file
  # ie prepend ==Start of filename / ==End of filename
  # h.map do |k,v|

  # end
  h
end
blend_files(h) click to toggle source
# File lib/island/creator.rb, line 32
def blend_files(h)
  @content = h.values.flatten
end
ensure_dependencies(libs, files) click to toggle source
# File lib/island/creator.rb, line 44
def ensure_dependencies(libs, files)
  to_include    = libs.select { |k,v| v == false }.keys
  to_include.flat_map do |l|
    found = files.grep(/#{l}/)
    if found.empty?
      warn "Please include #{l} in files list"
      exit(1)
    end
    found
  end
end
join_lines(c = content) click to toggle source
# File lib/island/creator.rb, line 98
def join_lines(c = content)
  c.join("\n") + "\n"
end
modify_lines(c, plugins = Plugins::MODIFICATIONS) click to toggle source
# File lib/island/creator.rb, line 92
def modify_lines(c, plugins = Plugins::MODIFICATIONS)
  c = plugins.each_with_object(c) do |b, obj|
    b.call(obj)
  end
end
read_content(f = files) click to toggle source
# File lib/island/creator.rb, line 81
def read_content(f = files)
  Hash[*f.flat_map { |z| [z, File.open(z).readlines.map(&:chomp)] }]
end
reject_line(c, plugins = Plugins::REJECTIONS) click to toggle source
# File lib/island/creator.rb, line 85
def reject_line(c, plugins = Plugins::REJECTIONS)
  # plugins must eval to true for it to be removed
  c = plugins.each_with_object(c) do |b, obj|
    obj.reject!{ |l| b.call(l) }
  end
end
requires(c) click to toggle source
# File lib/island/creator.rb, line 56
def requires(c)
  r = c.select { |i| i[/^\s?require/]}
       .map { |o| o[/['"](.*)['"]/]; $1 }[0..6]
  h = r.map { |o| stdlib?(o) }.flatten
  Hash[*h]
end
stdlib?(lib) click to toggle source
# File lib/island/creator.rb, line 63
def stdlib?(lib)
  result = false
  s = Thread.new do
        begin
          require lib
          result = true
        rescue LoadError
          result = false
        end
      end
  s.join
  [lib, result]
end