Class: INat::Entity::Request

Inherits:
Data::Entity show all
Includes:
INat::Entity
Defined in:
lib/inat/data/entity/request.rb

Instance Attribute Summary collapse

Attributes inherited from Data::Entity

#id

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Data::Entity

by_id, #complete?, ddl, fetch, from_db_rows, #get, #init, load, load_file, parse, read, #save, #to_db, #update

Methods included from App::Logger::DSL

#debug, debug, echo, #echo, error, #error, #info, info, log, #log, #warning, warning

Methods inherited from Data::Model

api_limit, api_part, api_path, backs, block, field, fields, has_path?, has_table?, #ignore, links, #post_update, #process?, #saved?, table, #to_h, #update

Constructor Details

#initialize(id) ⇒ Request

Returns a new instance of Request.



57
58
59
60
# File 'lib/inat/data/entity/request.rb', line 57

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

Instance Attribute Details

#activeObject

Returns the value of attribute active.



55
56
57
# File 'lib/inat/data/entity/request.rb', line 55

def active
  @active
end

#observationsArray<item_type: Observation>

Returns the observations field.

Returns:

  • (Array<item_type: Observation>)

    the observations field



24
# File 'lib/inat/data/entity/request.rb', line 24

links :observations, item_type: Observation, index: true

#projecttype: Project

Returns the project field.

Returns:

  • (type: Project)

    the project field



22
# File 'lib/inat/data/entity/request.rb', line 22

field :project, type: Project, index: true

#querytype: String

Returns the query field.

Returns:

  • (type: String)

    the query field



20
# File 'lib/inat/data/entity/request.rb', line 20

field :query, type: String, unique: true

#timetype: Time

Returns the time field.

Returns:

  • (type: Time)

    the time field



19
# File 'lib/inat/data/entity/request.rb', line 19

field :time, type: Time, index: true

Class Method Details

.create(query_string, project_id) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/inat/data/entity/request.rb', line 36

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

.DDLObject



28
29
30
31
32
33
34
# File 'lib/inat/data/entity/request.rb', line 28

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