Sha256: f3d6c307480bb16e1cf03b3a6c8b2a97732cc3691b32dc9f5abad9af8430d089

Contents?: true

Size: 1.67 KB

Versions: 64

Compression:

Stored size: 1.67 KB

Contents

module Intercom
  module Lib
    module DynamicAccessors

      class << self

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

        private

        def define_flat_store_based_accessors(attribute, value, klass)
          klass.class_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, klass)
          klass.class_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, klass)
            klass.class_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

64 entries across 64 versions & 2 rubygems

Version Path
intercom-4.1.2 lib/intercom/lib/dynamic_accessors.rb
intercom-4.1.1 lib/intercom/lib/dynamic_accessors.rb
intercom-4.1.0 lib/intercom/lib/dynamic_accessors.rb
intercom-4.0.1 lib/intercom/lib/dynamic_accessors.rb
intercom-4.0.0 lib/intercom/lib/dynamic_accessors.rb
intercom-3.9.5 lib/intercom/lib/dynamic_accessors.rb
intercom-3.9.4 lib/intercom/lib/dynamic_accessors.rb
intercom-3.9.3 lib/intercom/lib/dynamic_accessors.rb
intercom-3.9.2 lib/intercom/lib/dynamic_accessors.rb
intercom-3.9.0 lib/intercom/lib/dynamic_accessors.rb
intercom-3.8.1 lib/intercom/lib/dynamic_accessors.rb
intercom-3.8.0 lib/intercom/lib/dynamic_accessors.rb
intercom-3.7.7 lib/intercom/lib/dynamic_accessors.rb
intercom-3.7.6 lib/intercom/lib/dynamic_accessors.rb
intercom-3.7.5 lib/intercom/lib/dynamic_accessors.rb
intercom-3.7.4 lib/intercom/lib/dynamic_accessors.rb
intercom-3.7.3 lib/intercom/lib/dynamic_accessors.rb
intercom-3.7.2 lib/intercom/lib/dynamic_accessors.rb
intercom-3.7.1 lib/intercom/lib/dynamic_accessors.rb
intercom-3.7.0 lib/intercom/lib/dynamic_accessors.rb