class Bundix::Nixer

Constants

LIST_T
SET_T

Attributes

level[R]
obj[R]

Public Class Methods

class_order(left, right) click to toggle source
# File lib/bundix/nixer.rb, line 45
def class_order(left, right)
  left.class.name <=> right.class.name # like Erlang
end
new(obj, level = 0) click to toggle source
# File lib/bundix/nixer.rb, line 55
def initialize(obj, level = 0)
  @obj = obj
  @level = level
end
order(left, right) click to toggle source
# File lib/bundix/nixer.rb, line 23
def order(left, right)
  if right.is_a?(left.class)
    if right.respond_to?(:<=>)
      cmp = right <=> left
      return -1 * (cmp) unless cmp.nil?
    end
  end

  if left.is_a?(right.class)
    if left.respond_to?(:<=>)
      cmp = right <=> left
      if cmp.nil?
        return class_order(left, right)
      else
        return cmp
      end
    end
  end

  return class_order(left, right)
end
serialize(obj) click to toggle source
# File lib/bundix/nixer.rb, line 19
def serialize(obj)
  new(obj).serialize
end

Public Instance Methods

indent() click to toggle source
# File lib/bundix/nixer.rb, line 60
def indent
  ' ' * (level + 2)
end
outdent() click to toggle source
# File lib/bundix/nixer.rb, line 64
def outdent
  ' ' * level
end
serialize() click to toggle source
# File lib/bundix/nixer.rb, line 80
def serialize
  case obj
  when Hash
    return SET_T.result(binding)
  when Array
    return LIST_T.result(binding)
  when String
    obj.dump
  when Symbol
    obj.to_s.dump
  when Pathname
    str = obj.to_s
    if %r{/} !~ str
      "./"+ str
    else
      str
    end
  when true, false
    obj.to_s
  else
    fail "Cannot convert to nix: #{obj.inspect}"
  end
end
serialize_key(k) click to toggle source
# File lib/bundix/nixer.rb, line 72
def serialize_key(k)
  if k.to_s =~ /^[a-zA-Z_-]+[a-zA-Z0-9_-]*$/
    k.to_s
  else
    sub(k, 2)
  end
end
sub(obj, indent = 0) click to toggle source
# File lib/bundix/nixer.rb, line 68
def sub(obj, indent = 0)
  self.class.new(obj, level + indent).serialize
end