Class: CaRuby::Domain::JavaAttribute

Inherits:
Attribute show all
Defined in:
lib/caruby/domain/java_attribute.rb

Overview

The attribute metadata for an introspected Java property.

Constant Summary

Constants inherited from Attribute

Attribute::SUPPORTED_FLAGS

Instance Attribute Summary collapse

Attributes inherited from Attribute

#accessors, #declarer, #flags, #type

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Attribute

#autogenerated?, #autogenerated_on_create?, #bidirectional_java_association?, #cascade_update_to_create?, #cascaded?, #collection?, #creatable?, #dependent?, #derived?, #derived_inverse, #disjoint?, #domain?, #fetched?, #include_in_save_template?, #independent?, #inverse, #inverse=, #inverse_metadata, #java_property?, #logical?, #many_to_many?, #nondomain?, #optional?, #owner?, #proxied_save?, #qualify, #reader, #restrict_flags, #restrict_type, #saved?, #saved_fetch?, #searchable?, #storable_prerequisite?, #to_s, #to_sym, #transient?, #uncreated_dependent?, #unidirectional?, #unidirectional_java_dependent?, #unsaved?, #updatable?, #volatile?, #writer

Constructor Details

#initialize(pd, declarer, restricted_type = nil) ⇒ JavaAttribute

Creates a Ruby Attribute symbol corresponding to the given Ruby Java class wrapper klazz and Java property_descriptor.

The attribute name is the lower-case, underscore property descriptor name with the alterations described in to_attribute_symbol and Class#unocclude_reserved_method.

The attribute type is inferred as follows:

  • If the property descriptor return type is a primitive Java type, then that type is returned.

  • If the return type is a parameterized collection, then the parameter type is returned.

  • If the return type is an unparameterized collection, then this method infers the type from the property name, e.g. StudyProtocolCollectiontype is inferred as StudyProtocol by stripping the Collection suffix, capitalizing the prefix and looking for a class of that name in the Metadata#domain_module.

  • If the declarer class metadata configuration includes a domain_attributes property, then the type specified in that property is returned.

  • Otherwise, this method returns Java::Javalang::Object.

The optional restricted_type argument restricts the attribute to a subclass of the declared property type.

Raises:

  • (ArgumentError)


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/caruby/domain/java_attribute.rb', line 34

def initialize(pd, declarer, restricted_type=nil)
  symbol = create_standard_attribute_symbol(pd, declarer)
  super(symbol, declarer, restricted_type)
  @property_descriptor = pd
  # deficient Java introspector does not recognize 'is' prefix for a Boolean property
  rm = declarer.property_read_method(pd)
  raise ArgumentError.new("Property does not have a read method: #{declarer.qp}.#{pd.name}") unless rm
  reader = rm.name.to_sym
  unless declarer.method_defined?(reader) then
    reader = "is#{reader.to_s.capitalize_first}".to_sym
    unless declarer.method_defined?(reader) then
      raise ArgumentError.new("Reader method not found for #{declarer} property #{pd.name}")
    end
  end
  unless pd.write_method then
    raise ArgumentError.new("Property does not have a write method: #{declarer.qp}.#{pd.name}")
  end
  writer = pd.write_method.name.to_sym
  unless declarer.method_defined?(writer) then
    raise ArgumentError.new("Writer method not found for #{declarer} property #{pd.name}")
  end
  @property_accessors = [reader, writer]
  qualify(:collection) if collection_java_class?
  @type = infer_type
end

Instance Attribute Details

#property_accessorsObject (readonly)

This attribute’s Java property [reader, writer] accessors, e.g. [:getActivityStatus, :setActivityStatus].



13
14
15
# File 'lib/caruby/domain/java_attribute.rb', line 13

def property_accessors
  @property_accessors
end

#property_descriptorObject (readonly)

This attribute’s Java property descriptor.



10
11
12
# File 'lib/caruby/domain/java_attribute.rb', line 10

def property_descriptor
  @property_descriptor
end

Class Method Details

.to_attribute_symbol(property_name) ⇒ Object

Returns a lower-case, underscore symbol for the given property_name. A name ending in ‘Collection’ is changed to a pluralization.

Examples:

JavaAttribute.to_attribute_symbol('specimenEventCollection') #=> :specimen_events


75
76
77
78
79
80
81
82
# File 'lib/caruby/domain/java_attribute.rb', line 75

def self.to_attribute_symbol(property_name)
  name = if property_name =~ /(.+)Collection$/ then
    property_name[0...-'Collection'.length].pluralize.underscore
  else
    property_name.underscore
  end
  name.to_sym
end

Instance Method Details

#property_readerSymbol

Returns the JRuby wrapper method for the Java property reader.

Returns:

  • (Symbol)

    the JRuby wrapper method for the Java property reader



61
62
63
# File 'lib/caruby/domain/java_attribute.rb', line 61

def property_reader
  property_accessors.first
end

#property_writerSymbol

Returns the JRuby wrapper method for the Java property writer.

Returns:

  • (Symbol)

    the JRuby wrapper method for the Java property writer



66
67
68
# File 'lib/caruby/domain/java_attribute.rb', line 66

def property_writer
  property_accessors.last
end