imag/libimagruby/lib/imag.rb

141 lines
3.3 KiB
Ruby
Raw Normal View History

2017-01-22 15:39:57 +00:00
#!/usr/bin/env ruby
2017-02-21 10:09:15 +00:00
# imag ruby interface module
#
# This module is created because the library which is used to write the Ruby
# bindings in Rust does not support modules.
#
# This is a wrapper to have nice Ruby-like things in the Ruby codebase and for
# beeing backwards compatible as soon as the Rust library gets module support.
#
# There will probably always be a wrapper for the Rust library, to be more
# flexible with the API, though.
2017-01-22 16:05:40 +00:00
module Imag
2017-01-22 15:39:57 +00:00
2017-02-21 10:09:15 +00:00
# Function name of the function to call to initialize the Rust backend part.
# Do not use.
2017-01-22 15:39:57 +00:00
IMAG_INIT_FN_NAME = 'imag_ruby_initialize'
2017-02-21 10:09:15 +00:00
# Setup method
#
# Call this method for initializing the library.
# It dynamically creates the classes for the imag library.
2017-01-22 15:39:57 +00:00
def self.setup binary_path
require binary_path
2017-01-22 15:39:57 +00:00
self.core_setup
2017-01-22 15:39:57 +00:00
self.classes_setup
end
2017-02-21 10:09:15 +00:00
# Abstraction over the logger frontend of the binary
#
# This is just a translation to nice Ruby code
2017-01-22 15:39:57 +00:00
module Logger
2017-02-21 09:35:34 +00:00
def self.init cfg
debug = !!cfg[:debug]
verbose = !!cfg[:verbose]
color = !!cfg[:color]
2017-01-22 16:05:40 +00:00
RImag.init_logger debug, verbose, color
2017-01-22 15:39:57 +00:00
end
2017-02-21 10:09:15 +00:00
# Log text with "trace" level in imag
2017-01-22 15:39:57 +00:00
def self.trace msg
2017-01-22 16:05:40 +00:00
RImag.trace msg
2017-01-22 15:39:57 +00:00
end
2017-02-21 10:09:15 +00:00
# Log text with "debug" level in imag
2017-01-22 15:39:57 +00:00
def self.dbg msg
2017-01-22 16:05:40 +00:00
RImag.dbg msg
2017-01-22 15:39:57 +00:00
end
2017-02-21 10:09:15 +00:00
# Log text with "debug" level in imag (alias for Imag::Logger::dbg)
2017-01-22 15:39:57 +00:00
def self.debug msg
2017-01-22 16:05:40 +00:00
RImag.debug msg
2017-01-22 15:39:57 +00:00
end
2017-02-21 10:09:15 +00:00
# Log text with "info" level in imag
2017-01-22 15:39:57 +00:00
def self.info msg
2017-01-22 16:05:40 +00:00
RImag.info msg
2017-01-22 15:39:57 +00:00
end
2017-02-21 10:09:15 +00:00
# Log text with "warning" level in imag
2017-01-22 15:39:57 +00:00
def self.warn msg
2017-01-22 16:05:40 +00:00
RImag.warn msg
2017-01-22 15:39:57 +00:00
end
2017-02-21 10:09:15 +00:00
# Log text with "error" level in imag
2017-01-22 15:39:57 +00:00
def self.error msg
2017-01-22 16:05:40 +00:00
RImag.error msg
2017-01-22 15:39:57 +00:00
end
end
private
2017-02-21 10:09:15 +00:00
# Class names of the Classes in the Ruby scope
#
# These classes are created by the Rust backend with an "R" prefix, and are
# here mapped to Ruby classes by inheriting from them.
#
# Addidional functionality and convenience methods can then be set up upon
# these pure Ruby classes.
2017-01-22 15:39:57 +00:00
def self.class_names
[
:StoreId ,
:StoreHandle ,
:FileLockEntryHandle ,
:EntryHeader ,
:EntryContent ,
]
end
2017-02-21 10:09:15 +00:00
# Do the core setup
#
# Maps the Rust classes to Ruby classes by inheriting from them
def self.core_setup
2017-01-22 15:39:57 +00:00
self.class_names.map {|n| [n, "R#{n}".to_sym ] }.each do |elem|
2017-01-22 16:05:40 +00:00
Imag.const_set elem.first, Kernel.const_get(elem.last)
2017-01-22 15:39:57 +00:00
end
end
2017-02-21 10:09:15 +00:00
# Class setup
#
# Summarizing method for calling all the class-setup methods.
2017-01-22 15:39:57 +00:00
def self.classes_setup
self.class_storeid_setup
end
2017-02-21 10:09:15 +00:00
# Class setup for the StoreId class
#
# Sets up additional methods for the Imag::StoreId class.
2017-01-22 15:39:57 +00:00
def self.class_storeid_setup
2017-01-22 16:05:40 +00:00
Imag::StoreId.class_exec do
2017-01-22 15:39:57 +00:00
def to_s
self.to_str
end
end
end
end
if __FILE__ == $0
puts "Running some tests..."
puts "I hope you passed the library object as first argument..."
begin
2017-01-22 16:05:40 +00:00
Imag.setup ARGV.first
2017-01-22 15:39:57 +00:00
rescue Exception => e
puts "Seems not to be the case... or something else went wrong..."
puts e
exit 1
end
2017-02-21 09:35:34 +00:00
Imag::Logger.init debug: true, verbose: true, color: true
2017-01-22 16:05:40 +00:00
Imag::Logger.info "The Logger should work now"
2017-01-22 15:39:57 +00:00
2017-01-22 16:05:40 +00:00
Imag::Logger.info "Lets see whether we have properly setup StoreId"
Imag::Logger.info Imag::StoreId::new_baseless("baselessId").to_s
Imag::Logger.info "Seems good."
2017-01-22 15:39:57 +00:00
end