Sha256: 329996d3dea9d192fd16c8c0c0d18ed78a4bfcadaa7e5ab181105937c279a65a

Contents?: true

Size: 1.85 KB

Versions: 6

Compression:

Stored size: 1.85 KB

Contents

module PatronusFati
  module DataModels
    module ReportedAttributes
      def self.included(klass)
        klass.extend RAClassMethods
        klass.property  :reported_online, DataMapper::Property::Boolean,  :default  => false
      end

      module RAClassMethods
        def reported_offline
          all(:reported_online => false)
        end

        def reported_online
          all(:reported_online => true)
        end
      end
    end

    module AutoVendorLookup
      def self.included(klass)
        klass.extend AVLClassMethods
        klass.property :vendor, DataMapper::Property::String, :length => 255

        klass.before(:save) do
          next if self.vendor || self.class.vendor_attribute.nil?
          result = Louis.lookup(attributes[self.class.vendor_attribute])
          self.vendor = result['long_vendor'] || result['short_vendor']
        end
      end

      module AVLClassMethods
        def vendor_attribute(attr = nil)
          @@vendor_attribute = attr if attr && properties[attr]
          @@vendor_attribute
        end
      end
    end

    module ExpirationAttributes
      def self.included(klass)
        klass.extend EAClassMethods
        klass.property  :last_seen_at,  DataMapper::Property::Integer,  :default  => Proc.new { Time.now.to_i }
      end

      def active?
        last_seen_at >= self.class.current_expiration_threshold
      end

      def seen!
        update(last_seen_at: Time.now.to_i)
      end

      def uptime
        Time.now.to_i - last_seen_at
      end

      module EAClassMethods
        def active
          all(:last_seen_at.gte => current_expiration_threshold)
        end

        def inactive
          all(:last_seen_at.lt => current_expiration_threshold)
        end

        def ordered_limit(num)
          all(:limit => num, :order => [ :last_seen_at.desc ])
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
patronus_fati-0.9.22 lib/patronus_fati/data_models/common.rb
patronus_fati-0.9.21 lib/patronus_fati/data_models/common.rb
patronus_fati-0.9.20 lib/patronus_fati/data_models/common.rb
patronus_fati-0.9.19 lib/patronus_fati/data_models/common.rb
patronus_fati-0.9.18 lib/patronus_fati/data_models/common.rb
patronus_fati-0.9.17 lib/patronus_fati/data_models/common.rb