lib/intercom-rails/proxy.rb in intercom-rails-0.2.8 vs lib/intercom-rails/proxy.rb in intercom-rails-0.2.9

- old
+ new

@@ -23,20 +23,30 @@ def to_hash standard_data.merge custom_data end + def standard_data + proxied_values = self.class.standard_data_proxy_attributes.reduce({}) do |hsh, attribute_name| + hsh[attribute_name] = send(attribute_name) if send(attribute_name).present? + hsh + end + + configured_values = self.class.standard_data_config_attributes.reduce({}) do |hsh, attribute_name| + next(hsh) unless config_variable_set?(attribute_name) + hsh.merge(attribute_name => send(attribute_name)) + end + + proxied_values.merge(configured_values) + end + def custom_data custom_data_from_config.merge custom_data_from_request end protected - def attribute_present?(attribute) - proxied_object.respond_to?(attribute) && proxied_object.send(attribute).present? - end - def self.type self.class_string.downcase.to_sym end def type @@ -47,9 +57,61 @@ IntercomRails.config.send(type_override || type) end def config(type_override = nil) self.class.config(type_override) + end + + def config_variable_set?(variable_name) + config.send(variable_name).present? + end + + def identity_present? + self.class.identity_attributes.any? { |attribute_name| proxied_object.respond_to?(attribute_name) && proxied_object.send(attribute_name).present? } + end + + def self.proxy_delegator(attribute_name, options = {}) + instance_variable_name = :"@_proxy_#{attribute_name}_delegated_value" + standard_data_proxy_attributes << attribute_name + identity_attributes << attribute_name if options[:identity] + + send(:define_method, attribute_name) do + return nil unless proxied_object.respond_to?(attribute_name) + + current_value = instance_variable_get(instance_variable_name) + return current_value if current_value + + value = proxied_object.send(attribute_name) + instance_variable_set(instance_variable_name, value) + end + end + + def self.config_delegator(attribute_name) + instance_variable_name = :"@_config_#{attribute_name}_delegated_value" + standard_data_config_attributes << attribute_name + + send(:define_method, attribute_name) do + return nil unless config.send(attribute_name).present? + + current_value = instance_variable_get(instance_variable_name) + return current_value if current_value + + getter = config.send(attribute_name) + value = getter.call(proxied_object) + instance_variable_set(instance_variable_name, value) + end + end + + def self.identity_attributes + @_identity_attributes ||= [] + end + + def self.standard_data_proxy_attributes + @_standard_data_proxy_attributes ||= [] + end + + def self.standard_data_config_attributes + @_standard_data_config_attributes ||= [] end private def custom_data_from_request