Class: BuildingSyncTk::Factory

Inherits:
Isomorphic::Factory::AbstractFactory
  • Object
show all
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

Constructor Details

#initializeFactory

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>>?

Note:

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.

Parameters:

  • object (#userDefinedFields)

    the object

Returns:

  • (Hash<String, Array<String>>, nil)

    the key-value pairs as a hash or nil if the given object is invalid



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

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.

Parameters:

  • instance_class (Class)

    the class for the “link”

  • target_instance (#xmlattr_ID)

    the target instance

  • options (Hash<#to_sym, Object>)

    the options

Options Hash (**options):

  • :attributes (Hash<#to_sym, Object>) — default: {}

    the attributes for the “link”

  • :xmlattrs (Hash<#to_sym, #to_s>) — default: {}

    the XML attributes for the “link”

Yield Parameters:

  • instance (Object)

    the “link”

Yield Returns:

  • (void)

Returns:

  • (Object)

    the “link”

Raises:

  • (Isomorphic::InvalidFactoryClass)

    if the given class is invalid



59
60
61
62
63
64
65
# File 'lib/building_sync_tk/factory.rb', line 59

def link(instance_class, target_instance, **options, &block)
  send(:for, instance_class, **options.merge({
    attributes: (options[:attributes] || {}).merge({
      xmlattr_IDref: target_instance.send(:xmlattr_ID),
    }),
  }), &block)
end

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.

Parameters:

  • instance_method_name (#to_s)

    the method name

  • instance_class (Class)

    the class for the “link”

  • source_instance (#xmlattr_ID)

    the source instance

  • target_instance (#xmlattr_ID)

    the target instance

  • options (Hash<#to_sym, Object>)

    the options

Options Hash (**options):

  • :attributes (Hash<#to_sym, Object>) — default: {}

    the attributes for the “link”

  • :xmlattrs (Hash<#to_sym, #to_s>) — default: {}

    the XML attributes for the “link”

Yield Parameters:

  • instance (Object)

    the “link”

Yield Returns:

  • (void)

Returns:

  • (Object)

    the “link”

Raises:

  • (Isomorphic::InvalidFactoryClass)

    if the given class is invalid



80
81
82
# File 'lib/building_sync_tk/factory.rb', line 80

def link_to(instance_method_name, instance_class, source_instance, target_instance, **options, &block)
  source_instance.send(:"#{instance_method_name}=", send(:link, instance_class, target_instance, **options, &block))
end

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.

Parameters:

  • collection_method_name (#to_s)

    the method name

  • collection_class (Class)

    the class for the “link”

  • instance_class (Class)

    the class for the “link”

  • source_instance (#xmlattr_ID)

    the source instance

  • target_instance (#xmlattr_ID)

    the target instance

  • options (Hash<#to_sym, Object>)

    the options

Options Hash (**options):

  • :attributes (Hash<#to_sym, Object>) — default: {}

    the attributes for the “link”

  • :xmlattrs (Hash<#to_sym, #to_s>) — default: {}

    the XML attributes for the “link”

Yield Parameters:

  • instance (Object)

    the “link”

Yield Returns:

  • (void)

Returns:

  • (Object)

    the “link”

Raises:

  • (Isomorphic::InvalidFactoryClass)

    if the given class is invalid



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, **options, &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, **options, &block)

    collection << instance

    instance
  end
end

#set_user_defined_fields(object, hash = {}) ⇒ BuildingSync::UserDefinedFields

Set <auc:UserDefinedFields> for the given object.

Parameters:

  • object (#userDefinedFields)

    the object

  • hash (Hash<String, String>, nil) (defaults to: {})

    the key-value pairs

Returns:



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?

Parameters:

  • object (#userDefinedFields)

    the object

  • key (#to_s)

    the key

  • value (#to_s)

    the value

Returns:

  • (Boolean)

    true if the given key-value pair is present; otherwise, false



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

#xmlattrsArray<#to_sym>

Note:

In BuildingSync XML, elements may have an @ID attribute.

The XML attribute names.

Returns:

  • (Array<#to_sym>)

    the XML attribute names

See Also:

  • #xmlattr_ID_for


150
151
152
# File 'lib/building_sync_tk/factory.rb', line 150

def xmlattrs
  %w(ID)
end