Class: JavaPackage

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

Constant Summary

@@cache =
{}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pack_name, parent_pack = nil) ⇒ JavaPackage

Returns a new instance of JavaPackage



78
79
80
81
82
# File 'lib/rjbextension.rb', line 78

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



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

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



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

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

Instance Method Details

#class?(a) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#create_package_or_class(m) ⇒ Object



88
89
90
91
92
93
94
95
# File 'lib/rjbextension.rb', line 88

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



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

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