Sha256: 3471d5cbf2261acc33482c6eebaeb3685090c9ac01e18205636cecc6e3ea98ca

Contents?: true

Size: 1.66 KB

Versions: 3

Compression:

Stored size: 1.66 KB

Contents

module Intercom
  module Lib
    module DynamicAccessors

      class << self

        def define_accessors(attribute, value, object)
          if attribute.to_s.end_with?('_at') && attribute.to_s != 'update_last_request_at'
            define_date_based_accessors(attribute, value, object)
          elsif object.flat_store_attribute?(attribute)
            define_flat_store_based_accessors(attribute, value, object)
          else
            define_standard_accessors(attribute, value, object)
          end
        end

        private

        def define_flat_store_based_accessors(attribute, value, object)
          object.instance_eval %Q"
            def #{attribute}=(value)
              mark_field_as_changed!(:#{attribute})
              @#{attribute} = Intercom::Lib::FlatStore.new(value)
            end
            def #{attribute}
              @#{attribute}
            end
          "
        end

        def define_date_based_accessors(attribute, value, object)
          object.instance_eval %Q"
            def #{attribute}=(value)
              mark_field_as_changed!(:#{attribute})
              @#{attribute} = value.nil? ? nil : value.to_i
            end
            def #{attribute}
              @#{attribute}.nil? ? nil : Time.at(@#{attribute})
            end
          "
        end

        def define_standard_accessors(attribute, value, object)
            object.instance_eval %Q"
              def #{attribute}=(value)
                mark_field_as_changed!(:#{attribute})
                @#{attribute} = value
              end
              def #{attribute}
                @#{attribute}
              end
            "
        end

      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
intercom-4.2.1 lib/intercom/lib/dynamic_accessors.rb
intercom-4.2.0 lib/intercom/lib/dynamic_accessors.rb
intercom-4.1.3 lib/intercom/lib/dynamic_accessors.rb