Class: BuildingSyncTk::Factory
- Inherits:
-
Isomorphic::Factory::AbstractFactory
- Object
- Isomorphic::Factory::AbstractFactory
- BuildingSyncTk::Factory
- Extended by:
- Singleton
- Defined in:
- lib/building_sync_tk/factory.rb
Overview
An Isomorphic factory that is specialized for the BuildingSync base module.
Instance Method Summary collapse
-
#get_user_defined_fields(object) ⇒ Hash<String, Array<String>>?
Get <auc:UserDefinedFields> for the given object.
-
#initialize ⇒ Factory
constructor
Private constructor.
-
#link(instance_class, target_instance, **options) {|instance| ... } ⇒ Object
Build an instance of the given class that represents a “link” to the given target.
-
#link_to(instance_method_name, instance_class, source_instance, target_instance, **options) {|instance| ... } ⇒ Object
Build an instance of the given class that represents a “link” from the given source to the given target, and then assign the “link” to the source using the given method name.
-
#link_to_collection(collection_method_name, collection_class, instance_class, source_instance, target_instance, **options) {|instance| ... } ⇒ Object
Build an instance of the given class that represents a “link” from the given source to the given target, and then append the “link” to the “collection of links” for the source using the given method name.
-
#set_user_defined_fields(object, hash = {}) ⇒ BuildingSync::UserDefinedFields
Set <auc:UserDefinedFields> for the given object.
-
#user_defined_field?(object, key, value) ⇒ Boolean
Is a <auc:UserDefinedField> present with the given key-value pair?.
-
#xmlattrs ⇒ Array<#to_sym>
The XML attribute names.
Constructor Details
#initialize ⇒ Factory
Private constructor.
15 16 17 |
# File 'lib/building_sync_tk/factory.rb', line 15 def initialize super(::BuildingSync) end |
Instance Method Details
#get_user_defined_fields(object) ⇒ Hash<String, Array<String>>?
The return value is a hash of arrays (not a hash) because <auc:FieldName> elements are not guaranteed unique within the <auc:UserDefinedFields> element.
Get <auc:UserDefinedFields> for the given object.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/building_sync_tk/factory.rb', line 25 def get_user_defined_fields(object) object.try(:userDefinedFields).try { |building_sync_user_defined_fields| if building_sync_user_defined_fields.is_a?(::BuildingSync::UserDefinedFields) building_sync_user_defined_fields.inject({}) { |acc, building_sync_user_defined_fields_user_defined_field| if building_sync_user_defined_fields_user_defined_field.is_a?(::BuildingSync::UserDefinedFields::UserDefinedField) building_sync_user_defined_fields_user_defined_field.send(:fieldName).try(:to_s).try { |key| building_sync_user_defined_fields_user_defined_field.send(:fieldValue).try(:to_s).try { |value| acc[key] ||= [] acc[key] << value } } end acc } else nil end } end |
#link(instance_class, target_instance, **options) {|instance| ... } ⇒ Object
Build an instance of the given class that represents a “link” to the given target.
In BuildingSync XML, a “link” is an element with an @IDref attribute. The value of the @IDref attribute is the value of the @ID attribute of another element.
59 60 61 62 63 64 65 |
# File 'lib/building_sync_tk/factory.rb', line 59 def link(instance_class, target_instance, **, &block) send(:for, instance_class, **.merge({ attributes: ([:attributes] || {}).merge({ xmlattr_IDref: target_instance.send(:xmlattr_ID), }), }), &block) end |
#link_to(instance_method_name, instance_class, source_instance, target_instance, **options) {|instance| ... } ⇒ Object
Build an instance of the given class that represents a “link” from the given source to the given target, and then assign the “link” to the source using the given method name.
80 81 82 |
# File 'lib/building_sync_tk/factory.rb', line 80 def link_to(instance_method_name, instance_class, source_instance, target_instance, **, &block) source_instance.send(:"#{instance_method_name}=", send(:link, instance_class, target_instance, **, &block)) end |
#link_to_collection(collection_method_name, collection_class, instance_class, source_instance, target_instance, **options) {|instance| ... } ⇒ Object
Build an instance of the given class that represents a “link” from the given source to the given target, and then append the “link” to the “collection of links” for the source using the given method name.
98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/building_sync_tk/factory.rb', line 98 def link_to_collection(collection_method_name, collection_class, instance_class, source_instance, target_instance, **, &block) collection = source_instance.send(collection_method_name) || source_instance.send(:"#{collection_method_name}=", send(:for, collection_class)) unless (instance = collection.select { |instance| instance.send(:xmlattr_IDref) == target_instance.send(:xmlattr_ID) }.first).nil? instance else instance = send(:link, instance_class, target_instance, **, &block) collection << instance instance end end |
#set_user_defined_fields(object, hash = {}) ⇒ BuildingSync::UserDefinedFields
Set <auc:UserDefinedFields> for the given object.
117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/building_sync_tk/factory.rb', line 117 def set_user_defined_fields(object, hash = {}) hash.each do |key, value| unless user_defined_field?(object, key, value) send(:path, object, :userDefinedFields) << send(:for, ::BuildingSync::UserDefinedFields::UserDefinedField, attributes: { fieldName: key.to_s, fieldValue: value.to_s }) end end object.send(:userDefinedFields) end |
#user_defined_field?(object, key, value) ⇒ Boolean
Is a <auc:UserDefinedField> present with the given key-value pair?
136 137 138 139 140 141 142 |
# File 'lib/building_sync_tk/factory.rb', line 136 def user_defined_field?(object, key, value) return false if key.nil? || value.nil? send(:path, object, :userDefinedFields, try: true).try(:any?) { |building_sync_user_defined_fields_user_defined_field| building_sync_user_defined_fields_user_defined_field.is_a?(::BuildingSync::UserDefinedFields::UserDefinedField) && building_sync_user_defined_fields_user_defined_field.send(:fieldName).try(:to_s).try(:==, key.to_s) && building_sync_user_defined_fields_user_defined_field.send(:fieldValue).try(:to_s).try(:==, value.to_s) } end |
#xmlattrs ⇒ Array<#to_sym>
In BuildingSync XML, elements may have an @ID attribute.
The XML attribute names.
150 151 152 |
# File 'lib/building_sync_tk/factory.rb', line 150 def xmlattrs %w(ID) end |