Module: BindIt::Binding

Defined in:
lib/bind-it/binding.rb

Overview

This is the module your namespace should extend.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/bind-it/binding.rb', line 17

def self.extended(base)
  super(base)
  # Create configuration options.
  base.module_eval do
    class << self
      # Are the default JARs/classes loaded?
      attr_accessor :bound
      # The default path to look in for JARs.
      attr_accessor :jar_path
      # The flags for starting the JVM machine.
      attr_accessor :jvm_args
      # A file to redirect JVM output to.
      attr_accessor :log_file
      # The default JARs to load.
      attr_accessor :default_jars
      # The default Java namespace to 
      # search in for classes.
      attr_accessor :default_namespace
      # The default classes to load.
      attr_accessor :default_classes
    end
    # Set default values.
    self.jar_path = ''
    self.jvm_args = []
    self.log_file = nil
    self.default_namespace = 'java'
    self.default_classes = []
    self.bound = false
  end
end

Instance Method Details

#bindObject

Load the default JARs and classes.



51
52
53
54
55
56
57
58
59
# File 'lib/bind-it/binding.rb', line 51

def bind
  unless self.bound
    BindIt::JarLoader.log_file = self.log_file
    BindIt::JarLoader.jvm_args = self.jvm_args
    self.load_default_jars
    self.load_default_classes
  end
  self.bound = true
end

#load_class(klass, base = nil, rename = nil) ⇒ Object

Public function to load classes inside the namespace. Makes sure the default JARs are loaded before.



78
79
80
81
82
# File 'lib/bind-it/binding.rb', line 78

def load_class(klass, base = nil, rename = nil)
  base ||= self.default_namespace
  self.bind unless self.bound
  self.load_klass(klass, base, rename)
end

#load_default_classesObject

Create the Ruby classes corresponding to the StanfordNLP core classes.



86
87
88
89
90
91
# File 'lib/bind-it/binding.rb', line 86

def load_default_classes
  self.default_classes.each do |info|
    klass, base, rename = *info
    self.load_klass(klass, base, rename)
  end
end

#load_default_jarsObject

Load the JARs, looking in default path.



67
68
69
70
71
72
73
# File 'lib/bind-it/binding.rb', line 67

def load_default_jars
  self.default_jars.each do |jar|
    file, path = *jar
    path ||= self.jar_path
    load_jar(jar, path)
  end
end

#load_jar(jar, path = nil) ⇒ Object

Load a JAR file.



62
63
64
# File 'lib/bind-it/binding.rb', line 62

def load_jar(jar, path = nil)
  BindIt::JarLoader.load(jar, path)
end