Class: IntercomRails::Proxy::Proxy

Inherits:
Object
  • Object
show all
Defined in:
lib/intercom-rails/proxy.rb

Direct Known Subclasses

Company, User

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object_to_proxy, search_object = nil) ⇒ Proxy

Returns a new instance of Proxy.



19
20
21
22
# File 'lib/intercom-rails/proxy.rb', line 19

def initialize(object_to_proxy, search_object = nil)
  @search_object = search_object
  @proxied_object = instance_variable_set(:"@#{type}", object_to_proxy)
end

Instance Attribute Details

#proxied_objectObject (readonly)

Returns the value of attribute proxied_object.



17
18
19
# File 'lib/intercom-rails/proxy.rb', line 17

def proxied_object
  @proxied_object
end

#search_objectObject (readonly)

Returns the value of attribute search_object.



17
18
19
# File 'lib/intercom-rails/proxy.rb', line 17

def search_object
  @search_object
end

Class Method Details

.class_stringObject



7
8
9
# File 'lib/intercom-rails/proxy.rb', line 7

def self.class_string
  self.to_s.split('::').last
end

.config(type_override = nil) ⇒ Object



62
63
64
# File 'lib/intercom-rails/proxy.rb', line 62

def self.config(type_override = nil)
  IntercomRails.config.send(type_override || type)
end

.config_delegator(attribute_name) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/intercom-rails/proxy.rb', line 94

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

.identity_attributesObject



110
111
112
# File 'lib/intercom-rails/proxy.rb', line 110

def self.identity_attributes
  @_identity_attributes ||= []
end

.inherited(subclass) ⇒ Object



11
12
13
14
15
# File 'lib/intercom-rails/proxy.rb', line 11

def self.inherited(subclass)
  subclass.class_eval do
    attr_reader class_string.downcase.to_s
  end
end

.proxy_delegator(attribute_name, options = {}) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/intercom-rails/proxy.rb', line 78

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

.standard_data_config_attributesObject



118
119
120
# File 'lib/intercom-rails/proxy.rb', line 118

def self.standard_data_config_attributes
  @_standard_data_config_attributes ||= []
end

.standard_data_proxy_attributesObject



114
115
116
# File 'lib/intercom-rails/proxy.rb', line 114

def self.standard_data_proxy_attributes
  @_standard_data_proxy_attributes ||= []
end

.typeObject



54
55
56
# File 'lib/intercom-rails/proxy.rb', line 54

def self.type
  self.class_string.downcase.to_sym
end

Instance Method Details

#custom_dataObject



48
49
50
# File 'lib/intercom-rails/proxy.rb', line 48

def custom_data
  custom_data_from_config.merge custom_data_from_request
end

#standard_dataObject



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/intercom-rails/proxy.rb', line 34

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

#to_hashObject



24
25
26
27
28
29
30
31
32
# File 'lib/intercom-rails/proxy.rb', line 24

def to_hash
  data = standard_data.merge(custom_data)
  [:id, :user_id].each do |id_key|
    if(data[id_key] && !data[id_key].is_a?(Numeric))
      data[id_key] = data[id_key].to_s
    end
  end
  DateHelper.convert_dates_to_unix_timestamps(data)
end