Class: JavaPackage

Inherits:
Object
  • Object
show all
Defined in:
lib/rjbextension.rb

Constant Summary collapse

@@cache =
{}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pack_name, parent_pack = nil) ⇒ JavaPackage

Returns a new instance of JavaPackage.



85
86
87
88
89
# File 'lib/rjbextension.rb', line 85

def initialize(pack_name, parent_pack = nil)
  @pack_name = pack_name
  @parent_pack = parent_pack
  @cache = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args) ⇒ Object



91
92
93
94
# File 'lib/rjbextension.rb', line 91

def method_missing(m, *args)
  # return if possible old module/class
  @cache[m] ||= create_package_or_class(m)
end

Class Method Details

.new(pack_name, parent_pack = nil) ⇒ Object



118
119
120
# File 'lib/rjbextension.rb', line 118

def self.new(pack_name, parent_pack = nil)
  @@cache[pack_name] ||= super
end

Instance Method Details

#class?(a) ⇒ Boolean

Returns:

  • (Boolean)


112
113
114
115
# File 'lib/rjbextension.rb', line 112

def class?(a)
  first_letter = a[0,1]
  first_letter >= 'A' && first_letter <= 'Z'
end

#create_package_or_class(m) ⇒ Object



95
96
97
98
99
100
101
102
# File 'lib/rjbextension.rb', line 95

def create_package_or_class(m)
  method = m.to_s
  if class?(method)
    Rjb::import("#{self}.#{method}")
  else
    JavaPackage.new(method, self)
  end
end

#to_sObject



104
105
106
107
108
109
110
# File 'lib/rjbextension.rb', line 104

def to_s
  if @parent_pack
    "#{@parent_pack.to_s}.#@pack_name"
  else
    "#@pack_name"
  end
end