Sha256: b62c0e3787e64bd2bbf033109513a024957e82e70e11048d4044019f3004e431

Contents?: true

Size: 1.44 KB

Versions: 3

Compression:

Stored size: 1.44 KB

Contents

# frozen_string_literal: true

require_relative '../types/std'
require_relative '../types/extras'
require_relative '../entity'
require_relative '../db'

module INat::Entity
  autoload :Observation, 'inat/data/entity/observation'
  autoload :Project,     'inat/data/entity/project'
end

class INat::Entity::Request < INat::Data::Entity

  include INat::Entity

  table :requests

  field :time, type: Time, index: true
  field :query, type: String, unique: true

  field :project, type: Project, index: true

  links :observations, item_type: Observation, index: true

  class << self

    def DDL
      super +
      "CREATE VIEW IF NOT EXISTS project_observations AS\n" +
      "  SELECT r.project_id, ro.observation_id\n" +
      "         FROM requests r, request_observations ro\n" +
      "         WHERE r.id = ro.request_id AND r.project_id IS NOT NULL;\n"
    end

    def create query_string, project_id
      update do
        max_id = DB.execute("SELECT max(id) AS id FROM requests;").map{ |r| r['id'] }.first
        new_id = if max_id == nil
          1
        else
          max_id + 1
        end
          @entities ||= {}
        @entities[new_id] ||= new new_id
        @entities[new_id].time = Time::at(0)
        @entities[new_id].query = query_string
        @entities[new_id].project_id = project_id
        @entities[new_id].save
      end
    end

  end

  attr_accessor :active

  def initialize id
    super(id)
    @active = false
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
inat-get-0.8.0.15 lib/inat/data/entity/request.rb
inat-get-0.8.0.14 lib/inat/data/entity/request.rb
inat-get-0.8.0.13 lib/inat/data/entity/request.rb