Module: Entity::REST

Defined in:
lib/rbbt/rest/entity/rest.rb

Constant Summary collapse

USE_ENSEMBL =
true
REST_ENTITIES =
Set.new

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.clean_element(elem) ⇒ Object

{{{ MISC



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

def self.clean_element(elem)
  elem.gsub('/', '-..-').gsub('%', 'o-o').gsub('[','(.-(').gsub(']',').-)')
end

.entity_action_url(entity, type, action, params = {}) ⇒ Object



92
93
94
95
96
# File 'lib/rbbt/rest/entity/rest.rb', line 92

def self.entity_action_url(entity, type, action, params = {})
  url = File.join('/', 'entity_action', Entity::REST.clean_element(type.to_s), action.to_s, entity)
  url << "?" << Misc.hash2GET_params(params) if params.any?
  url
end

.entity_list_action_url(list, type, action, params) ⇒ Object



102
103
104
105
106
# File 'lib/rbbt/rest/entity/rest.rb', line 102

def self.entity_list_action_url(list, type, action, params)
  url = File.join('/', 'entity_list_action', Entity::REST.clean_element(type.to_s), action.to_s, Entity::REST.clean_element(list))
  url << "?" << Misc.hash2GET_params(params) if params.any?
  url
end

.entity_list_url(list, type) ⇒ Object



98
99
100
# File 'lib/rbbt/rest/entity/rest.rb', line 98

def self.entity_list_url(list, type)
  File.join('/', 'entity_list', Entity::REST.clean_element(type.to_s), Entity::REST.clean_element(list))
end

.entity_map_action_url(map, type, column, action) ⇒ Object



115
116
117
118
119
120
# File 'lib/rbbt/rest/entity/rest.rb', line 115

def self.entity_map_action_url(map, type, column, action)
  type = Entity::REST.clean_element(type.to_s)
  column = Entity::REST.clean_element(column)
  map = Entity::REST.clean_element(map)
  File.join('/', 'entity_map_action', type,  column, action, map)
end

.entity_map_url(map, type, column) ⇒ Object



108
109
110
111
112
113
# File 'lib/rbbt/rest/entity/rest.rb', line 108

def self.entity_map_url(map, type, column)
  type = Entity::REST.clean_element(type.to_s)
  column = Entity::REST.clean_element(column)
  map = Entity::REST.clean_element(map)
  File.join('/', 'entity_map', type,  column, map)
end

.entity_url(entity, type = nil, params = nil) ⇒ Object

{{{ URLS



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

def self.entity_url(entity, type = nil, params = nil)
  if type.nil?
    type = entity.annotation_types.last.to_s
    type << ":" << entity.format if entity.respond_to? :format and entity.format
  end

  if params.nil?
    if entity.respond_to? :entity_link_params
      params = entity.entity_link_params
    else
      params = entity.respond_to?(:info) ? entity.info : {}
    end
  end


  url = File.join('/', 'entity', Entity::REST.clean_element(type.to_s), entity) 
  url << "?" << Misc.hash2GET_params(params) if params.any?
  url
end

.included(base) ⇒ Object



10
11
12
# File 'lib/rbbt/rest/entity/rest.rb', line 10

def self.included(base)
  REST_ENTITIES << base
end

.restore_element(elem) ⇒ Object



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

def self.restore_element(elem)
  CGI.unescape(CGI.unescape(elem.gsub('-..-', '/').gsub('o-o', '%').gsub('(.-(','[').gsub(').-)',']')))
end

.setup(mod) ⇒ Object



24
25
26
27
28
# File 'lib/rbbt/rest/entity/rest.rb', line 24

def self.setup(mod)
  mod.module_eval do
    include Entity::REST
  end
end

Instance Method Details



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/rbbt/rest/entity/rest.rb', line 155

def action_link(action, text = nil, options = {})
  return self.collect{|e| e.link(action, text) } if Array === self

  klasses = self.klasses
  klasses << 'entity_action'

  attributes, link_params = process_link_options({:title => [action, self] * ": " }.merge(options))

  attributes[:class] << klasses
  attributes[:href] = Entity::REST.entity_action_url(self, entity_type.to_s, action, link_params)

  if text.nil? or (String === text and text.empty?)
    text = self.respond_to?(:name)? self.name || self : self if text.nil?
    text = [text, action] * "&rarr;"
  end
  Misc.html_tag('a', text, attributes)
end

#base_typeObject



38
39
40
# File 'lib/rbbt/rest/entity/rest.rb', line 38

def base_type
  annotation_types.select{|mod| mod.include? Entity::REST }.first.to_s
end


30
31
32
33
34
35
36
# File 'lib/rbbt/rest/entity/rest.rb', line 30

def entity_link_params
  info = self.info.dup
  info.delete :format
  info.delete :annotation_types
  info.delete :annotated_array
  info
end

#entity_typeObject



49
50
51
# File 'lib/rbbt/rest/entity/rest.rb', line 49

def entity_type
  entity_type = self.respond_to?(:format) ? [base_type, format].compact.join(":") : base_type
end

#klassesObject



42
43
44
45
46
47
# File 'lib/rbbt/rest/entity/rest.rb', line 42

def klasses
  klasses = []
  klasses << base_type.to_s if base_type
  klasses << format if self.respond_to? :format and format
  klasses.collect{|klass| klass.gsub(/[^\w]/, '_') }
end

{{{ LINKS



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/rbbt/rest/entity/rest.rb', line 125

def link(text = nil, options = {})
  #return self.tap{|a| a.extend AnnotatedArray}.collect{|e| e.link(text, options) } if Array === self
  return self.collect{|e| e.nil? ? nil : e.link(text, options) } if Array === self
  return self.split(";").collect{|e| self.annotate(e).link(text, options) } * ", " if self.include? ";"
  return nil if self.empty?

  klasses = self.klasses
  klasses <<  'entity'

  attributes, link_params = process_link_options(options)

  attributes[:class] << klasses
  attributes[:href] = Entity::REST.entity_url(self, entity_type.to_s, link_params)
  attributes["attr-entity_id"] = self.to_s

  attributes["data-entity-type"] = self.base_type
  attributes["data-entity"] = self.to_s
  attributes["data-entity-id"] = self.respond_to?(:default)? self.default || self.to_s : self.to_s

  begin
    text = self.respond_to?(:name)? self.name || self : self if text.nil?
  rescue
    text = self
  end
  attributes[:title] = text if attributes[:title].nil?

  Misc.html_tag('a', text, attributes)
end


203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/rbbt/rest/entity/rest.rb', line 203

def list_action_link(action, text = nil, id = nil, options = {})
  id = options[:id] || Misc.digest((self * "|").inspect) if id.nil? or (String === id and id.empty?)
  text = [id, action] * "&rarr;" if text.nil? or (String === text and text.strip.empty?)

  reuse = options.delete(:reuse)
  reuse = options.delete("reuse") if reuse.nil?
  reuse = true if reuse.nil?

  Entity::List.save_list(entity_type.to_s, id, self) unless reuse and File.exists? Entity::List.list_file(entity_type.to_s, id, self)

  klasses = self.klasses
  klasses << 'entity_list_action'

  attributes, link_params = process_link_options({:title => [action, id] * ": " }.merge(options), false)

  attributes[:class] ||= ''
  attributes[:class] << klasses
  attributes[:href] = Entity::REST.entity_list_action_url(id, entity_type.to_s, action, link_params)

  attributes[:title] = id if attributes[:title].nil?
  Misc.html_tag('a', text, attributes)
end


173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/rbbt/rest/entity/rest.rb', line 173

def list_link(text = nil, id = nil, options = {})
  options = Misc.add_defaults options, :ensembl => USE_ENSEMBL
  ensembl = Misc.process_options options, :ensembl

  if ensembl and self.respond_to? :ensembl and self.format !~ /^Ensembl /
    return self.ensembl.compact.uniq.list_link(text, id, options.merge({:ensembl => false}))
  end

  text = self.length if text.nil? or text == :length or (String === text and text.strip.empty?)
  id = options[:id] || Misc.digest((self * "|").inspect) if id.nil? or (String === id and id.empty?)

  reuse = options.delete(:reuse)
  reuse = options.delete("reuse") if reuse.nil?
  reuse = true if reuse.nil?

  Entity::List.save_list(entity_type.to_s, id, self) unless reuse and File.exists?(Entity::List.list_file(entity_type.to_s, id))

  klasses = self.klasses
  klasses << 'entity_list'

  attributes, link_params = process_link_options(options)

  attributes[:class] ||= ''
  attributes[:class] << klasses
  attributes[:href] = Entity::REST.entity_list_url(id, entity_type.to_s)

  attributes[:title] = id
  Misc.html_tag('a', text, attributes)
end


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/rbbt/rest/entity/rest.rb', line 53

def process_link_options(options = {}, include_entity_params = true)
  attributes = {}
  link_params = include_entity_params ? entity_link_params : {}

  %w(class style title).each do |at|
    attributes[at.to_sym] = options.delete(at.to_sym) || options.delete(at.to_s) || nil
  end

  attributes[:class] = attributes[:class].split(" ") if String === attributes[:class]

  attributes[:class] = [] if attributes[:class].nil?

  link_params = link_params.merge(options)

  [attributes, link_params]
end