Class: JavaHead::Package
- Inherits:
-
Object
- Object
- JavaHead::Package
- Includes:
- Exceptions
- Defined in:
- lib/java_head/package.rb
Overview
The class to represent Java packages. Packages are immutable and duplicate package names are not allowed. To this end, the ::new method is private and packages are accessed using the ::get method which checks the class’s internal cache prior to creating a new object
Constant Summary collapse
- FORMAT =
The required format for all package names
/^([a-z][a-z0-9]*\.)*[a-z_][a-z0-9_]*$/.freeze
- @@stored =
Hash.new
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
getter methods for name, superpackage, path.
-
#path ⇒ Object
readonly
getter methods for name, superpackage, path.
-
#superpackage ⇒ Object
readonly
getter methods for name, superpackage, path.
Class Method Summary collapse
-
.get(name) ⇒ JavaHead::Package
Get the package that corresponds to name.
Instance Method Summary collapse
-
#class(name = nil) ⇒ JavaHead::Class
return a class within the current package.
-
#classes ⇒ Array<JavaHead::Class>
get all classes in the current package.
-
#compile ⇒ JavaHead::Package
compile all classes in the package.
-
#compiled? ⇒ Boolean
Check if all the classes in this package are compiled.
-
#fullname ⇒ String
(also: #to_s)
recursively compute fullname using superpackage fullname.
-
#initialize(name) ⇒ Package
constructor
Construct a package This method is private.
-
#inspect ⇒ String
print useful fully-qualified name and path of class.
-
#member(name) ⇒ JavaHead::Package, JavaHead::Class
(also: #>)
returns #class(name) or #subpackage(name) depending on the format of name.
-
#remove_class ⇒ JavaHead::Package
call #remove_class on all class files of the package.
-
#subpackage(name) ⇒ JavaHead::Package
return a subpackage of the current package.
Constructor Details
#initialize(name) ⇒ Package
Construct a package This method is private
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/java_head/package.rb', line 16 def initialize(name) raise PackageException, "Package #{name} already exists" if @@stored[name.intern] # Test name raise PackageException, "Invalid package name #{name}" unless name.match FORMAT names = name.split('.') # An array of the package names, we will be using this a lot JavaHead::CLASSPATH.each do |base| absolute = base.join(*names) @path = absolute.realpath if absolute.exist? and absolute.directory? end raise PackageException, "Could not find directory for package #{name}" unless @path # Set superpackage @name = names.pop.freeze if names.empty? @superpackage = nil else @superpackage = self.class.get(names.join('.')) end end |
Instance Attribute Details
#name ⇒ Object (readonly)
getter methods for name, superpackage, path
41 42 43 |
# File 'lib/java_head/package.rb', line 41 def name @name end |
#path ⇒ Object (readonly)
getter methods for name, superpackage, path
41 42 43 |
# File 'lib/java_head/package.rb', line 41 def path @path end |
#superpackage ⇒ Object (readonly)
getter methods for name, superpackage, path
41 42 43 |
# File 'lib/java_head/package.rb', line 41 def superpackage @superpackage end |
Class Method Details
.get(name) ⇒ JavaHead::Package
Get the package that corresponds to name
151 152 153 154 155 156 157 |
# File 'lib/java_head/package.rb', line 151 def get(name) sym = name.intern return @@stored[sym] if @@stored[sym] package = new(name) @@stored[sym] = package package end |
Instance Method Details
#class(name = nil) ⇒ JavaHead::Class
return a class within the current package
75 76 77 78 79 80 |
# File 'lib/java_head/package.rb', line 75 def class(name=nil) return super() if name.eql? nil Dir.chdir @path do JavaHead::Class.new("#{fullname}.#{name}") end end |
#classes ⇒ Array<JavaHead::Class>
get all classes in the current package
85 86 87 88 89 90 91 |
# File 'lib/java_head/package.rb', line 85 def classes Dir.chdir(@path) do Dir.glob('*.java').map! do |filename| self.class( filename.match(/^([A-Z][A-Za-z0-9]*)\.java$/)[1] ) end end end |
#compile ⇒ JavaHead::Package
compile all classes in the package
96 97 98 99 |
# File 'lib/java_head/package.rb', line 96 def compile classes.each { |c| c.compile } self end |
#compiled? ⇒ Boolean
Check if all the classes in this package are compiled
104 105 106 107 108 109 |
# File 'lib/java_head/package.rb', line 104 def compiled? classes.each do |jclass| return false unless jclass.compiled? end true end |
#fullname ⇒ String Also known as: to_s
recursively compute fullname using superpackage fullname
46 47 48 49 |
# File 'lib/java_head/package.rb', line 46 def fullname return @name unless @superpackage "#{@superpackage.fullname}.#{@name}" end |
#inspect ⇒ String
print useful fully-qualified name and path of class
57 58 59 |
# File 'lib/java_head/package.rb', line 57 def inspect "[Java Package, name: #{fullname}, path: #{path}]" end |
#member(name) ⇒ JavaHead::Package, JavaHead::Class Also known as: >
returns #class(name) or #subpackage(name) depending on the format of name
125 126 127 128 129 130 131 132 |
# File 'lib/java_head/package.rb', line 125 def member(name) if name.match JavaHead::Class::FORMAT self.class(name) else subpackage(name) end end |
#remove_class ⇒ JavaHead::Package
call #remove_class on all class files of the package
114 115 116 117 |
# File 'lib/java_head/package.rb', line 114 def remove_class classes.each { |c| c.remove_class } self end |
#subpackage(name) ⇒ JavaHead::Package
return a subpackage of the current package
65 66 67 68 69 |
# File 'lib/java_head/package.rb', line 65 def subpackage(name) Dir.chdir @path do self.class.get("#{fullname}.#{name}") end end |