Module: EntityRESTHelpers

Defined in:
lib/rbbt/rest/entity/finder.rb,
lib/rbbt/rest/entity/locate.rb,
lib/rbbt/rest/entity/render.rb,
lib/rbbt/rest/entity/helpers.rb,
lib/rbbt/rest/entity/favourites.rb,
lib/rbbt/rest/entity/action_card.rb,
lib/rbbt/rest/entity/entity_card.rb,
lib/rbbt/rest/entity/list_container.rb,
lib/rbbt/rest/entity/entity_map_card.rb,
lib/rbbt/rest/entity/entity_list_card.rb,
lib/rbbt/rest/entity/action_controller.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.entity_resourcesObject



3
4
5
# File 'lib/rbbt/rest/entity/locate.rb', line 3

def self.entity_resources
  @entity_resources ||= []
end

Instance Method Details

#action_card_render(card, &block) ⇒ Object



27
28
29
# 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) ⇒ Object



40
41
42
# 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) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/rbbt/rest/entity/helpers.rb', line 29

def action_parameters(values = nil, action_options = {}, form_options = {}, &block)
  o = Object.new
  o.extend AnnotatedModule

  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]
  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 = locals.merge(form_options)

  html_tag :div, partial_render('partials/form', locals), {:class => 'action_parameters ui raised segment'}.merge(action_options)
end

#action_parameters_idObject



20
21
22
23
24
25
26
27
# File 'lib/rbbt/rest/entity/helpers.rb', line 20

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) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 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) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# 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) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# 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) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# 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 = {}) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/rbbt/rest/entity/render.rb', line 50

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}]"

  layout_file = layout ? locate_template("layout") : nil

  render(template_file, locals, layout_file, "Action #{ action }: #{ entity }")
end

#entity_card_render(card, &block) ⇒ Object



23
24
25
# File 'lib/rbbt/rest/entity/entity_card.rb', line 23

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 = {}) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/rbbt/rest/entity/render.rb', line 76

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}]"

  layout_file = layout ? locate_template("layout") : nil

  render(template_file, locals, layout_file, "Action #{ action } for list: #{ id }")
end

#entity_list_card_render(card) ⇒ Object



23
24
25
# File 'lib/rbbt/rest/entity/entity_list_card.rb', line 23

def entity_list_card_render(card)
  partial_render('entity_partials/entity_list_card', :card => card)
end

#entity_list_render(list, id) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/rbbt/rest/entity/render.rb', line 63

def entity_list_render(list, id)
  template_file = locate_entity_list_template(list)

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

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

  layout_file = layout ? locate_template("layout") : nil

  render(template_file, locals, layout_file, "Entity list: #{ id }")
end

#entity_map_action_render(map, action, id, params = {}) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/rbbt/rest/entity/render.rb', line 103

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}]"

  layout_file = layout ? locate_template("layout") : nil

  render(template_file, locals, layout_file, "Action #{ action } for map: #{ id }")
end

#entity_map_card_render(card) ⇒ Object



12
13
14
# 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) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/rbbt/rest/entity/render.rb', line 89

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}]"

  layout_file = layout ? locate_template("layout") : nil

  render(template_file, locals, layout_file, "Entity map: #{ map_id }")
end

#entity_render(entity, params = {}) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rbbt/rest/entity/render.rb', line 36

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}]"

  layout_file = layout ? locate_template("layout") : nil

  render(template_file, locals, layout_file, "Entity: #{ entity }")
end

#entity_resourcesObject



7
8
9
# File 'lib/rbbt/rest/entity/locate.rb', line 7

def entity_resources
  [Rbbt.share.views.find(:lib)] +  EntityRESTHelpers.entity_resources
end

#favourite_entitiesObject

{{{ Entities



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 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_listsObject

{{{ Entity Lists



55
56
57
58
59
60
61
62
63
64
65
66
# 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_mapsObject

{{{ Entity Maps



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# 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) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/rbbt/rest/entity/locate.rb', line 67

def find_all_entity_action_templates(entity, check = false)
  paths = []

  if entity.respond_to? :dir and Path === entity.dir
    paths.concat find_all_entity_action_templates_from_resource(entity.dir.www.views, entity)
  end

  entity_resources.each do |resource|
    paths.concat find_all_entity_action_templates_from_resource(resource, entity)
  end

  entity_resources.each do |resource|
    paths.concat find_all_entity_action_templates_from_resource(resource, "Default")
  end

  if check
    paths = paths.reject do |path|
      check_file = path.sub(/\.haml$/, '.check')
      case
      when (path.basename == "edit.haml" or path.basename == 'new.haml')
        true
      when File.exists?(check_file)
        begin
          Log.debug{"Checking action template: #{path}"}
          code = File.read(check_file)
          accept = eval code
          not accept
        rescue
          Log.debug{"Error Checking action template #{path}: #{$!.message}"}
          true
        end
      else
        false
      end
    end
  end

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

#find_all_entity_action_templates_from_resource(resource, entity) ⇒ Object

{{{ ENTITY ACTION



57
58
59
60
61
62
63
64
65
# File 'lib/rbbt/rest/entity/locate.rb', line 57

def find_all_entity_action_templates_from_resource(resource, entity)
  if entity == "Default" 
    resource.entity["Default"].glob("*.haml").sort
  else
    entity.annotation_types.collect do |annotation|
      resource.entity[annotation].glob('*.haml')
    end.compact.flatten.sort
  end
end

#find_all_entity_list_action_templates(list, check = false) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/rbbt/rest/entity/locate.rb', line 203

def find_all_entity_list_action_templates(list, check = false)
  paths = []

  if list.respond_to? :dir and Path === list.dir
    paths.concat find_all_entity_list_action_templates_from_resource(list.dir.www.views, list)
  end

  entity_resources.each do |resource|
    paths.concat find_all_entity_list_action_templates_from_resource(resource, list)
  end

  entity_resources.each do |resource|
    paths.concat find_all_entity_list_action_templates_from_resource(resource, "Default")
  end

  if check
    paths = paths.reject do |path|
      check_file = path.sub(/\.haml$/, '.check')
      case
      when (path.basename == "edit.haml" or path.basename == 'new.haml')
        true
      when File.exists?(check_file)
        begin
          Log.debug{ "Checking action template: #{path}" }
          code = File.read(check_file)
          accept = eval code
          not accept
        rescue
          Log.debug{ "Error Checking action template #{path}: #{$!.message}" }
          true
        end
      else
        false
      end
    end
  end

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

#find_all_entity_list_action_templates_from_resource(resource, entity) ⇒ Object

{{{ ENTITY LIST ACTION



192
193
194
195
196
197
198
199
200
201
# File 'lib/rbbt/rest/entity/locate.rb', line 192

def find_all_entity_list_action_templates_from_resource(resource, entity)

  if entity == "Default" 
    resource.entity_list["Default"].glob("*.haml").sort
  else
    entity.annotation_types.collect do |annotation|
      resource.entity_list[annotation].glob('*.haml')
    end.compact.flatten.sort
  end
end

#find_all_entity_map_action_templates(map, check = false) ⇒ Object



333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# File 'lib/rbbt/rest/entity/locate.rb', line 333

def find_all_entity_map_action_templates(map, check = false)
  paths = []

  entity_resources.each do |resource|
    paths.concat find_all_entity_map_action_templates_from_resource(resource, map)
  end

  if check
    paths = paths.reject do |path|
      check_file = path.sub(/\.haml$/, '.check')
      case
      when (path.basename == "edit.haml" or path.basename == 'new.haml')
        true
      when File.exists?(check_file)
        begin
          Log.debug{ "Checking action template: #{path}" }
          code = File.read(check_file)
          accept = eval code
          not accept
        rescue
          Log.debug{ "Error Checking action template #{path}: #{$!.message}" }
          true
        end
      else
        false
      end
    end
  end

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

#find_all_entity_map_action_templates_from_resource(resource, map) ⇒ Object

{{{ ENTITY MAP ACTION



317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'lib/rbbt/rest/entity/locate.rb', line 317

def find_all_entity_map_action_templates_from_resource(resource, map)
  field = map.key_field

  if map.entity_templates[field] 
    annotation_types = map.entity_templates[field].annotation_types
  else
    annotation_types = [Entity.formats[field]].compact
  end

  annotation_types += ["Default"]

  annotation_types.collect do |annotation|
    resource.entity_map[annotation].glob('*.haml')
  end.compact.flatten
end

#find_entity_url(term) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# 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) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# 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) ⇒ Object



16
17
18
# File 'lib/rbbt/rest/entity/helpers.rb', line 16

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) ⇒ Object



19
20
21
# File 'lib/rbbt/rest/entity/list_container.rb', line 19

def list_container_render(container)
  partial_render('entity_partials/list_container', :container => container)
end

#list_hash(list) ⇒ Object



4
5
6
7
8
9
10
# File 'lib/rbbt/rest/entity/helpers.rb', line 4

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) ⇒ Object



12
13
14
# File 'lib/rbbt/rest/entity/helpers.rb', line 12

def list_json(list)
  list_hash(list).to_json
end

#locate_entity_action_template(entity, action) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/rbbt/rest/entity/locate.rb', line 125

def locate_entity_action_template(entity, action)

  if entity.respond_to? :dir and Path === entity.dir
    path = locate_entity_action_template_from_resource(entity.dir.www.views, entity, action)
    return path if path and path.exists?
  end

  entity_resources.each do |resource|
    path = locate_entity_action_template_from_resource(resource, entity, action)
    return path if path and path.exists?
  end

  entity_resources.each do |resource|
    path = locate_entity_action_template_from_resource(resource, "Default", action)
    return path if path and path.exists?
  end

  raise "Template not found for action #{action}: #{ entity } (#{entity.annotation_types * ", "})"
end

#locate_entity_action_template_from_resource(resource, entity, action) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/rbbt/rest/entity/locate.rb', line 107

def locate_entity_action_template_from_resource(resource, entity, action)
  if entity == "Default" 
    path = resource.entity["Default"][action.to_s + ".haml"]
    if path.exists?
      return path
    else
      return nil
    end
  end

  entity.annotation_types.each do |annotation|
    path = resource.entity[annotation][action.to_s + ".haml"]
    return path if path.exists?
  end

  nil
end

#locate_entity_list_action_template(list, action) ⇒ Object



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/rbbt/rest/entity/locate.rb', line 262

def locate_entity_list_action_template(list, action)

  if list.respond_to? :dir and Path === list.dir
    path = locate_entity_list_action_template_from_resource(list.dir.www.views, list, action)
    return path if path and path.exists?
  end

  entity_resources.each do |resource|
    path = locate_entity_list_action_template_from_resource(resource, list, action)
    return path if path and path.exists?
  end

  entity_resources.each do |resource|
    path = locate_entity_list_action_template_from_resource(resource, "Default", action)
    return path if path and path.exists?
  end

  raise "Template not found for list #{ action } (#{list.annotation_types * ", "})"
end

#locate_entity_list_action_template_from_resource(resource, list, action) ⇒ Object



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/rbbt/rest/entity/locate.rb', line 244

def locate_entity_list_action_template_from_resource(resource, list, action)
  if list == "Default" 
    path = resource.entity_list["Default"][action.to_s + ".haml"]
    if path.exists?
      return path
    else
      return nil
    end
  end

  list.annotation_types.each do |annotation|
    path = resource.entity_list[annotation][action.to_s + ".haml"]
    return path if path.exists?
  end

  nil
end

#locate_entity_list_template(list) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/rbbt/rest/entity/locate.rb', line 165

def locate_entity_list_template(list)

  if list.respond_to? :dir and Path === list.dir
    list_views = list.dir.www.views

    list.annotation_types.each do |annotation|
      path = list_views.entity_list[annotation.to_s + ".haml"]
      return path if path.exists?
    end
  end

  entity_resources.each do |resource|
    path = locate_entity_list_template_from_resource(resource, list)
    return path if path and path.exists?
  end

  entity_resources.each do |resource|
    path = locate_entity_list_template_from_resource(resource, "Default")
    return path if path and path.exists?
  end

  raise "Template not found for list (#{list.annotation_types * ", "})"
end

#locate_entity_list_template_from_resource(resource, list) ⇒ Object

{{{ ENTITY LIST



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/rbbt/rest/entity/locate.rb', line 147

def locate_entity_list_template_from_resource(resource, list)
  if list == "Default" 
    path = resource.entity_list["Default.haml"]
    if path.exists?
      return path
    else
      return nil
    end
  end

  list.annotation_types.each do |annotation|
    path = resource.entity_list[annotation.to_s + ".haml"]
    return path if path.exists?
  end

  nil
end

#locate_entity_map_action_template(map, action) ⇒ Object



385
386
387
388
389
390
391
392
393
# File 'lib/rbbt/rest/entity/locate.rb', line 385

def locate_entity_map_action_template(map, action)

  entity_resources.each do |resource|
    path = locate_entity_map_action_template_from_resource(resource, map, action)
    return path if path and path.exists?
  end

  raise "Template not found for map #{ action } (#{map.key_field * ", "})"
end

#locate_entity_map_action_template_from_resource(resource, map, action) ⇒ Object



366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
# File 'lib/rbbt/rest/entity/locate.rb', line 366

def locate_entity_map_action_template_from_resource(resource, map, action)
  field = map.key_field

  if map.entity_templates[field] 
    annotation_types = map.entity_templates[field].annotation_types
  else
    annotation_types = [Entity.formats[field]].compact
  end

  annotation_types += ["Default"]

  annotation_types.each do |annotation|
    path = resource.entity_map[annotation][action.to_s + ".haml"]
    return path if path.exists?
  end

  nil
end

#locate_entity_map_template(type, column) ⇒ Object



300
301
302
303
304
305
306
307
308
309
310
311
312
# File 'lib/rbbt/rest/entity/locate.rb', line 300

def locate_entity_map_template(type, column)
  entity_resources.each do |resource|
    path = locate_entity_map_template_from_resource(resource, type)
    return path if path and path.exists?
  end

  entity_resources.each do |resource|
    path = locate_entity_map_template_from_resource(resource, "Default")
    return path if path and path.exists?
  end

  raise "Template not found for list (#{type}--#{column})"
end

#locate_entity_map_template_from_resource(resource, type) ⇒ Object

{{{ ENTITY MAP



284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/rbbt/rest/entity/locate.rb', line 284

def locate_entity_map_template_from_resource(resource, type)
  if type == "Default" 
    path = resource.entity_map["Default.haml"]
    if path.exists?
      return path
    else
      return nil
    end
  end

  path = resource.entity_map[type.to_s + ".haml"]
  return path if path.exists?

  nil
end

#locate_entity_template(entity) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rbbt/rest/entity/locate.rb', line 31

def locate_entity_template(entity)

  if entity.respond_to? :dir and Path === entity.dir
    entity_views = entity.dir.www.views

    entity.annotation_types.each do |annotation|
      path = entity_views.entity[annotation.to_s + ".haml"]
      return path if path.exists?
    end
  end

  entity_resources.each do |resource|
    path = locate_entity_template_from_resource(resource, entity)
    return path if path and path.exists?
  end

  entity_resources.each do |resource|
    path = locate_entity_template_from_resource(resource, "Default")
    return path if path and path.exists?
  end

  raise "Template not found for entity: #{ entity } (#{entity.annotation_types * ", "})"
end

#locate_entity_template_from_resource(resource, entity) ⇒ Object

{{{ ENTITY



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rbbt/rest/entity/locate.rb', line 13

def locate_entity_template_from_resource(resource, entity)
  if entity == "Default" 
    path = resource.entity["Default.haml"]
    if path.exists?
      return path
    else
      return nil
    end
  end

  entity.annotation_types.each do |annotation|
    path = resource.entity[annotation.to_s + ".haml"]
    return path if path.exists?
  end

  nil
end

#page_action(path = nil) ⇒ Object



103
104
105
106
107
108
109
110
111
112
# File 'lib/rbbt/rest/entity/helpers.rb', line 103

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) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/rbbt/rest/entity/helpers.rb', line 90

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) ⇒ Object



151
152
153
# File 'lib/rbbt/rest/entity/helpers.rb', line 151

def page_entity_base_type(path = nil)
  page_entity_type.split(":").first
end

#page_entity_list(path = nil) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/rbbt/rest/entity/helpers.rb', line 114

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) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/rbbt/rest/entity/helpers.rb', line 127

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) ⇒ Object



155
156
157
158
159
160
161
162
163
164
# File 'lib/rbbt/rest/entity/helpers.rb', line 155

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) ⇒ Object



140
141
142
143
144
145
146
147
148
149
# File 'lib/rbbt/rest/entity/helpers.rb', line 140

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) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/rbbt/rest/entity/helpers.rb', line 69

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

#remove_favourite_entity(entity) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# 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) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# 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) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# 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

#setup_entity(type, entity, params) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 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?

  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