mab2 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d1659413e48d57b2b853237962d2763d20bf6288
4
- data.tar.gz: cd5bbecc8442e0769c27539e40283b41e361dc96
3
+ metadata.gz: 1ddffd930eea7cf17107898031a771fc6dad90be
4
+ data.tar.gz: b05ecb5a0ac27dfd41c2b6abec9d198a24db5031
5
5
  SHA512:
6
- metadata.gz: bfde3b1e18c169f751db9faf12fa988c614ef63dcd76600ea9b143a1e9ecc95deebbce608579843b80dbdb3c517d1abfd7f242d7468294a2ea24108bcd684d37
7
- data.tar.gz: 54e634c0052d6594bb3cc6e8920d2452baf901f5250bc8de34d0611088a222fb36ca83c63fbe060946d828c640a60b79ac42ba5d16111caf1413c304bdf1671d
6
+ metadata.gz: fa6d7aac578e249cd39daf51aba91ccacd0430e9950326521637d659703eac0fc0c9733fe65cbe557db5f8b8b8959c792ca216cd1c7cd7bf3469e4fdc3af9dbf
7
+ data.tar.gz: 4037cb77b8a966576f3e8e7cb0b78fb7393a2410d50b67956392848cb6f03e594e315c52ea640b17754fef00f8bd3c858f42a0d5303a9a2e9c46987b9747b3f2
data/README.md CHANGED
@@ -20,6 +20,19 @@ Or install it yourself as:
20
20
 
21
21
  TODO: Write usage instructions here
22
22
 
23
+ ## Notes
24
+
25
+ The code is slightly optimized for performance.
26
+
27
+ ### Bang operators
28
+ Bang (!) operators are used where possible (http://www.skorks.com/2010/04/merging-ruby-hashes-and-bang-method-usage/), because they are faster than their non-bang counterparts.
29
+
30
+ ### Circumvent attribute accessors internally
31
+ In order to minimize function calls, instance variables are accessed directly where possible, not through (own) accessors.
32
+
33
+ ### Shortcuts
34
+ There are attemps to shortcut execution as soon as possible, e.g. by checking certain 'show stoppers' right in front of a function.
35
+
23
36
  ## Contributing
24
37
 
25
38
  1. Fork it
@@ -1,89 +1,56 @@
1
1
  class Mab2::Document::Scope
2
2
 
3
- attr_accessor :scope_subfields
4
-
5
3
  def initialize(document)
6
4
  @document = document
7
5
  end
8
6
 
9
7
  def controlfield(tag)
10
- @document.controlfields[tag.to_sym].try(:chars) || []
11
- end
12
-
13
- def datafield(tag, indicators)
14
- @scope_datafields = []
15
- indicators = sanitize_indicators(indicators)
16
-
17
- selected_datafields = if !(datafields_by_tag = @document.datafields[tag.to_sym]).nil?
18
- datafields_by_tag.select do |indicator_one_key, indicator_one_value|
19
- indicators[:ind1].nil? || (indicators[:ind1].any? { |ind1| ind1.length > 1 && ind1.to_s.starts_with?('-') } ^ indicators[:ind1].include?(indicator_one_key)) && indicator_one_value.any? do |indicator_two_key, indicator_two_value|
20
- indicators[:ind2].nil? || indicators[:ind2].include?(indicator_two_key)
21
- end
22
- end
23
- end
24
-
25
- if !selected_datafields.nil?
26
- selected_datafields.each_pair do |indicator_one_key, indicator_one_value|
27
- indicator_one_value.each_pair do |indicator_two_key, indicator_two_value|
28
- indicator_two_value.each do |subfields|
29
- @scope_datafields.push Mab2::Document::Datafield.new(tag, indicators, subfields)
30
- end
31
- end
32
- end
33
- end
34
-
35
- self
8
+ @document.controlfields_by(tag: tag)
36
9
  end
37
10
 
38
- def get
11
+ def datafield(tag, indicators = {})
12
+ @scope_datafields = @document.datafields_by(tag: tag, indicators: indicators)
39
13
  self
40
14
  end
41
15
 
42
- def fields
43
- @scope_datafields
44
- end
45
-
46
- def scope_subfields
47
- @scope_subfields || @scope_datafields.map { |datafield| datafield.subfields }.flatten.presence
48
- end
49
-
50
16
  def subfield(subfield_codes)
51
- # make subfield_codes an array (always)
52
- subfield_codes = subfield_codes.is_a?(Array) ? subfield_codes : [subfield_codes]
17
+ subfield_codes = subfield_codes.is_a?(Array) ? subfield_codes : [subfield_codes] # subfield_codes should be an array
53
18
 
54
- @scope_subfields = []
55
-
56
- @scope_datafields.each do |datafield|
57
- @scope_subfields.push datafield.subfields.select { |subfield| subfield_codes.include? subfield.name.to_s }
58
- end
59
-
60
- @scope_subfields.flatten!
19
+ @scope_subfields = @scope_datafields.map do |datafield|
20
+ datafield.subfields.select { |subfield| subfield_codes.include? subfield.name.to_s }
21
+ end.flatten! || []
61
22
 
62
23
  self
63
24
  end
64
25
 
65
- def subfields
66
- scope_subfields
67
- end
68
-
69
26
  def value(options = {})
70
- if !(_values = values(options)).nil?
71
- _values.first
72
- end
27
+ values.first
73
28
  end
74
29
 
75
30
  def values(options = {})
76
- if !scope_subfields.nil?
77
- subfield_values = scope_subfields.map { |subfield| subfield.value }
78
- @scope_values = (join_string = options[:join_subfields]) ? subfield_values.join(join_string == true ? '' : join_string) : subfield_values
31
+ unless @scope_values
32
+ # if no subfields where specified so far, take all (e.g. doc.datafield('100').values))
33
+ @scope_subfields ||= (@scope_datafields.map { |datafield| datafield.subfields }).flatten! || []
34
+ subfield_values = @scope_subfields.map { |subfield| subfield.value }
35
+ @scope_values = options[:join_subfields] ? subfield_values.join(options[:join_subfields]) : subfield_values
36
+ else
37
+ @scope_values
79
38
  end
80
39
  end
81
40
 
82
- private
41
+ #
42
+ # compatibility
43
+ #
44
+ def get
45
+ self
46
+ end
83
47
 
84
- # ensure a hash with array values
85
- def sanitize_indicators(indicators)
86
- indicators.inject({}) { |hash, (k, v)| hash[k] = (v.is_a?(Array) ? v.map(&:to_sym) : [v.to_sym]); hash }
48
+ def fields
49
+ @scope_datafields
50
+ end
51
+
52
+ def subfields
53
+ @scope_subfields
87
54
  end
88
55
 
89
56
  end
data/lib/mab2/document.rb CHANGED
@@ -24,13 +24,52 @@ class Mab2::Document
24
24
  @datafields = mab.datafields
25
25
  end
26
26
 
27
+ #
28
+ # scope functions
29
+ #
27
30
  def controlfield(tag)
31
+ tag = tag.to_sym # tag should be a symbol
28
32
  Mab2::Document::Scope.new(self).controlfield(tag)
29
33
  end
30
34
 
31
35
  # mabmapper uses field instead of datafield
32
- def field(tag, options = {})
33
- Mab2::Document::Scope.new(self).datafield(tag, options)
36
+ def field(tag, indicators = {})
37
+ tag = tag.to_sym # tag should be a symbol
38
+ indicators.each_pair { |key, val| indicators[key] = val.is_a?(Array) ? val.map(&:to_sym) : [val.to_sym] } # indicators should be hash of symbol arrays
39
+ Mab2::Document::Scope.new(self).datafield(tag, indicators)
40
+ end
41
+
42
+ #
43
+ # query functions
44
+ #
45
+ def controlfields_by(options = {})
46
+ (result = @controlfields[options[:tag]]).nil? ? [] : result.chars
47
+ end
48
+
49
+ def datafields_by(options = {})
50
+ indicators = options[:indicators]
51
+ tag = options[:tag]
52
+ query_result = []
53
+
54
+ return [] if @datafields[tag].nil? # shortcut for 'no datafield with that tag exists'
55
+
56
+ matching_datafields = @datafields[tag].select do |indicator_one_key, indicator_one_value|
57
+ indicators[:ind1].nil? || (indicators[:ind1].any? { |ind1| ind1.length > 1 && ind1.to_s.starts_with?('-') } ^ indicators[:ind1].include?(indicator_one_key)) && indicator_one_value.any? do |indicator_two_key, indicator_two_value|
58
+ indicators[:ind2].nil? || indicators[:ind2].include?(indicator_two_key)
59
+ end
60
+ end
61
+
62
+ if !matching_datafields.nil?
63
+ matching_datafields.each_pair do |indicator_one_key, indicator_one_value|
64
+ indicator_one_value.each_pair do |indicator_two_key, indicator_two_value|
65
+ indicator_two_value.each do |subfields|
66
+ query_result.push Mab2::Document::Datafield.new(tag, indicators, subfields)
67
+ end
68
+ end
69
+ end
70
+ end
71
+
72
+ return query_result
34
73
  end
35
74
 
36
75
  end
data/lib/mab2/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Mab2
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mab2
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Sievers
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-11-19 00:00:00.000000000 Z
11
+ date: 2013-11-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri