module Capataz

Constants

VERSION

Public Class Methods

allow_for(types, *options) click to toggle source
# File lib/capataz.rb, line 47
def allow_for(types, *options)
  store_options(:modules, :allow, types, options) { |type| type.is_a?(Module) }
end
allow_on(objs, *methods) click to toggle source
# File lib/capataz.rb, line 39
def allow_on(objs, *methods)
  store_options(:instances, :allow, objs, methods)
end
allowed_constant?(const) click to toggle source
# File lib/capataz.rb, line 90
def allowed_constant?(const)
  (set = @config[:allowed_constants]) && set.include?(const)
end
allowed_constants(*constants) click to toggle source
# File lib/capataz.rb, line 32
def allowed_constants(*constants)
  set = @config[:allowed_constants]
  constants = [constants] unless constants.is_a?(Enumerable)
  constants << Capataz unless constants.include?(Capataz)
  constants.each { |constant| set << constant }
end
allows_invocation_of(method) click to toggle source
# File lib/capataz.rb, line 84
def allows_invocation_of(method)
  method = method.to_s.to_sym unless method.is_a?(Symbol)
  return false if @config[:denied_methods].include?(method)
  true
end
can_declare?(symbol) click to toggle source
# File lib/capataz.rb, line 94
def can_declare?(symbol)
  (set = @config[:denied_declarations]).empty? || !set.include?(symbol)
end
config(&block) click to toggle source
# File lib/capataz.rb, line 55
def config(&block)
  @config ||=
    {
      denied_declarations: Set.new,
      allowed_constants: Set.new,
      denied_methods: Set.new,
      instances: {},
      modules: {}
    }
  class_eval(&block) if block
  @config
end
deny_declarations_of(*symbols) click to toggle source
# File lib/capataz.rb, line 24
def deny_declarations_of(*symbols)
  symbol_array_store(:denied_declarations, symbols)
end
deny_for(types, *options) click to toggle source
# File lib/capataz.rb, line 51
def deny_for(types, *options)
  store_options(:modules, :deny, types, options) { |type| type.is_a?(Module) }
end
deny_invoke_of(*methods) click to toggle source
# File lib/capataz.rb, line 28
def deny_invoke_of(*methods)
  symbol_array_store(:denied_methods, methods)
end
deny_on(objs, *methods) click to toggle source
# File lib/capataz.rb, line 43
def deny_on(objs, *methods)
  store_options(:instances, :deny, objs, methods)
end
disable(*args) click to toggle source
# File lib/capataz.rb, line 16
def disable(*args)
  @disable = args.first.to_s.to_i.to_b
end
disable?() click to toggle source
# File lib/capataz.rb, line 20
def disable?
  @disable
end
eval(string, *binding_filename_lineno) click to toggle source
Calls superclass method
# File lib/capataz.rb, line 11
def eval(string, *binding_filename_lineno)
  string = rewrite(string)
  super
end
handle(obj, options = {}) click to toggle source
# File lib/capataz.rb, line 132
def handle(obj, options = {})
  if obj.capataz_proxy? || [NilClass, Fixnum, Symbol, String, TrueClass, FalseClass].any? { |type| obj.is_a?(type) }
    obj
  elsif obj.is_a?(Hash)
    Capataz::HashProxy.new(obj)
  else
    Capataz::Proxy.new(obj, options)
  end
end
instance_response_to?(instance, *args) click to toggle source
# File lib/capataz.rb, line 68
def instance_response_to?(instance, *args)
  fail ArgumentError if args.length == 0
  method = args[0].is_a?(Symbol) ? args[0] : args[0].to_s.to_sym
  return false if @config[:denied_methods].include?(method)
  if (options = @config[:instances][instance])
    return false unless allowed_method?(options, instance, method)
  else
    @config[:modules].each do |type, opts|
      if instance.is_a?(type)
        return false unless allowed_method?(opts, instance, method)
      end
    end
  end
  instance.respond_to?(*args)
end
rewrite(code, options = {}) click to toggle source
# File lib/capataz.rb, line 110
def rewrite(code, options = {})
  return code if Capataz.disable?
  options ||= {}
  options[:halt_on_error] = true if options[:halt_on_error].nil?
  if (locals = options[:locals])
    locals = [locals] unless locals.is_a?(Enumerable)
    locals.each { |local| code = "#{local} ||= nil\r\n" + code }
  end
  buffer = Parser::Source::Buffer.new('code')
  buffer.source = code
  begin
    Capataz::Rewriter.new(options).rewrite(buffer, Parser::CurrentRuby.new.parse(buffer))
  rescue Exception => ex
    if (logs = options[:logs]).is_a?(Hash) &&
      (errors = (logs[:errors] ||= [])).is_a?(Array)
      errors << "syntax error: #{ex.message}"
    else
      raise ex
    end
  end
end
validate(code) click to toggle source
# File lib/capataz.rb, line 98
def validate(code)
  errors = []
  begin
    buffer = Parser::Source::Buffer.new('code')
    buffer.source = code
    Capataz::Rewriter.new(errors: errors).rewrite(buffer, Parser::CurrentRuby.new.parse(buffer))
  rescue => ex
    errors << ex.message
  end
  errors
end

Private Class Methods

allow_method_overrides() click to toggle source
# File lib/capataz.rb, line 144
def allow_method_overrides
  true #TODO Setup on config
end
allowed_method?(options, instance, method) click to toggle source
# File lib/capataz.rb, line 148
def allowed_method?(options, instance, method)
  if (allow = options[:allow])
    if allow.is_a?(Proc)
      return false unless allow.call(instance, method)
    else
      return false unless allow.include?(method)
    end
  end
  if (deny = options[:deny])
    if deny.is_a?(Proc)
      return false if deny.call(instance, method)
    else
      return false if deny.include?(method)
    end
  end
  true
end
instances_store(key, objs, methods) click to toggle source
# File lib/capataz.rb, line 172
def instances_store(key, objs, methods)
  methods = [methods] unless methods.is_a?(Enumerable)
  methods = methods.to_a.collect { |method| method.is_a?(Symbol) ? method : method.to_s.to_sym }
  objs = [objs] unless objs.is_a?(Enumerable)
  instances = @config[:instances]
  objs.each do |obj|
    (instances[obj] ||= {}).merge!(key => methods)
  end
end
store_options(entry_key, access_key, objs, options) { |obj| ... } click to toggle source
# File lib/capataz.rb, line 182
def store_options(entry_key, access_key, objs, options)
  if options.is_a?(Enumerable)
    options = options.flatten if options.is_a?(Array)
  else
    options = [options]
  end
  options =
    if options.length == 1 && options[0].is_a?(Proc)
      options[0]
    else
      options.collect { |option| option.is_a?(Symbol) ? option : option.to_s.to_sym }
    end
  objs = [objs] unless objs.is_a?(Enumerable)
  entry = @config[entry_key]
  objs.each do |obj|
    if block_given?
      fail "Illegal object #{obj}" unless yield(obj)
    end
    (entry[obj] ||= {}).merge!(access_key => options)
  end
end
symbol_array_store(key, symbols) click to toggle source
# File lib/capataz.rb, line 166
def symbol_array_store(key, symbols)
  set = @config[key]
  symbols = [symbols] unless symbols.is_a?(Enumerable)
  symbols.each { |symbol| set << (symbol.is_a?(Symbol) ? symbol : symbol.to_s.to_sym) }
end