rbbt-util 5.26.171 → 5.27.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b30207a19261c11a2613c091fde28604cc9186a6fafec8f74b33ecbb4e9cbac9
4
- data.tar.gz: 3822e3009d4955690eff0e5554756cf6b1e898e1e9235814630f37a7e3192cfd
3
+ metadata.gz: ce00e5bba751f062a2cf055c1b0e5d937936a452c6edb6eed6da174aadeaa111
4
+ data.tar.gz: a54bc18f2cc45f0879151f44904ab312cbba26eddeed5a68f592ccd667258d0a
5
5
  SHA512:
6
- metadata.gz: 187d8e5dd75a5bfc750e93e6f8d41b4bf813ab800bb33b6aefd07695fc5d3d7531b904cb049b72f56cd06089d651d090152a8e7adfe3e0642182bf229d3b5ee9
7
- data.tar.gz: 383c16854ec7772f32ced9fc0a57708b626b3db59cce5b41cee7ae909418f96f0058eba3e488db167a68d760bc8b85f252998f7aa9fb577ba9aec45d461c5d8f
6
+ metadata.gz: 86fd735a87478a1c37ea38eb075d76093db55178f5c6aae7d97a3b7fe70e68df82774ec830f7e6f55e1370df60df6cfe6312a42220af389876263669eb966aa4
7
+ data.tar.gz: 9facd461d8a5d6f8f549310df3257be39d9cc939abf2a36dcb2eb45e7c48c65d3aae09e269efc50fb67fc4e574dc9c801b9111986ae3b9483c53c3015631e463
@@ -120,7 +120,7 @@ module Annotated
120
120
  if object.instance_variables.include?(:@annotation_values)
121
121
  hash = {}
122
122
  object.instance_variable_get(:@annotation_values).each{|k,v| hash[k] = v}
123
- self.annotation_values.each{|k,v| hash[k] = v}
123
+ self.annotation_values.each{|k,v| hash[k] = v unless v.nil?}
124
124
 
125
125
  object.instance_variable_set(:@annotation_values, hash)
126
126
  object.instance_variable_set(:@shared_annotations, false)
@@ -289,6 +289,7 @@ module Annotation
289
289
  object.instance_variable_set(:@annotation_types, nil)
290
290
 
291
291
  if Hash === (hash = values.last)
292
+ setup_positional(object, *values[0..-2]) if values.length > 1
292
293
  clean_and_setup_hash(object, hash)
293
294
  else
294
295
  setup_positional(object, *values)
@@ -115,19 +115,21 @@ module Annotated
115
115
 
116
116
  fields = case
117
117
 
118
- when ((fields.compact.empty?) and not annotations.empty?)
118
+ when ((fields.compact.empty?) && ! annotations.empty?)
119
119
  fields = AnnotatedArray === annotations ? annotations.annotations : annotations.compact.first.annotations
120
120
  fields << :annotation_types
121
121
 
122
122
  when (fields == [:literal] and not annotations.compact.empty?)
123
123
  fields << :literal
124
124
 
125
- when (fields == [:all] and Annotated === annotations)
126
- fields = [:annotation_types] + annotations.annotations
125
+ when (fields == [:all] && Annotated === annotations)
126
+ annotation_names = annotations.annotations
127
+ annotation_names += annotations.first.annotations if Annotated === annotations.first
128
+ fields = [:annotation_types] + annotation_names.uniq
127
129
  fields << :annotated_array if AnnotatedArray === annotations
128
130
  fields << :literal
129
131
 
130
- when (fields == [:all] and not annotations.compact.empty?)
132
+ when (fields == [:all] && ! annotations.compact.empty?)
131
133
  raise "Input array must be annotated or its elements must be" if not Annotated === annotations.compact.first and not Array === annotations.compact.first
132
134
  raise "Input array must be annotated or its elements must be. No double arrays of singly annotated entities." if not Annotated === annotations.compact.first and Array === annotations.compact.first
133
135
  fields = [:annotation_types] + (Annotated === annotations ?
@@ -157,12 +159,11 @@ module Annotated
157
159
  tsv = TSV.setup({}, :key_field => "ID", :fields => fields, :type => :list, :unnamed => true)
158
160
 
159
161
  annotations.compact.each_with_index do |annotation,i|
160
- tsv[annotation.id + ":" << i.to_s] = annotation.tsv_values(*fields).dup
162
+ tsv[annotation.id + "#" << i.to_s] = annotation.tsv_values(*fields).dup
161
163
  end
162
164
 
163
165
  else
164
166
  raise "Annotations need to be an Array to create TSV"
165
-
166
167
  end
167
168
 
168
169
  tsv
data/lib/rbbt/entity.rb CHANGED
@@ -5,6 +5,8 @@ module Entity
5
5
 
6
6
  UNPERSISTED_PREFIX = "entity_unpersisted_property_"
7
7
 
8
+ class MultipleEntity < Exception; end
9
+
8
10
  class DontPersist < Exception
9
11
  attr_accessor :payload
10
12
  def self.initialize(payload)
@@ -170,7 +172,7 @@ module Entity
170
172
  when :array, :array2single
171
173
  ary_name = "_ary_" << name
172
174
  define_method ary_name, &block
173
-
175
+
174
176
  define_method name do |*args|
175
177
  case
176
178
  when Array === self
@@ -188,6 +190,53 @@ module Entity
188
190
  Hash === res ? res[self] : res[0]
189
191
  end
190
192
  end
193
+ when :multiple
194
+ multi_name = "_multiple_" << name
195
+
196
+ define_method multi_name do
197
+ if self.instance_variable_get("@multiple_result")
198
+ return self.instance_variable_get("@multiple_result")
199
+ end
200
+ raise MultipleEntity, "Entity #{name} runs with multiple entities: #{self}"
201
+ end
202
+
203
+ define_method name do |*args|
204
+ obj = if Array === self
205
+ self
206
+ elsif self.respond_to?(:container) && Array === self.container
207
+ self.container
208
+ else
209
+ self.make_list
210
+ end
211
+
212
+ missing = []
213
+ obj.each do |e|
214
+ begin
215
+ e.send(multi_name)
216
+ rescue MultipleEntity
217
+ missing << e
218
+ end
219
+ end
220
+
221
+ res = missing.any? ? block.call(missing) : nil
222
+ case res
223
+ when Array
224
+ missing.zip(res).each do |o,res|
225
+ o.instance_variable_set("@multiple_result", res)
226
+ end
227
+ when Hash
228
+ res.each do |o,res|
229
+ o.instance_variable_set("@multiple_result", res)
230
+ end
231
+ end
232
+
233
+ if Array === self
234
+ self.collect{|o| o.send(multi_name)}
235
+ else
236
+ self.send(multi_name)
237
+ end
238
+ end
239
+
191
240
  else
192
241
  raise "Type not undestood in property: #{ type }"
193
242
  end
@@ -200,12 +249,16 @@ module Entity
200
249
  options ||= {}
201
250
  options = Misc.add_defaults options, :dir => Entity.entity_property_cache
202
251
 
252
+ orig_method_name = method_name
253
+ multi_name = "_multiple_" + method_name.to_s
254
+ method_name = multi_name if self.instance_methods.include?(multi_name.to_sym)
255
+
203
256
  orig_name = UNPERSISTED_PREFIX + method_name.to_s
204
257
  alias_method orig_name, method_name unless self.instance_methods.include? orig_name.to_sym
205
258
 
206
259
  define_method method_name do |*args|
207
260
  id = self.id
208
- persist_name = __method__.to_s << ":" << (Array === id ? Misc.obj2digest(id) : id)
261
+ persist_name = orig_method_name.to_s << ":" << (Array === id ? Misc.obj2digest(id) : id)
209
262
 
210
263
  persist_options = options
211
264
  persist_options = persist_options.merge(:other => {:args => args}) if args and args.any?
data/lib/rbbt/persist.rb CHANGED
@@ -402,6 +402,7 @@ module Persist
402
402
  subkey = name + ":"
403
403
 
404
404
  if String === repo
405
+ repo = repo.find if Path === repo
405
406
  repo = Persist.open_tokyocabinet(repo, false, :list, "BDB")
406
407
  repo.read_and_close do
407
408
  keys = repo.range subkey + 0.chr, true, subkey + 254.chr, true
@@ -14,6 +14,7 @@ Use - to read from STDIN
14
14
  -tch--tokyocabinet File is a tokyocabinet hash database
15
15
  -tcb--tokyocabinet_bd File is a tokyocabinet B database
16
16
  -f--fields* Fields to extract
17
+ -s--sep* Separation character
17
18
  -hh--header_hash* Change the character used to mark the header line (defaults to #)
18
19
  -k--key_field* Use this field as key
19
20
  -h--help Print this help
@@ -46,7 +47,7 @@ fields = options[:fields]
46
47
  key_field = options[:key_field]
47
48
  fields = fields.split(/[,|]/, -1) unless fields.nil?
48
49
 
49
- parser = TSV::Parser.new tsv, :key_field => key_field, :fields => fields, :type => options[:type], :header_hash => options[:header_hash]
50
+ parser = TSV::Parser.new tsv, :key_field => key_field, :fields => fields, :type => options[:type], :header_hash => options[:header_hash], :sep => options[:sep]
50
51
  fields ||= parser.fields
51
52
 
52
53
  TSV.traverse(parser) do |k,v|
@@ -13,6 +13,7 @@ Use - to read from STDIN
13
13
  -k--key_field* Key field
14
14
  -f--fields* Fields
15
15
  -t--type* Type
16
+ -s--sep* Separation character
16
17
  -m--merge Merge from multiple rows
17
18
  -h--help Print this help
18
19
  -tch--tokyocabinet File is a tokyocabinet hash database
@@ -1,7 +1,7 @@
1
1
  require File.join(File.expand_path(File.dirname(__FILE__)), '../..', 'test_helper.rb')
2
2
  require 'rbbt/annotations'
3
3
 
4
- class TestClass < Test::Unit::TestCase
4
+ class TestAnnotation < Test::Unit::TestCase
5
5
  def test_marshal
6
6
  a = "STRING"
7
7
  a.extend Annotated
@@ -77,11 +77,11 @@ class TestAnnotations < Test::Unit::TestCase
77
77
  AnnotatedString.setup(str1, annotation_str1)
78
78
  AnnotatedString.setup(str2, annotation_str2)
79
79
 
80
- assert_equal str1, Annotated.tsv([str1, str2], :all).tap{|t| t.unnamed = false}[str1.id + ":0"]["literal"]
81
- assert_equal annotation_str1, Annotated.tsv([str1, str2], :all).tap{|t| t.unnamed = false}[str1.id + ":0"]["annotation_str"]
82
- assert_equal str1, Annotated.tsv([str1, str2], :all).tap{|t| t.unnamed = false}[str1.id + ":0"]["literal"]
83
- assert_equal annotation_str1, Annotated.tsv([str1, str2], :all).tap{|t| t.unnamed = false}[str1.id + ":0"]["annotation_str"]
84
- assert_equal annotation_str1, Annotated.tsv([str1, str2], :annotation_str, :JSON).tap{|t| t.unnamed = false}[str1.id + ":0"]["annotation_str"]
80
+ assert_equal str1, Annotated.tsv([str1, str2], :all).tap{|t| t.unnamed = false}[str1.id + "#0"]["literal"]
81
+ assert_equal annotation_str1, Annotated.tsv([str1, str2], :all).tap{|t| t.unnamed = false}[str1.id + "#0"]["annotation_str"]
82
+ assert_equal str1, Annotated.tsv([str1, str2], :all).tap{|t| t.unnamed = false}[str1.id + "#0"]["literal"]
83
+ assert_equal annotation_str1, Annotated.tsv([str1, str2], :all).tap{|t| t.unnamed = false}[str1.id + "#0"]["annotation_str"]
84
+ assert_equal annotation_str1, Annotated.tsv([str1, str2], :annotation_str, :JSON).tap{|t| t.unnamed = false}[str1.id + "#0"]["annotation_str"]
85
85
  end
86
86
 
87
87
  def test_literal
@@ -59,6 +59,19 @@ module ReversableString
59
59
  persist :reverse_text_ary_p_array, :array, :dir => TmpFile.tmp_file
60
60
 
61
61
  persist :annotation_list, :annotations, :dir => TmpFile.tmp_file
62
+
63
+
64
+ $processed_multiple = []
65
+ property :multiple_annotation_list => :multiple do |list|
66
+ $processed_multiple.concat list
67
+ res = {}
68
+ list.collect do |e|
69
+ e.chars.to_a.collect{|c|
70
+ ReversableString.setup(c)
71
+ }
72
+ end
73
+ end
74
+ persist :multiple_annotation_list, :annotations, :dir => TmpFile.tmp_file
62
75
  end
63
76
 
64
77
  class TestEntity < Test::Unit::TestCase
@@ -174,6 +187,42 @@ class TestEntity < Test::Unit::TestCase
174
187
  assert_equal string.length, string.annotation_list.length
175
188
  end
176
189
 
190
+ def test_persist_multiple_annotations
191
+ string1 = 'aaabbbccc'
192
+ string2 = 'AAABBBCCC'
193
+ string3 = 'AAABBBCCC_3'
194
+ string4 = 'AAABBBCCC_4'
195
+
196
+ array = ReversableString.setup([string1, string2])
197
+ assert_equal [string1, string2].collect{|s| s.chars}, array.multiple_annotation_list
198
+
199
+ assert_equal string1.length, array[0].multiple_annotation_list.length
200
+ assert_equal $processed_multiple, [string1, string2]
201
+
202
+ array = ReversableString.setup([string2, string3])
203
+ assert_equal string2.length, array[0].multiple_annotation_list.length
204
+ assert_equal $processed_multiple, [string1, string2, string3]
205
+
206
+ array = ReversableString.setup([string2, string3])
207
+ assert_equal string2.length, array[0].multiple_annotation_list.length
208
+ assert_equal $processed_multiple, [string1, string2, string3]
209
+
210
+ $processed_multiple = []
211
+ array = ReversableString.setup([string2, string3, string4])
212
+ assert_equal string2.length, array[0].multiple_annotation_list.length
213
+ assert_equal $processed_multiple, [string4]
214
+
215
+ string1 = 'aaabbbccc'
216
+ string2 = 'AAABBBCCC'
217
+ string3 = 'AAABBBCCC_3'
218
+ string4 = 'AAABBBCCC_4'
219
+ $processed_multiple = []
220
+ array = ReversableString.setup([string2, string3, string4])
221
+ assert_equal string2.length, array[0].multiple_annotation_list.length
222
+ assert_equal $processed_multiple, [string4]
223
+
224
+ end
225
+
177
226
  def test_clean_annotations
178
227
 
179
228
  string = "test_string"
@@ -184,7 +233,7 @@ class TestEntity < Test::Unit::TestCase
184
233
  end
185
234
 
186
235
  def test_all_properties
187
- puts ReversableString.setup("TEST").all_properties
188
- puts ReversableString.all_properties
236
+ assert ReversableString.setup("TEST").all_properties.include?("reverse_text_ary")
237
+ assert_equal ReversableString.setup("TEST").all_properties, ReversableString.all_properties
189
238
  end
190
239
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rbbt-util
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.26.171
4
+ version: 5.27.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Miguel Vazquez
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-04-16 00:00:00.000000000 Z
11
+ date: 2020-04-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake