Module: Jinx::Metadata
- Includes:
- Dependency, Introspector, Inverse
- Defined in:
- lib/jinx/metadata.rb
Overview
The metadata introspection mix-in for a Java application domain class or interface.
Constant Summary collapse
- TYPE_PRINTER =
A proc to print the unqualified class name.
PrintWrapper.new { |type| type.qp }
- PROP_DESC_PRINTER =
A proc to print the property descriptor name.
PrintWrapper.new { |pd| pd.name }
Instance Attribute Summary collapse
-
#domain_module ⇒ Module
The application domain Resource module which introspected this class.
Attributes included from Propertied
Attributes included from Dependency
Instance Method Summary collapse
-
#dependent_attributes_print_wrapper ⇒ PrintWrapper
private
A proc to print the dependent attributes.
-
#domain_type(attribute) ⇒ Class?
The domain type for attribute, or nil if attribute is not a domain attribute.
-
#empty_value(attribute) ⇒ Numeric, ...
Returns an empty value for the given attribute.
- #format_print_value(value) ⇒ Object private
-
#pretty_print(q) ⇒ Object
Prints this classifier’s content to the log.
-
#pretty_print_attribute_flags(prop) ⇒ <Symbol>
private
The flags to modify the property.
-
#pretty_print_attribute_hash ⇒ {String => <Symbol>}
private
The attributes to print.
- #property(attribute, *opts) ⇒ Object
Methods included from Introspector
#add_attribute_value_initializer, #add_java_property, #alias_property_accessors, #create_java_property, #define_java_property, #delegate_to_property, ensure_introspected, #introspect, #wrap_java_date_property, #wrap_java_property, #wrap_java_string_property
Methods included from Propertied
#add_alternate_key_attribute, #add_attribute, #add_attribute_aliases, #add_attribute_default, #add_attribute_defaults, #add_mandatory_attribute, #add_mandatory_attributes, #add_primary_key_attribute, #add_property, #add_restriction, #add_secondary_key_attribute, #alias_attribute, #alias_standard_attribute_hash, #all_key_attributes, #alternate_key_attributes, #append_ancestor_enum, #attribute_filter, #collect_mandatory_attributes, #collection_attribute?, #compose_property, #create_nonjava_property, #default_mandatory_local_attributes, #dependent_attributes, #dependent_properties, #domain_attribute?, #domain_attributes, #domain_properties, #each_property, #independent_attributes, #init_property_classifiers, #introspected?, #java_attributes, #mandatory_attributes, #mandatory_owner_attribute, #most_specific_domain_attribute, #nondomain_attribute?, #nondomain_attributes, #nondomain_java_attributes, #nonowner_attributes, #offset_attribute, #primary_key_attributes, #properties, #property_defined?, #property_hash, #property_path, #qualify_attribute, #qualify_property, #register_property_alias, #remove_attribute, #secondary_key_attributes, #secondary_key_non_owner_domain_attributes, #set_alternate_key_attributes, #set_attribute_type, #set_primary_key_attributes, #set_secondary_key_attributes, #standard_attribute, #unidirectional_dependent_attributes
Methods included from Inverse
#add_inverse_updater, #clear_inverse, #delegate_writer_to_inverse, #detect_inverse_attribute, #detect_inverse_attribute_from_candidates, #infer_property_inverse, #inverse_property, #restrict_attribute_inverse, #set_attribute_inverse
Methods included from Dependency
#add_dependent_attribute, #add_dependent_property, #add_owner, #add_owner_attribute, #bidirectional_java_dependent?, #create_owner_properties_enumerator, #create_owner_property_hash, #dependency_path_to, #dependent?, #dependent_attribute, #dependent_types, #depends_on?, #depends_on_recursive?, #detect_owner_attribute, #local_owner_properties, #local_owner_property_hash, #order_owner_attributes, #owner_attribute, #owner_properties, #owner_property, #owner_property_hash, #owner_type, #owner_types
Instance Attribute Details
Instance Method Details
#dependent_attributes_print_wrapper ⇒ PrintWrapper (private)
Returns a proc to print the dependent attributes.
140 141 142 143 144 145 |
# File 'lib/jinx/metadata.rb', line 140 def dependent_attributes_print_wrapper PrintWrapper.new do |prop| flags = pretty_print_attribute_flags(prop) flags.empty? ? "#{prop}" : "#{prop}(#{flags.join(',')})" end end |
#domain_type(attribute) ⇒ Class?
Returns the domain type for attribute, or nil if attribute is not a domain attribute.
42 43 44 45 |
# File 'lib/jinx/metadata.rb', line 42 def domain_type(attribute) prop = property(attribute) prop.type if prop.domain? end |
#empty_value(attribute) ⇒ Numeric, ...
Returns an empty value for the given attribute.
-
If this class is not abstract, then the empty value is the initialized value.
-
Otherwise, if the attribute is a Java primitive number then zero.
-
Otherwise, if the attribute is a Java primitive boolean then
false
. -
Otherwise, the empty value is nil.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/jinx/metadata.rb', line 55 def empty_value(attribute) if abstract? then prop = property(attribute) # the Java attribute type jtype = prop.property_descriptor.attribute_type if JavaProperty === prop # A primitive is either a boolean or a number (String is not primitive). if jtype and jtype.primitive? then type.name == 'boolean' ? false : 0 end else # Since this class is not abstract, create a prototype instance on demand and make # a copy of the initialized collection value from that instance. @prototype ||= new value = @prototype.send(attribute) || return value.class.new end end |
#format_print_value(value) ⇒ Object (private)
147 148 149 150 151 152 153 |
# File 'lib/jinx/metadata.rb', line 147 def format_print_value(value) case value when String then value when Class then value.qp else value.pp_s(:single_line) end end |
#pretty_print(q) ⇒ Object
Prints this classifier’s content to the log.
74 75 76 77 78 79 80 |
# File 'lib/jinx/metadata.rb', line 74 def pretty_print(q) map = pretty_print_attribute_hash.delete_if { |k, v| v.nil_or_empty? } # one indented line per entry, all but the last line ending in a comma content = map.map { |label, value| " #{label}=>#{format_print_value(value)}" }.join(",\n") # print the content to the log q.text("#{qp} structure:\n#{content}") end |
#pretty_print_attribute_flags(prop) ⇒ <Symbol> (private)
Returns the flags to modify the property.
94 95 96 97 98 |
# File 'lib/jinx/metadata.rb', line 94 def pretty_print_attribute_flags(prop) flags = [] flags << :disjoint if prop.disjoint? flags end |
#pretty_print_attribute_hash ⇒ {String => <Symbol>} (private)
Returns the attributes to print.
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/jinx/metadata.rb', line 101 def pretty_print_attribute_hash # the Java property descriptors pds = java_attributes.wrap { |pa| property(pa).property_descriptor } # the display label => properties printer prop_prn = pds.wrap { |pd| PROP_DESC_PRINTER.wrap(pd) } # the Java attributes prop_syms = pds.map { |pd| pd.name.to_sym }.to_set # the attribute aliases aliases = @alias_std_prop_map.keys - attributes.to_a - prop_syms alias_hash = aliases.to_compact_hash { |aliaz| @alias_std_prop_map[aliaz] } # the dependent attributes printer dep_prn_wrapper = dependent_attributes_print_wrapper dep_prn = dependent_attributes.wrap { |pa| dep_prn_wrapper.wrap(property(pa)) } # the owner classes printer own_prn = owners.wrap { |type| TYPE_PRINTER.wrap(type) } # the inverse attributes printer inv_prn = @attributes.to_compact_hash do |pa| prop = property(pa) "#{prop.type.qp}.#{prop.inverse}" if prop.inverse end # the domain attribute printer dom_prn = domain_attributes.to_compact_hash { |pa| domain_type(pa).qp } # the description => printable hash { 'Java attributes' => prop_prn, 'standard attributes' => attributes, 'aliases to standard attributes' => alias_hash, 'secondary key' => secondary_key_attributes, 'mandatory attributes' => mandatory_attributes, 'domain attributes' => dom_prn, 'owners' => own_prn, 'owner attributes' => owner_attributes, 'inverse attributes' => inv_prn, 'dependent attributes' => dep_prn, 'default values' => defaults } end |
#property(attribute, *opts) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/jinx/metadata.rb', line 23 def property(attribute, *opts) return super(attribute) if opts.empty? Options.to_hash(*opts).each do |k, v| case k when :alias then alias_attribute(v, attribute) when :default then add_attribute_default(attribute, v) when :dependent then add_dependent_attribute(attribute) if v when :inverse then set_attribute_inverse(attribute, v) when :mandatory then add_mandatory_attribute(attribute) if v when :primary_key then add_primary_key_attribute(attribute) if v when :secondary_key then add_secondary_key_attribute(attribute) if v when :alternate_key then add_alternate_key_attribute(attribute) if v when :type then set_attribute_type(attribute, v) else qualify_attribute(attribute, k) if v end end end |