Module: Malachite

Defined in:
lib/malachite.rb,
lib/malachite/client.rb,
lib/malachite/errors.rb,
lib/malachite/railtie.rb,
lib/malachite/version.rb,
lib/malachite/compiler.rb,
lib/malachite/function.rb,
lib/malachite/file_compiler.rb,
lib/malachite/function_cache.rb

Defined Under Namespace

Classes: ArgumentError, BuildError, Client, Compiler, DLError, FileCompiler, Function, MalachiteRailtie, ResponseError

Constant Summary collapse

DEFAULTS =
{
  precompile: false
}
VERSION =
'0.0.16'

Class Method Summary collapse

Class Method Details

.add_to_function_cache(method_name) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/malachite/function_cache.rb', line 16

def self.add_to_function_cache(method_name)
  function_cache = Malachite.function_cache
  function = Malachite.fiddle_function(method_name)
  function_cache[method_name] = function
  Malachite.function_cache = function_cache
  function
end

.called_method(method_name) ⇒ Object



35
36
37
# File 'lib/malachite/function_cache.rb', line 35

def self.called_method(method_name)
  "call#{method_name.to_s.camelize}"
end

.dump_json(object) ⇒ Object



39
40
41
# File 'lib/malachite.rb', line 39

def self.dump_json(object)
  JSON.generate(object)
end

.dylibObject



24
25
26
27
28
# File 'lib/malachite/function_cache.rb', line 24

def self.dylib
  @dylib ||= Fiddle.dlopen(Malachite.shared_object_path)
rescue Fiddle::DLError
  raise Malachite::DLError, 'Unable to open dynamic library.'
end

.fiddle_function(method_name) ⇒ Object



30
31
32
33
# File 'lib/malachite/function_cache.rb', line 30

def self.fiddle_function(method_name)
  call_method = Malachite.called_method(method_name)
  Fiddle::Function.new(Malachite.dylib[call_method], [Fiddle::TYPE_VOIDP], Fiddle::TYPE_VOIDP)
end

.from_function_cache(method_name) ⇒ Object



10
11
12
13
14
# File 'lib/malachite/function_cache.rb', line 10

def self.from_function_cache(method_name)
  existing_function = Malachite.function_cache[method_name]
  return existing_function if existing_function.present?
  Malachite.add_to_function_cache(method_name)
end

.function_cacheObject



2
3
4
# File 'lib/malachite/function_cache.rb', line 2

def self.function_cache
  @function_cache ||= {}
end

.function_cache=(cache) ⇒ Object



6
7
8
# File 'lib/malachite/function_cache.rb', line 6

def self.function_cache=(cache)
  @function_cache = cache
end

.hook_rails!Object



2
3
4
5
6
7
8
9
10
11
12
13
# File 'lib/malachite/railtie.rb', line 2

def self.hook_rails!
  Dir.glob(Rails.root.join('tmp', '*.so')).each do |file|
    File.delete(file)
  end
  Dir.glob(Rails.root.join('tmp', '*.go')).each do |file|
    File.delete(file)
  end
  Dir.glob(Rails.root.join('tmp', '*.h')).each do |file|
    File.delete(file)
  end
  Malachite::Compiler.new.compile if Malachite.precompile?
end

.load_json(string) ⇒ Object



35
36
37
# File 'lib/malachite.rb', line 35

def self.load_json(string)
  JSON.parse(string)
end

.method_missing(name, args) ⇒ Object



31
32
33
# File 'lib/malachite.rb', line 31

def self.method_missing(name, args)
  Malachite::Client.new(name, args).call
end

.optionsObject



19
20
21
# File 'lib/malachite.rb', line 19

def self.options
  @options ||= DEFAULTS.dup
end

.options=(opts) ⇒ Object



23
24
25
# File 'lib/malachite.rb', line 23

def self.options=(opts)
  @options = opts
end

.precompile?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/malachite.rb', line 27

def self.precompile?
  Malachite.options.fetch(:precompile, false)
end

.shared_object_pathObject



39
40
41
# File 'lib/malachite/function_cache.rb', line 39

def self.shared_object_path
  @so_path ||= Malachite::Compiler.new.compile
end