module EntityRESTHelpers

Public Class Methods

entity_resources() click to toggle source
# File lib/rbbt/rest/entity/locate.rb, line 3
def self.entity_resources
  @entity_resources ||= []
end

Public Instance Methods

action_card_render(card, &block) click to toggle source
# File lib/rbbt/rest/entity/action_card.rb, line 27
def action_card_render(card, &block)
  partial_render('entity_partials/action_card', :card => card, :block => block)
end
action_controller_render(controller) click to toggle source
# File lib/rbbt/rest/entity/action_controller.rb, line 40
def action_controller_render(controller)
  partial_render('entity_partials/action_controller', :controller => controller)
end
action_parameters(values = nil, action_options = {}, form_options = {}, &block) click to toggle source
# File lib/rbbt/rest/entity/helpers.rb, line 30
def action_parameters(values = nil, action_options = {}, form_options = {}, &block)
  o = Object.new
  o.extend InputModule

  if values.nil?
    values = @clean_params
  else
    values = @clean_params.merge(values)
  end

  o.instance_eval &block

  #description = o.description

  inputs = o.inputs || []
  input_types = o.input_types
  input_defaults = o.input_defaults
  input_options = o.input_options
  input_descriptions = o.input_descriptions

  hidden_inputs = []
  inputs.each do |input|
    values[input] = values.delete input.to_s unless values.include? input
    input_value = values[input]
    input_default = input_defaults[input]
    input_option = input_options[input]
    input_option ||= {}
    if form_options[:method] != 'POST' && ! input_option.include?(:no_file)
      input_option[:no_file] = true 
      input_options[input] = input_option
    end
  end

  locals = {}
  info = {:inputs => inputs, :input_descriptions => input_descriptions, :input_defaults => input_defaults, :input_options => input_options, :input_types => input_types, :values => values}
  locals[:id] = action_parameters_id
  locals[:action] = @ajax_url
  locals[:klass] = 'action_parameter_form'
  locals[:info] = info
  #locals[:description] = description
  locals[:method] = 'GET'
  locals = locals.merge(form_options)

  html_tag :div, partial_render('partials/form', locals), {:class => 'action_parameters ui raised segment'}.merge(action_options)
end
action_parameters_id() click to toggle source
# File lib/rbbt/rest/entity/helpers.rb, line 21
def action_parameters_id
  case
  when (params[:entity] and params[:action])
    ["action_params", params[:entity], params[:action]] * "__"
  else
    ["action_params", (rand * 1000).to_i] * "__"
  end
end
add_favourite_entity(entity) click to toggle source
# File lib/rbbt/rest/entity/favourites.rb, line 20
def add_favourite_entity(entity)
  raise "You need to login to have favourites" unless authorized?

  entity_type = entity.base_type
  dir = Path.setup(File.join(settings.favourites_dir, user))

  if (file = dir[entity_type]).exists?
    entities = Annotated.load_tsv(TSV.open(file))
    entities << entity
    Open.write(file, Annotated.tsv(entities.uniq, :all).to_s)
  else
    entities = [entity]
    Open.write(file, Annotated.tsv(entities, :all).to_s)
  end
end
add_favourite_entity_list(entity_type, list) click to toggle source
# File lib/rbbt/rest/entity/favourites.rb, line 68
def add_favourite_entity_list(entity_type, list)
  raise "You need to login to have favourites" unless authorized?

  dir = Path.setup(File.join(settings.favourite_lists_dir, user))

  if (file = dir[entity_type].find).exists?
    lists = Open.read(file.find).split("\n")
    lists << list
    Open.write(file, lists.uniq * "\n")
  else
    lists = [list]
    Open.write(file, lists * "\n")
  end
end
add_favourite_entity_map(entity_type, column, map) click to toggle source
# File lib/rbbt/rest/entity/favourites.rb, line 118
def add_favourite_entity_map(entity_type, column, map)
  raise "You need to login to have favourites" unless authorized?

  column = column.gsub('/', '--')
  dir = Path.setup(File.join(settings.favourite_maps_dir, user))

  if (file = dir[entity_type][column]).exists?
    maps = Open.read(file.find).split("\n")
    maps << map
    maps.uniq!
    Open.write(file, maps.uniq * "\n")
  else
    maps = [map]
    Open.write(file, maps * "\n")
  end
end
default_action_controller(entity, list_id = nil) click to toggle source
# File lib/rbbt/rest/entity/action_controller.rb, line 20
def default_action_controller(entity, list_id = nil)
  action_controller = EntityActionController.new(entity, list_id)

  case
  when Array === entity
    find_all_entity_list_action_templates(entity, true).each do |action|
      action_controller.add action, Misc.humanize(action, :format => :sentence), :reuse => true
    end
  when TSV === entity
    find_all_entity_map_action_templates(entity, true).each do |action|
      action_controller.add action, Misc.humanize(action, :format => :sentence), :reuse => true
    end
  else
    find_all_entity_action_templates(entity, true).each do |action|
      action_controller.add action, Misc.humanize(action, :format => :sentence)
    end
  end
  action_controller
end
entity_action_render(entity, action, params = {}) click to toggle source
# File lib/rbbt/rest/entity/render.rb, line 53
def entity_action_render(entity, action, params = {})
  template_file = locate_entity_action_template(entity, action)

  locals = params.merge({:entity => entity})

  name = entity.respond_to?(:name)? entity.name : entity
  @title = "#{action} #{name} [#{$title}]"

  Log.low "Rendering #{ entity } #{ action }: #{ template_file }"
  layout_file = layout ? locate_template("layout") : nil

  render(template_file, locals, layout_file, "Action #{ action }: #{ entity }".gsub(/\s/,'_'))
end
entity_card_render(card,&block) click to toggle source
# File lib/rbbt/rest/entity/entity_card.rb, line 33
def entity_card_render(card,&block)
  partial_render('entity_partials/entity_card', :card => card, :block => block)
end
entity_list_action_render(list, action, id, params = {}) click to toggle source
# File lib/rbbt/rest/entity/render.rb, line 81
def entity_list_action_render(list, action, id, params = {})
  template_file = locate_entity_list_action_template(list, action)

  locals = params.merge({:list => list, :list_id => id})

  name = id
  @title = "#{action} #{name} [#{$title}]"

  Log.low "Rendering #{list.base_type.to_s} list #{ action }: #{ template_file }"
  layout_file = layout ? locate_template("layout") : nil

  render(template_file, locals, layout_file, "Action #{ action } for list: #{ id }".gsub(/\s/,'_'))
end
entity_list_card_render(card) click to toggle source
# File lib/rbbt/rest/entity/entity_list_card.rb, line 32
def entity_list_card_render(card)
  partial_render('entity_partials/entity_list_card', :card => card)
end
entity_list_render(list, id) click to toggle source
# File lib/rbbt/rest/entity/render.rb, line 67
def entity_list_render(list, id)
  template_file = locate_entity_list_template(list)

  locals = {:list => list, :list_id => id}

  name = id
  @title = "#{name} [#{$title}]"

  Log.low "Rendering #{list.base_type.to_s} list: #{ template_file }"
  layout_file = layout ? locate_template("layout") : nil

  render(template_file, locals, layout_file, "Entity list: #{ id }".gsub(/\s/,'_'))
end
entity_map_action_render(map, action, id, params = {}) click to toggle source
# File lib/rbbt/rest/entity/render.rb, line 110
def entity_map_action_render(map, action, id, params = {})
  template_file = locate_entity_map_action_template(map, action)

  locals = params.merge({:map => map, :map_id => id})

  name = id
  @title = "#{action} #{name} [#{$title}]"

  Log.low "Rendering '#{map.key_field} - #{map.fields.first}' map #{ action }: #{ template_file }"
  layout_file = layout ? locate_template("layout") : nil

  render(template_file, locals, layout_file, "Action #{ action } for map: #{ id }".gsub(/\s/,'_'))
end
entity_map_card_render(card) click to toggle source
# File lib/rbbt/rest/entity/entity_map_card.rb, line 12
def entity_map_card_render(card)
  partial_render('entity_partials/entity_map_card', :card => card)
end
entity_map_render(map_id, type, column) click to toggle source
# File lib/rbbt/rest/entity/render.rb, line 95
def entity_map_render(map_id, type, column)
  template_file = locate_entity_map_template(type, column)

  map = Entity::Map.load_map(type, column, map_id, user)
  locals = {:map => map, :map_id => map_id}

  name = map_id
  @title = "#{name} [#{$title}]"

  Log.low "Rendering #{type} map: #{ template_file }"
  layout_file = layout ? locate_template("layout") : nil

  render(template_file, locals, layout_file, "Entity map: #{ map_id }".gsub(/\s/,'_'))
end
entity_render(entity, params = {}) click to toggle source
# File lib/rbbt/rest/entity/render.rb, line 38
def entity_render(entity, params = {})
  template_file = locate_entity_template(entity)

  params = {} if params.nil?
  locals = {:entity => entity}.merge(params)

  name = entity.respond_to?(:name)? entity.name : entity
  @title = "#{name} [#{$title}]"

  Log.low "Rendering #{ entity }: #{ template_file }"
  layout_file = layout ? locate_template("layout") : nil

  render(template_file, locals, layout_file, "Entity: #{ entity }".gsub(/\s/,'_'))
end
entity_resources() click to toggle source
# File lib/rbbt/rest/entity/locate.rb, line 7
def entity_resources
  EntityRESTHelpers.entity_resources + [Rbbt.share.views.find(:lib)] 
end
favourite_entities() click to toggle source

{{{ Entities

# File lib/rbbt/rest/entity/favourites.rb, line 5
def favourite_entities
  raise "You need to login to have favourites" unless authorized?

  dir = Path.setup(File.join(settings.favourites_dir, user))

  favourites = {}
  dir.glob('**').each do |file|
    type = File.basename(file)
    entities = Annotated.load_tsv(TSV.open(file))
    favourites[type] = entities
  end

  favourites
end
favourite_entity_lists() click to toggle source

{{{ Entity Lists

# File lib/rbbt/rest/entity/favourites.rb, line 55
def favourite_entity_lists
  raise "You need to login to have favourites" unless authorized?

  dir = Path.setup(File.join(settings.favourite_lists_dir, user))
  favourites = {}
  dir.glob('**').each do |file|
    type = File.basename(file)
    lists = Open.read(file).split("\n")
    favourites[type] = lists
  end
  favourites
end
favourite_entity_maps() click to toggle source

{{{ Entity Maps

# File lib/rbbt/rest/entity/favourites.rb, line 101
def favourite_entity_maps
  raise "You need to login to have favourites" unless authorized?

  dir = Path.setup(File.join(settings.favourite_maps_dir, user))
  favourites = {}
  dir.find.glob('*').each do |type_dir|
    type = File.basename(type_dir)
    Path.setup(type_dir).glob('*').each do |file|
      column = File.basename(file).gsub('--', '/')
      maps = Open.read(file).split("\n")
      favourites[type] ||= {}
      favourites[type][column] = maps
    end
  end
  favourites
end
find_all_entity_action_templates(entity, check = false) click to toggle source
# File lib/rbbt/rest/entity/locate.rb, line 85
def find_all_entity_action_templates(entity, check = false)
  resources = resources_for_entity(entity)

  types = entity.annotation_types.collect{|e| e.to_s}
  types << "Default"

  paths = types.inject([]) do |acc,type|
    all_files  = glob_all_server_files(["entity", type, "*.haml"]*"/", resources)
    all_checks = glob_all_server_files(["entity", type, "*.check"]*"/", resources)
    rejected = []
    all_checks.each do |check_file|
      rejected << File.basename(check_file).sub('.check', '') if reject?(check_file, binding)
    end

    accepted = all_files.reject{|path|
      rejected.include? File.basename(path).sub('.haml', '')
    }

    acc += accepted
  end

  if check
    paths = paths.reject do |path|
      (path.basename == "edit.haml" or path.basename == 'new.haml') 
    end
  end

  actions = paths.collect{|file| file.basename.sub('.haml', '') }.uniq

  actions.select! do |action|
    begin
      locate_entity_action_template(entity, action)
    rescue Exception
      false
    end
  end if check
  
  actions
end
find_all_entity_list_action_templates(list, check = false) click to toggle source
# File lib/rbbt/rest/entity/locate.rb, line 162
def find_all_entity_list_action_templates(list, check = false)
  resources = resources_for_entity(list)

  types = list.annotation_types.collect{|e| e.to_s}
  types << "Default"

  paths = types.inject([]) do |acc,type|
    all_files  = glob_all_server_files(["entity_list", type, "*.haml"]*"/", resources)
    all_checks = glob_all_server_files(["entity_list", type, "*.check"]*"/", resources)
    rejected = []
    all_checks.each do |check_file|
      rejected << File.basename(check_file).sub('.check', '') if reject?(check_file, binding)
    end

    accepted = all_files.reject{|path|
      rejected.include? File.basename(path).sub('.haml', '')
    }

    acc += accepted
  end

  if check
    paths = paths.reject do |path|
      (path.basename == "edit.haml" or path.basename == 'new.haml') 
    end
  end

  actions = paths.collect{|file| file.basename.sub('.haml', '') }.uniq

  actions.select! do |action|
    begin
      locate_entity_list_action_template(list, action)
    rescue Exception
      false
    end
  end if check
  
  actions
end
find_all_entity_map_action_templates(map, check = false) click to toggle source
# File lib/rbbt/rest/entity/locate.rb, line 246
def find_all_entity_map_action_templates(map, check = false)
  resources = entity_resources

  field = map.key_field

  if map.entity_templates[field] 
    types = map.entity_templates[field].annotation_types
  else
    types = [Entity.formats[field]].compact
  end
  types += ["Default"]

  paths = types.inject([]) do |acc,type|
    all_files  = glob_all_server_files(["entity_map", type, "*.haml"]*"/", resources)
    all_checks = glob_all_server_files(["entity_map", type, "*.check"]*"/", resources)
    rejected = []
    all_checks.each do |check_file|
      rejected << File.basename(check_file).sub('.check', '') if reject?(check_file, binding)
    end

    accepted = all_files.reject{|path|
      rejected.include? File.basename(path).sub('.haml', '')
    }

    acc += accepted
  end

  if check
    paths = paths.reject do |path|
      (path.basename == "edit.haml" or path.basename == 'new.haml') 
    end
  end

  actions = paths.collect{|file| file.basename.sub('.haml', '') }.uniq

  actions.select! do |action|
    begin
      locate_entity_map_action_template(map, action)
    rescue Exception
      Log.exception $!
      false
    end
  end if check
  
  actions
end
find_entity_url(term) click to toggle source
# File lib/rbbt/rest/entity/finder.rb, line 33
def find_entity_url(term)
  if term =~ /(.*) \[(.*)\]$/
    term = $1
    namespace, format = $2.split(":")
    format, namespace = namespace, nil if format.nil?

    Entity::REST.entity_url(term, format, :organism => namespace)
  else
    sorted_results = finder_find(term)
    organism = sorted_results.select{|r| r[:type] == 'organism' }.first
    sorted_results = sorted_results.reject{|r| r[:type] == 'organism' }
    i = sorted_results.first
    halt 404, "Term not recognized: #{ term }" if i.nil?
    organism_code = i[:namespace]
    organism_code = organism[:code] unless organism.nil? or organism[:code].nil?
    Entity::REST.entity_url(i[:code], i[:format], :organism => organism_code)
  end
end
finder_find(term) click to toggle source
# File lib/rbbt/rest/entity/finder.rb, line 4
def finder_find(term)
  return [] unless settings.respond_to? :finder and not settings.finder.nil? and not settings.finder.instances.empty?
  results = settings.finder.find(term)

  results.uniq.collect{|r| 
    info = r.info
    format_string = [info[:namespace], info[:format]].compact * ":"

    info[:code] = r

    if not format_string.empty?
      info[:value] = r + " [#{format_string}]" 
    else
      info[:value] = r
    end

    score = info.delete :score

    if Array === score
      info[:score] = score[0]
      info[:best] = score[1]
    end

    info[:format] ||= info[:type]

    info 
  }.sort_by{|i| i[:score] || 0}.reverse
end
list(list, list_id = nil, text = nil) click to toggle source
# File lib/rbbt/rest/entity/helpers.rb, line 17
def list(list, list_id = nil, text = nil)
  partial_render('entity_partials/entity_list', :list => list, :list_id => list_id, :text => text)
end
list_container_render(container) click to toggle source
# File lib/rbbt/rest/entity/list_container.rb, line 20
def list_container_render(container)
  partial_render('entity_partials/list_container', :container => container)
end
list_hash(list) click to toggle source
# File lib/rbbt/rest/entity/helpers.rb, line 5
def list_hash(list)
  return {:entities => list} unless list.respond_to? :base_entity
  info = list.info
  info.delete :annotation_types
  info.delete :annotated_array
  {:entities => list, :info => list.info, :entity_type => list.base_entity}
end
list_json(list) click to toggle source
# File lib/rbbt/rest/entity/helpers.rb, line 13
def list_json(list)
  list_hash(list).to_json
end
locate_entity_action_template(entity, action) click to toggle source
# File lib/rbbt/rest/entity/locate.rb, line 66
def locate_entity_action_template(entity, action)
  resources = resources_for_entity(entity)

  types = entity.annotation_types.collect{|e| e.to_s}
  types << "Default"

  path = nil
  types.each do |type|
    next if path
    path = locate_server_file(["entity", type, action]*"/", resources, 'haml')
    check_file = locate_server_file(["entity", type, action]*"/", resources, 'check')
    raise "This action was rejected: #{ action }" if reject?(check_file, binding)
  end

  raise TemplateMissing, "Template not found for entity action #{action}: #{ entity } (#{entity.annotation_types * ", "})" if path.nil?

  path
end
locate_entity_list_action_template(list, action) click to toggle source
# File lib/rbbt/rest/entity/locate.rb, line 143
def locate_entity_list_action_template(list, action)
  resources = resources_for_entity(list)

  types = list.annotation_types.collect{|e| e.to_s}
  types << "Default"

  path = nil
  types.each do |type|
    next if path
    path = locate_server_file(["entity_list", type, action]*"/", resources, 'haml')
    check_file = locate_server_file(["entity_list", type, action]*"/", resources, 'check')
    raise "This action was rejected: #{ action }" if reject?(check_file, binding)
  end

  raise TemplateMissing, "Template not found for entity list action #{action} (#{list.annotation_types * ", "})" if path.nil?

  path
end
locate_entity_list_template(list) click to toggle source

{{{ ENTITY LIST

# File lib/rbbt/rest/entity/locate.rb, line 127
def locate_entity_list_template(list)
  resources = resources_for_entity(list)

  types = list.annotation_types.collect{|e| e.to_s}
  types << "Default"

  path = nil
  types.each do |type|
    path ||= locate_server_file(["entity_list", type]*"/", resources, 'haml')
  end

  raise TemplateMissing, "Template not found for list (#{list.annotation_types * ", "})" if path.nil?

  path
end
locate_entity_map_action_template(map, action) click to toggle source
# File lib/rbbt/rest/entity/locate.rb, line 221
def locate_entity_map_action_template(map, action)
  resources = entity_resources

  field = map.key_field

  if map.entity_templates[field] 
    types = map.entity_templates[field].annotation_types
  else
    types = [Entity.formats[field]].compact
  end
  types += ["Default"]

  path = nil
  types.each do |type|
    next if path
    path = locate_server_file(["entity_map", type, action]*"/", resources, 'haml')
    check_file = locate_server_file(["entity_map", type, action]*"/", resources, 'check')
    raise "This action was rejected: #{ action }" if reject?(check_file, binding)
  end

  raise TemplateMissing, "Template not found for entity map action #{action} (#{field}--#{map.fields.first})" if path.nil?

  path
end
locate_entity_map_template(type, column = nil) click to toggle source

{{{ ENTITY MAP

# File lib/rbbt/rest/entity/locate.rb, line 205
def locate_entity_map_template(type, column = nil)
  resources = entity_resources

  types = [type.to_s]
  types << "Default"

  path = nil
  types.each do |t|
    path ||= locate_server_file(["entity_map", t]*"/", resources, 'haml')
  end

  raise TemplateMissing, "Template not found for map (#{type}--#{column})" if path.nil?

  path
end
locate_entity_template(entity) click to toggle source

{{{ ENTITY

# File lib/rbbt/rest/entity/locate.rb, line 50
def locate_entity_template(entity)
  resources = resources_for_entity(entity)

  types = entity.annotation_types.collect{|e| e.to_s}
  types << "Default"

  path = nil
  types.each do |type|
    path ||= locate_server_file(["entity", type]*"/", resources, 'haml')
  end

  raise TemplateMissing, "Template not found for entity: #{ entity } (#{entity.annotation_types * ", "})" if path.nil?

  path
end
page_action(path = nil) click to toggle source
# File lib/rbbt/rest/entity/helpers.rb, line 110
def page_action(path = nil)
  path = request.path_info if path.nil?

  case page_type
  when "entity_action", "entity_list_action"
    return Entity::REST.restore_element(path.split("/")[3])
  else
    return nil
  end
end
page_entity(path = nil) click to toggle source
# File lib/rbbt/rest/entity/helpers.rb, line 97
def page_entity(path = nil)
  path = request.path_info if path.nil?

  case page_type
  when "entity"
    return Entity::REST.restore_element(path.split("/")[3])
  when "entity_action"
    return Entity::REST.restore_element(path.split("/")[4])
  else
    return nil
  end
end
page_entity_base_type(path = nil) click to toggle source
# File lib/rbbt/rest/entity/helpers.rb, line 158
def page_entity_base_type(path = nil)
  page_entity_type.split(":").first
end
page_entity_list(path = nil) click to toggle source
# File lib/rbbt/rest/entity/helpers.rb, line 121
def page_entity_list(path = nil)
  path = request.path_info if path.nil?

  case page_type
  when "entity_list"
    return Entity::REST.restore_element(path.split("/")[3])
  when "entity_list_action"
    return Entity::REST.restore_element(path.split("/")[4])
  else
    return nil
  end
end
page_entity_map(path = nil) click to toggle source
# File lib/rbbt/rest/entity/helpers.rb, line 134
def page_entity_map(path = nil)
  path = request.path_info if path.nil?

  case page_type
  when "entity_map"
    return Entity::REST.restore_element(path.split("/")[4])
  when "entity_map_action"
    return Entity::REST.restore_element(path.split("/")[5])
  else
    return nil
  end
end
page_entity_map_column(path = nil) click to toggle source
# File lib/rbbt/rest/entity/helpers.rb, line 162
def page_entity_map_column(path = nil)
  path = request.path_info if path.nil?

  case page_type
  when  "entity_map"
    return Entity::REST.restore_element(path.split("/")[3])
  else
    return nil
  end
end
page_entity_type(path = nil) click to toggle source
# File lib/rbbt/rest/entity/helpers.rb, line 147
def page_entity_type(path = nil)
  path = request.path_info if path.nil?

  case page_type
  when "entity", "entity_list", "entity_action", "entity_list_action", "entity_map"
    return Entity::REST.restore_element(path.split("/")[2])
  else
    return nil
  end
end
page_type(path = nil) click to toggle source
# File lib/rbbt/rest/entity/helpers.rb, line 76
def page_type(path = nil)
  path = request.path_info if path.nil?

  case
  when path.match(/^\/entity\//)
    return "entity"; 
  when path.match(/^\/entity_action\//)
    return "entity_action"; 
  when path.match(/^\/entity_list\//)
    return "entity_list"; 
  when path.match(/^\/entity_list_action\//)
    return "entity_list_action"; 
  when path.match(/^\/entity_map\//)
    return "entity_map"; 
  when path.match(/^\/entity_map_action\//)
    return "entity_map_action"; 
  else
    return nil;
  end
end
reject?(check_file, binding) click to toggle source

{{{ CHECKS

# File lib/rbbt/rest/entity/locate.rb, line 19
def reject?(check_file, binding)
  return false if check_file.nil? or not check_file.exists?
  begin
    code = Open.read(check_file)
    accept = eval code, binding, check_file, 0
    action = File.basename(check_file).sub('.check', '')
    Log.debug{"Checking action template: #{action} - #{accept ? 'accepted' : 'rejected'}"}
    return ! accept
  rescue 
    Log.exception $!
    return true
  end
  false
end
reject_template(path, binding) click to toggle source
# File lib/rbbt/rest/entity/locate.rb, line 34
def reject_template(path, binding)
  return false if path.nil?
  check_file = path.sub(/\.haml$/, '.check')

  if Path === path
    path.annotate check_file 
    return false unless check_file.exists?
  else
    return false unless File.exist?(check_file)
  end

  reject?(check_file, binding)
end
remove_favourite_entity(entity) click to toggle source
# File lib/rbbt/rest/entity/favourites.rb, line 36
def remove_favourite_entity(entity)
  raise "You need to login to have favourites" unless authorized?

  entity_type = entity.base_type
  dir = Path.setup(File.join(settings.favourites_dir, user))

  if (file = dir[entity_type].find).exists?
    entities = Annotated.load_tsv(TSV.open(file))
    entities -= [entity]
    if entities.any?
      Open.write(file, Annotated.tsv(entities.uniq, :all).to_s)
    else
      FileUtils.rm file
    end
  end
end
remove_favourite_entity_list(entity_type, list) click to toggle source
# File lib/rbbt/rest/entity/favourites.rb, line 83
def remove_favourite_entity_list(entity_type, list)
  raise "You need to login to have favourites" unless authorized?

  dir = Path.setup(File.join(settings.favourite_lists_dir, user))

  if (file = dir[entity_type].find).exists?
    lists = Open.read(file).split("\n")
    lists -= [list]
    if lists.any?
      Open.write(file, lists * "\n")
    else
      FileUtils.rm file
    end
  end
end
remove_favourite_entity_map(entity_type, column, map) click to toggle source
# File lib/rbbt/rest/entity/favourites.rb, line 135
def remove_favourite_entity_map(entity_type, column, map)
  raise "You need to login to have favourites" unless authorized?

  column = column.gsub('/', '--')

  dir = Path.setup(File.join(settings.favourite_maps_dir, user))

  if (file = dir[entity_type][column].find).exists?
    maps = Open.read(file).split("\n")
    maps -= [map]
    if maps.any?
      Open.write(file, maps * "\n")
    else
      FileUtils.rm file
    end
  end
end
resources_for_entity(entity) click to toggle source
# File lib/rbbt/rest/entity/locate.rb, line 11
def resources_for_entity(entity)
  resources = entity_resources
  resources.unshift entity.dir.www.views if entity.respond_to? :dir and Path === entity.dir
  resources
end
setup_entity(type, entity, params) click to toggle source
# File lib/rbbt/rest/entity/render.rb, line 7
def setup_entity(type, entity, params)
  base_type, format = type.split ":"
  entity_class = case
                 when Entity.formats.include?(base_type)
                   Entity.formats[base_type] 
                 when Entity.formats.include?(format)
                   Entity.formats[format] 
                 else
                   nil
                 end

  raise "Unknown entity type: #{ type }" if entity_class.nil?

  raise "Entity type not supported in REST: #{ type }" unless entity_class.include? Entity::REST

  entity_annotations = {}
  entity_class.annotations.each do |annotation|
    value = consume_parameter annotation, params
    value = Entity::REST.restore_element(value) if String === value
    value = false if value == "false"
    value = true if value == "true"
    entity_annotations[annotation] = value
  end

  entity.extend entity_class
  entity_class.setup_hash(entity, entity_annotations)
  entity.format = format if format and entity.respond_to? :format

  entity
end