intermine 0.98.10 → 0.98.11

Sign up to get free protection for your applications and to get access to all the features.
@@ -46,6 +46,8 @@ module InterMine::Lists
46
46
  #
47
47
  class List
48
48
 
49
+ include Enumerable
50
+
49
51
  # The name of the list. This can be changed at any time.
50
52
  attr_reader :name
51
53
 
@@ -164,23 +166,10 @@ module InterMine::Lists
164
166
  # end
165
167
  #
166
168
  def each
167
- query.each_result {|r| yield r}
169
+ query.results.each {|r| yield r}
168
170
  return self
169
171
  end
170
172
 
171
- # Return a list composed of the results of the elements of
172
- # this list process by the given block
173
- #
174
- # symbols = list.map {|gene| gene.symbol}
175
- #
176
- def map
177
- ret = []
178
- query.each_result {|r|
179
- ret.push(yield r)
180
- }
181
- return ret
182
- end
183
-
184
173
  # Used to create a new list from the contents of this one. This can be used
185
174
  # to define a sub-list
186
175
  #
@@ -298,6 +287,31 @@ module InterMine::Lists
298
287
  return self
299
288
  end
300
289
 
290
+ # Add tags to the list
291
+ #
292
+ # Updates the current tags by adding tags to the list.
293
+ #
294
+ def add_tags(*tags)
295
+ @tags = @manager.add_tags(self, tags)
296
+ end
297
+
298
+ # Remove one or more tags from the list
299
+ #
300
+ # If the tags are not currently associated with the list,
301
+ # they will be ignored.
302
+ #
303
+ def remove_tags(*tags)
304
+ to_remove = tags.select {|t| @tags.include? t}
305
+ unless to_remove.empty?
306
+ @tags = @manager.remove_tags(self, to_remove)
307
+ end
308
+ end
309
+
310
+ # Update this lists tags with the current tags on the server.
311
+ def update_tags
312
+ @tags = @manager.tags_for(self)
313
+ end
314
+
301
315
  private
302
316
 
303
317
  # Used to interpret arguments to add and remove
@@ -585,6 +599,51 @@ module InterMine::Lists
585
599
  return ret
586
600
  end
587
601
 
602
+ # Add tags to a list.
603
+ #
604
+ # Returns the current tags
605
+ #
606
+ def add_tags(list, *tags)
607
+ uri = URI.parse(@service.root + Service::LIST_TAG_PATH)
608
+ params = @service.params.merge("name" => list.name, "tags" => tags.join(";"))
609
+ res = Net::HTTP.post_form(uri, params)
610
+ check_response_for_error(res)
611
+ return JSON.parse(res.body)["tags"]
612
+ end
613
+
614
+ # Remove tags from a list
615
+ #
616
+ # Returns the current tags
617
+ #
618
+ def remove_tags(list, *tags)
619
+ uri = URI.parse(@service.root + Service::LIST_TAG_PATH)
620
+ params = @service.params.merge(
621
+ "name" => list.name,
622
+ "tags" => tags.join(";")
623
+ )
624
+ req_path = uri.path + "?" + params_to_query_string(params)
625
+ req = Net::HTTP::Delete.new(req_path)
626
+ res = Net::HTTP.start(uri.host, uri.port) do |http|
627
+ http.request(req)
628
+ end
629
+ check_response_for_error(res)
630
+ return JSON.parse(res.body)["tags"]
631
+ end
632
+
633
+ # Get the current tags for a list
634
+ def tags_for(list)
635
+ uri = URI.parse(@service.root + Service::LIST_TAG_PATH)
636
+ params = @service.params.merge(
637
+ "name" => list.name
638
+ )
639
+ req_path = uri.path + "?" + params_to_query_string(params)
640
+ res = Net::HTTP.start(uri.host, uri.port) {|http|
641
+ http.get(req_path)
642
+ }
643
+ check_response_for_error(res)
644
+ return JSON.parse(res.body)["tags"]
645
+ end
646
+
588
647
  # only handles single value keys!
589
648
  def params_to_query_string(p)
590
649
  return @service.params.merge(p).map { |k,v| "#{k}=#{CGI::escape(v.to_s)}" }.join('&')
@@ -509,10 +509,10 @@ module Metadata
509
509
 
510
510
  klass = Module.new
511
511
  fd_names = @fields.values.map { |x| x.name }
512
+ attr_names = @fields.values.select { |x| x.is_a?(AttributeDescriptor)}.map {|x| x.name}
512
513
  klass.class_eval do
513
514
  include *supers
514
- attr_reader *fd_names
515
-
515
+ attr_reader *attr_names
516
516
  end
517
517
 
518
518
  @fields.values.each do |fd|
@@ -535,6 +535,20 @@ module Metadata
535
535
  end
536
536
  end
537
537
  end
538
+
539
+ if fd.is_a?(ReferenceDescriptor)
540
+ klass.class_eval do
541
+ define_method(fd.name) do
542
+ if instance_variable_get("@" + fd.name).nil?
543
+ q = __cd__.select(fd.name + ".*").where(:id => objectId)
544
+ instance_var = q.results.first[fd.name]
545
+ instance_variable_set("@" + fd.name, instance_var)
546
+ end
547
+ return instance_variable_get("@" + fd.name)
548
+ end
549
+ end
550
+ end
551
+
538
552
  klass.class_eval do
539
553
  define_method(fd.name + "=") do |val|
540
554
  if fd.is_a?(AttributeDescriptor)
@@ -108,6 +108,7 @@ module InterMine
108
108
  LIST_DIFFERENCE_PATH = "/lists/diff/json"
109
109
  LIST_INTERSECTION_PATH = "/lists/intersect/json"
110
110
  LIST_SUBTRACTION_PATH = "/lists/subtract/json"
111
+ LIST_TAG_PATH = "/list/tags/json"
111
112
 
112
113
  # The webservice version. An integer that
113
114
  # supplies information about what features are supported.
@@ -122,11 +123,13 @@ module InterMine
122
123
 
123
124
  # A collection of the names of any templates that this service was not able to parse,
124
125
  # and you will thus not be able to access.
125
- :broken_templates
126
+ attr_reader :broken_templates
126
127
 
127
128
  def_delegators :@list_manager,
128
- :lists, :list, :list_names, :create_list, :delete_lists, :get_lists_with_tags,
129
- :union_of, :intersection_of, :symmetric_difference_of, :subtract
129
+ :lists, :list, :list_names, :create_list, :delete_lists,
130
+ :get_lists_with_tags,
131
+ :union_of, :intersection_of, :symmetric_difference_of,
132
+ :subtract
130
133
 
131
134
  # Construct a new service.
132
135
  #
@@ -3,8 +3,14 @@ module Intermine
3
3
  # Webservice Client Version number
4
4
  #
5
5
  # Changes:
6
+ # 0.98.11 - * InterMineObjects now lazily fetch their references
7
+ # if nil. Note that while extremely convenient,
8
+ # explicit prefetching will be much more efficient
9
+ # (how much depends on the number of extra requests, but
10
+ # a factor of 10 seems pretty common).
11
+ # * Added support for list tagging
6
12
  # 0.98.10 - Added status property to lists
7
13
  # 0.98.09 - Major changes to results - now with thorough-going Enumerable support
8
14
  # 0.98.08 - Added column summary support
9
- VERSION = "0.98.10"
15
+ VERSION = "0.98.11"
10
16
  end
data/test/live_test.rb CHANGED
@@ -7,6 +7,17 @@ class LiveDemoTest < Test::Unit::TestCase
7
7
 
8
8
  def setup
9
9
  @service = Service.new("http://localhost/intermine-test", "Z1a3D3U16cicCdS0T6y4bdN1SQh")
10
+ @temp_lists = []
11
+ end
12
+
13
+
14
+ def teardown
15
+ @temp_lists.each do |l|
16
+ begin
17
+ @service.delete_lists(l)
18
+ rescue
19
+ end
20
+ end
10
21
  end
11
22
 
12
23
  def testVersion
@@ -34,4 +45,89 @@ class LiveDemoTest < Test::Unit::TestCase
34
45
  assert_match("CURRENT", list.status)
35
46
  end
36
47
 
48
+ def testListEnumerability
49
+ list = @service.list("My-Favourite-Employees")
50
+ names = list.map {|emp| emp.name }
51
+ exp = ["Bernd Stromberg", "David Brent", "Neil Godwin", "Timo Becker"]
52
+ assert_equal(exp, names)
53
+
54
+ old = list.select {|emp| emp.age > 55}
55
+ assert_equal(1, old.size)
56
+ assert_equal("David Brent", old.first.name)
57
+
58
+ sum = list.reduce(0) {|m, i| m + i.age}
59
+ assert_equal(185, sum)
60
+ end
61
+
62
+ def testLazyReferenceFetching
63
+ list = @service.list("My-Favourite-Employees")
64
+
65
+ deps = list.map {|emp| emp.department.name }
66
+ exp = ["Schadensregulierung M-Z", "Sales",
67
+ "Human Resources", "Schadensregulierung"]
68
+ assert_equal(exp, deps)
69
+
70
+ comps = list.map {|emp| emp.department.company.name }
71
+ exp = ["Capitol Versicherung AG", "Wernham-Hogg",
72
+ "Wernham-Hogg", "Capitol Versicherung AG"]
73
+ assert_equal(exp, comps)
74
+ end
75
+
76
+ def testLazyCollectionFetching
77
+ list = @service.list("My-Favourite-Employees")
78
+ emps = list.map {|manager| manager.department.employees.map {|employee| employee.age} }
79
+ exp = [[42, 28, 29, 46, 28, 48],
80
+ [62, 64, 58, 35, 36, 46],
81
+ [41, 57, 36],
82
+ [63, 37, 61, 55, 58, 45]]
83
+ assert_equal(exp, emps)
84
+
85
+ sum = list.reduce(0) {|m, manager| m + manager.department.employees.reduce(0) {|n, emp| n + emp.age}}
86
+ assert_equal(975, sum)
87
+ end
88
+
89
+ def testListCreation
90
+ ids = %{Alex Brenda Carol David Edgar}
91
+ new_list = @service.create_list(ids, "Employee")
92
+ @temp_lists << new_list
93
+ assert_equal(3, new_list.size)
94
+ end
95
+
96
+ def testListTagging
97
+ ids = %{Alex Brenda Carol David Edgar}
98
+ new_list = @service.create_list(ids, "Employee")
99
+ @temp_lists << new_list
100
+ assert_equal([], new_list.tags)
101
+ new_list.add_tags("a-tag", "another-tag")
102
+ assert_equal(2, new_list.tags.size)
103
+ assert_equal(["a-tag", "another-tag"].sort, new_list.tags.sort)
104
+ end
105
+
106
+ def testListTagRemoval
107
+ ids = %{Alex Brenda Carol David Edgar}
108
+ tags = ["a-tag", "another-tag"]
109
+ new_list = @service.create_list(ids, "Employee", tags)
110
+ @temp_lists << new_list
111
+ assert_equal(2, new_list.tags.size)
112
+ new_list.remove_tags("another-tag")
113
+ assert_equal(["a-tag"], new_list.tags)
114
+ new_list.remove_tags("a-tag")
115
+ assert_equal([], new_list.tags)
116
+ new_list.remove_tags("a-non-existent-tag")
117
+ assert_equal([], new_list.tags)
118
+ end
119
+
120
+ def testListTagUpdating
121
+ ids = %{Alex Brenda Carol David Edgar}
122
+ tags = ["a-tag", "another-tag"]
123
+ new_list = @service.create_list(ids, "Employee")
124
+ @temp_lists << new_list
125
+ assert_equal([], new_list.tags)
126
+ manager = @service.instance_variable_get("@list_manager")
127
+ assert_equal(2, manager.add_tags(new_list, tags).size)
128
+ assert_equal([], new_list.tags)
129
+ new_list.update_tags
130
+ assert_equal(tags.sort, new_list.tags.sort)
131
+ end
132
+
37
133
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: intermine
3
3
  version: !ruby/object:Gem::Version
4
- hash: 387
4
+ hash: 385
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 98
9
- - 10
10
- version: 0.98.10
9
+ - 11
10
+ version: 0.98.11
11
11
  platform: ruby
12
12
  authors:
13
13
  - Alex Kalderimis
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-10-28 00:00:00 +01:00
18
+ date: 2011-11-01 00:00:00 +00:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency