ddr-models 2.11.0 → 3.0.0.alpha.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (268) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +0 -1
  3. data/.travis.yml +7 -6
  4. data/Gemfile +2 -3
  5. data/Rakefile +5 -5
  6. data/app/controllers/users/sessions_controller.rb +5 -0
  7. data/app/helpers/models_helper.rb +7 -0
  8. data/app/models/attachment.rb +3 -5
  9. data/app/models/collection.rb +12 -79
  10. data/app/models/component.rb +8 -67
  11. data/app/models/item.rb +8 -73
  12. data/app/models/target.rb +6 -5
  13. data/config/initializers/active_fedora_base.rb +11 -30
  14. data/config/initializers/{active_fedora_datastream.rb → active_fedora_file.rb} +1 -1
  15. data/config/initializers/devise.rb +2 -3
  16. data/config/initializers/subscriptions.rb +2 -19
  17. data/ddr-models.gemspec +10 -14
  18. data/lib/ddr/actions/fixity_check.rb +1 -1
  19. data/lib/ddr/actions/virus_check.rb +17 -10
  20. data/lib/ddr/auth.rb +6 -38
  21. data/lib/ddr/auth/ability.rb +1 -5
  22. data/lib/ddr/auth/ability_definitions/collection_ability_definitions.rb +0 -16
  23. data/lib/ddr/auth/ability_definitions/datastream_ability_definitions.rb +10 -4
  24. data/lib/ddr/auth/ability_definitions/role_based_ability_definitions.rb +16 -9
  25. data/lib/ddr/auth/ability_factory.rb +25 -2
  26. data/lib/ddr/auth/abstract_ability.rb +1 -2
  27. data/lib/ddr/auth/auth_context.rb +0 -18
  28. data/lib/ddr/auth/auth_context_factory.rb +2 -0
  29. data/lib/ddr/auth/group.rb +0 -6
  30. data/lib/ddr/auth/grouper_gateway.rb +0 -24
  31. data/lib/ddr/auth/permissions.rb +1 -12
  32. data/lib/ddr/auth/role_based_access_controls_enforcement.rb +5 -5
  33. data/lib/ddr/auth/roles.rb +1 -11
  34. data/lib/ddr/auth/roles/role_set.rb +0 -7
  35. data/lib/ddr/auth/roles/roles_datastream.rb +9 -0
  36. data/lib/ddr/contacts.rb +25 -0
  37. data/lib/ddr/datastreams.rb +10 -28
  38. data/lib/ddr/datastreams/datastream_behavior.rb +30 -100
  39. data/lib/ddr/datastreams/descriptive_metadata_datastream.rb +28 -31
  40. data/lib/ddr/datastreams/fits_datastream.rb +9 -22
  41. data/lib/ddr/datastreams/metadata_datastream.rb +17 -31
  42. data/lib/ddr/derivatives.rb +13 -16
  43. data/lib/ddr/derivatives/derivative.rb +68 -0
  44. data/lib/ddr/derivatives/{generator.rb → generators/generator.rb} +11 -7
  45. data/lib/ddr/derivatives/generators/png_generator.rb +23 -0
  46. data/lib/ddr/derivatives/generators/ptif_generator.rb +56 -0
  47. data/lib/ddr/derivatives/multires_image.rb +59 -0
  48. data/lib/ddr/derivatives/thumbnail.rb +36 -0
  49. data/lib/ddr/events.rb +0 -1
  50. data/lib/ddr/events/deletion_event.rb +9 -5
  51. data/lib/ddr/events/event.rb +8 -13
  52. data/lib/ddr/events/fixity_check_event.rb +0 -6
  53. data/lib/ddr/events/ingestion_event.rb +9 -5
  54. data/lib/ddr/events/update_event.rb +4 -17
  55. data/lib/ddr/index.rb +4 -4
  56. data/lib/ddr/index/abstract_query_result.rb +4 -3
  57. data/lib/ddr/index/connection.rb +9 -28
  58. data/lib/ddr/index/csv_query_result.rb +32 -47
  59. data/lib/ddr/index/field.rb +1 -11
  60. data/lib/ddr/index/fields.rb +16 -99
  61. data/lib/ddr/index/filter.rb +30 -103
  62. data/lib/ddr/index/filters.rb +19 -0
  63. data/lib/ddr/index/query.rb +11 -58
  64. data/lib/ddr/index/query_builder.rb +36 -147
  65. data/lib/ddr/index/query_clause.rb +19 -79
  66. data/lib/ddr/index/query_result.rb +4 -36
  67. data/lib/ddr/index/query_value.rb +18 -0
  68. data/lib/ddr/index/response.rb +2 -19
  69. data/lib/ddr/jobs.rb +12 -0
  70. data/lib/ddr/jobs/fits_file_characterization.rb +51 -0
  71. data/lib/ddr/jobs/permanent_id.rb +23 -0
  72. data/lib/ddr/managers.rb +1 -0
  73. data/lib/ddr/managers/derivatives_manager.rb +53 -93
  74. data/lib/ddr/managers/permanent_id_manager.rb +91 -0
  75. data/lib/ddr/managers/technical_metadata_manager.rb +10 -24
  76. data/lib/ddr/managers/workflow_manager.rb +10 -29
  77. data/lib/ddr/models.rb +50 -101
  78. data/lib/ddr/models/access_controllable.rb +12 -0
  79. data/lib/ddr/models/admin_set.rb +5 -16
  80. data/lib/ddr/models/base.rb +45 -165
  81. data/lib/ddr/models/describable.rb +19 -21
  82. data/lib/ddr/models/engine.rb +11 -39
  83. data/lib/ddr/models/error.rb +0 -3
  84. data/lib/ddr/models/file_management.rb +36 -72
  85. data/lib/ddr/models/finding_aid.rb +2 -35
  86. data/lib/ddr/models/fixity_checkable.rb +4 -0
  87. data/lib/ddr/models/governable.rb +3 -1
  88. data/lib/ddr/models/has_admin_metadata.rb +82 -56
  89. data/lib/ddr/models/has_attachments.rb +3 -1
  90. data/lib/ddr/models/has_children.rb +3 -7
  91. data/lib/ddr/models/has_content.rb +35 -32
  92. data/lib/ddr/models/has_multires_image.rb +7 -12
  93. data/lib/ddr/models/has_struct_metadata.rb +44 -6
  94. data/lib/ddr/models/has_thumbnail.rb +1 -4
  95. data/lib/ddr/models/indexing.rb +97 -186
  96. data/lib/ddr/models/licenses/admin_policy_license.rb +11 -0
  97. data/lib/ddr/models/licenses/effective_license.rb +9 -0
  98. data/lib/ddr/models/licenses/inherited_license.rb +9 -0
  99. data/lib/ddr/models/licenses/license.rb +21 -0
  100. data/lib/ddr/models/licenses/parent_license.rb +11 -0
  101. data/lib/ddr/models/metadata/descriptive_metadata.rb +50 -0
  102. data/lib/ddr/models/metadata/metadata.rb +37 -0
  103. data/lib/ddr/models/metadata/metadata_mapper.rb +32 -0
  104. data/lib/ddr/models/metadata/metadata_mappers.rb +18 -0
  105. data/lib/ddr/models/metadata/metadata_term.rb +24 -0
  106. data/lib/ddr/models/metadata/metadata_vocabularies.rb +18 -0
  107. data/lib/ddr/models/metadata/metadata_vocabulary.rb +34 -0
  108. data/lib/ddr/models/solr_document.rb +24 -156
  109. data/lib/ddr/models/struct_div.rb +63 -0
  110. data/lib/ddr/models/structure.rb +39 -175
  111. data/lib/ddr/models/version.rb +1 -1
  112. data/lib/ddr/models/year_facet.rb +118 -60
  113. data/lib/ddr/notifications.rb +12 -10
  114. data/lib/ddr/utils.rb +15 -28
  115. data/lib/ddr/vocab.rb +17 -15
  116. data/lib/ddr/vocab/asset.rb +17 -42
  117. data/lib/ddr/vocab/contact.rb +7 -5
  118. data/lib/ddr/vocab/display.rb +8 -6
  119. data/lib/ddr/vocab/duke_terms.rb +10 -8
  120. data/lib/ddr/vocab/rdf_vocabulary_parser.rb +39 -37
  121. data/lib/ddr/vocab/roles.rb +19 -17
  122. data/lib/ddr/vocab/sources/duketerms.rdf +8 -22
  123. data/lib/ddr/vocab/vocabulary.rb +26 -26
  124. data/spec/auth/ability_factory_spec.rb +1 -18
  125. data/spec/auth/ability_spec.rb +15 -119
  126. data/spec/auth/effective_permissions_spec.rb +1 -1
  127. data/spec/auth/effective_roles_spec.rb +1 -1
  128. data/spec/auth/ldap_gateway_spec.rb +9 -0
  129. data/spec/contacts/contacts_spec.rb +26 -0
  130. data/spec/controllers/including_role_based_access_controls_enforcement_spec.rb +5 -5
  131. data/spec/derivatives/multires_image_spec.rb +48 -0
  132. data/spec/derivatives/png_generator_spec.rb +8 -13
  133. data/spec/derivatives/ptif_generator_spec.rb +11 -9
  134. data/spec/derivatives/thumbnail_spec.rb +48 -0
  135. data/spec/dummy/config/environments/test.rb +1 -1
  136. data/spec/dummy/db/schema.rb +23 -25
  137. data/spec/factories/attachment_factories.rb +2 -2
  138. data/spec/factories/collection_factories.rb +2 -3
  139. data/spec/factories/component_factories.rb +2 -2
  140. data/spec/factories/item_factories.rb +2 -2
  141. data/spec/factories/structure_factories.rb +2 -8
  142. data/spec/factories/test_model_factories.rb +12 -4
  143. data/spec/fixtures/fits/document.xml +44 -42
  144. data/spec/fixtures/fits/image.xml +41 -42
  145. data/spec/helpers/models_helper_spec.rb +11 -0
  146. data/spec/index/filter_spec.rb +30 -208
  147. data/spec/index/filters_spec.rb +17 -0
  148. data/spec/index/query_spec.rb +10 -82
  149. data/spec/jobs/fits_file_characterization_spec.rb +52 -0
  150. data/spec/managers/derivatives_manager_spec.rb +105 -107
  151. data/spec/managers/technical_metadata_manager_spec.rb +27 -34
  152. data/spec/models/active_fedora_base_spec.rb +105 -5
  153. data/spec/models/active_fedora_datastream_spec.rb +39 -106
  154. data/spec/models/admin_set_spec.rb +10 -43
  155. data/spec/models/attachment_spec.rb +2 -3
  156. data/spec/models/collection_spec.rb +11 -225
  157. data/spec/models/component_spec.rb +4 -106
  158. data/spec/models/descriptive_metadata_spec.rb +38 -0
  159. data/spec/models/effective_license_spec.rb +64 -0
  160. data/spec/models/events_spec.rb +59 -52
  161. data/spec/models/file_management_spec.rb +13 -113
  162. data/spec/models/finding_aid_spec.rb +8 -31
  163. data/spec/models/has_admin_metadata_spec.rb +122 -131
  164. data/spec/models/has_children_spec.rb +3 -3
  165. data/spec/models/has_struct_metadata_spec.rb +37 -50
  166. data/spec/models/indexing_spec.rb +23 -160
  167. data/spec/models/item_spec.rb +2 -133
  168. data/spec/models/license_spec.rb +24 -0
  169. data/spec/models/solr_document_spec.rb +47 -236
  170. data/spec/models/struct_div_spec.rb +70 -0
  171. data/spec/models/structure_spec.rb +11 -89
  172. data/spec/models/target_spec.rb +2 -4
  173. data/spec/models/year_facet_spec.rb +23 -50
  174. data/spec/spec_helper.rb +13 -17
  175. data/spec/support/ezid_mock_identifier.rb +2 -2
  176. data/spec/support/shared_examples_for_auth_contexts.rb +0 -33
  177. data/spec/support/shared_examples_for_ddr_models.rb +94 -142
  178. data/spec/support/shared_examples_for_describables.rb +36 -31
  179. data/spec/support/shared_examples_for_governables.rb +2 -3
  180. data/spec/support/shared_examples_for_has_content.rb +46 -101
  181. data/spec/support/shared_examples_for_indexing.rb +6 -6
  182. data/spec/support/shared_examples_for_non_collection_models.rb +1 -32
  183. data/spec/support/structural_metadata_helper.rb +59 -230
  184. data/spec/utils_spec.rb +2 -0
  185. metadata +96 -229
  186. data/.ruby-version +0 -1
  187. data/config/initializers/rsolr_monkey_patches.rb +0 -7
  188. data/config/initializers/rubydora_monkey_patch.rb +0 -11
  189. data/config/locales/ddr-models.en.yml +0 -81
  190. data/db/migrate/20161021201011_add_permanent_id_to_events.rb +0 -8
  191. data/lib/ddr/auth/ability_definitions/admin_set_ability_definitions.rb +0 -9
  192. data/lib/ddr/auth/ability_definitions/lock_ability_definitions.rb +0 -13
  193. data/lib/ddr/auth/ability_definitions/publication_ability_definitions.rb +0 -16
  194. data/lib/ddr/datastreams/administrative_metadata_datastream.rb +0 -75
  195. data/lib/ddr/datastreams/caption_datastream.rb +0 -5
  196. data/lib/ddr/datastreams/content_datastream.rb +0 -17
  197. data/lib/ddr/datastreams/delete_external_files.rb +0 -29
  198. data/lib/ddr/datastreams/external_file_datastream.rb +0 -80
  199. data/lib/ddr/datastreams/intermediate_file_datastream.rb +0 -5
  200. data/lib/ddr/datastreams/multires_image_datastream.rb +0 -5
  201. data/lib/ddr/datastreams/streamable_media_datastream.rb +0 -5
  202. data/lib/ddr/derivatives/png_generator.rb +0 -17
  203. data/lib/ddr/derivatives/ptif_generator.rb +0 -55
  204. data/lib/ddr/derivatives/update_derivatives.rb +0 -25
  205. data/lib/ddr/events/deaccession_event.rb +0 -8
  206. data/lib/ddr/index/field_attribute.rb +0 -22
  207. data/lib/ddr/index/query_params.rb +0 -40
  208. data/lib/ddr/index/sort_order.rb +0 -28
  209. data/lib/ddr/models/cache.rb +0 -20
  210. data/lib/ddr/models/captionable.rb +0 -37
  211. data/lib/ddr/models/contact.rb +0 -23
  212. data/lib/ddr/models/file_characterization.rb +0 -37
  213. data/lib/ddr/models/has_intermediate_file.rb +0 -41
  214. data/lib/ddr/models/language.rb +0 -31
  215. data/lib/ddr/models/media_type.rb +0 -22
  216. data/lib/ddr/models/permanent_id.rb +0 -240
  217. data/lib/ddr/models/rights_statement.rb +0 -25
  218. data/lib/ddr/models/streamable.rb +0 -37
  219. data/lib/ddr/models/structures/agent.rb +0 -49
  220. data/lib/ddr/models/structures/component_type_term.rb +0 -29
  221. data/lib/ddr/models/structures/div.rb +0 -64
  222. data/lib/ddr/models/structures/f_locat.rb +0 -54
  223. data/lib/ddr/models/structures/file.rb +0 -52
  224. data/lib/ddr/models/structures/file_grp.rb +0 -35
  225. data/lib/ddr/models/structures/file_sec.rb +0 -22
  226. data/lib/ddr/models/structures/fptr.rb +0 -31
  227. data/lib/ddr/models/structures/mets_hdr.rb +0 -37
  228. data/lib/ddr/models/structures/mptr.rb +0 -49
  229. data/lib/ddr/models/structures/struct_map.rb +0 -40
  230. data/lib/ddr/models/with_content_file.rb +0 -37
  231. data/spec/datastreams/external_file_datastream_spec.rb +0 -48
  232. data/spec/dummy/config/structure_component_type.yml +0 -18
  233. data/spec/fixtures/abcd1234.vtt +0 -38
  234. data/spec/fixtures/arrow1rightred_e0.gif +0 -0
  235. data/spec/fixtures/extractedText1.txt +0 -1
  236. data/spec/fixtures/extractedText2.txt +0 -1
  237. data/spec/fixtures/extractedText3.txt +0 -3
  238. data/spec/fixtures/imageA.jpg +0 -0
  239. data/spec/fixtures/imageB.jpg +0 -0
  240. data/spec/index/csv_query_result_spec.rb +0 -44
  241. data/spec/index/fields_spec.rb +0 -380
  242. data/spec/index/query_builder_spec.rb +0 -140
  243. data/spec/index/query_clause_spec.rb +0 -68
  244. data/spec/models/cache_spec.rb +0 -32
  245. data/spec/models/contact_spec.rb +0 -56
  246. data/spec/models/descriptive_metadata_datastream_spec.rb +0 -139
  247. data/spec/models/file_characterization_spec.rb +0 -39
  248. data/spec/models/language_spec.rb +0 -59
  249. data/spec/models/media_type_spec.rb +0 -46
  250. data/spec/models/permanent_id_spec.rb +0 -432
  251. data/spec/models/rights_statement_spec.rb +0 -55
  252. data/spec/models/structures/agent_spec.rb +0 -30
  253. data/spec/models/structures/component_type_term_spec.rb +0 -14
  254. data/spec/models/structures/div_spec.rb +0 -26
  255. data/spec/models/structures/f_locat_spec.rb +0 -78
  256. data/spec/models/structures/file_grp_spec.rb +0 -23
  257. data/spec/models/structures/file_sec_spec.rb +0 -22
  258. data/spec/models/structures/file_spec.rb +0 -60
  259. data/spec/models/structures/fptr_spec.rb +0 -23
  260. data/spec/models/structures/mets_hdr_spec.rb +0 -26
  261. data/spec/models/structures/mptr_spec.rb +0 -25
  262. data/spec/models/structures/struct_map_spec.rb +0 -24
  263. data/spec/models/with_content_file_spec.rb +0 -37
  264. data/spec/support/shared_examples_for_associations.rb +0 -8
  265. data/spec/support/shared_examples_for_captionable.rb +0 -23
  266. data/spec/support/shared_examples_for_has_intermediate_file.rb +0 -28
  267. data/spec/support/shared_examples_for_publication.rb +0 -43
  268. data/spec/support/shared_examples_for_streamable_media.rb +0 -23
@@ -1,121 +1,48 @@
1
- require "forwardable"
2
- require "virtus"
3
-
4
1
  module Ddr::Index
5
2
  class Filter
6
- include Virtus.model
7
-
8
- attribute :clauses, Array, default: [ ]
9
3
 
10
- def ==(other)
11
- other.instance_of?(self.class) && (other.clauses == self.clauses)
4
+ class << self
5
+ delegate :where, :raw, :before_days, :before, :present, :absent, to: :new
12
6
  end
13
7
 
14
- module Api
15
- def raw(*clauses)
16
- self.clauses += clauses
17
- self
18
- end
19
-
20
- def term(conditions)
21
- self.clauses += conditions.map { |f, v| QueryClause.term(f, v) }
22
- self
23
- end
24
-
25
- def where(conditions)
26
- self.clauses += conditions.map { |f, v| QueryClause.where(f, v) }
27
- self
28
- end
29
-
30
- def where_not(conditions)
31
- self.clauses += conditions.map do |field, v|
32
- Array(v).map { |value| QueryClause.negative(field, value) }
33
- end.flatten
34
- self
35
- end
36
-
37
- def absent(field)
38
- self.clauses << QueryClause.absent(field)
39
- self
40
- end
41
-
42
- def present(field)
43
- self.clauses << QueryClause.present(field)
44
- self
45
- end
46
-
47
- def negative(field, value)
48
- self.clauses << QueryClause.negative(field, value)
49
- self
50
- end
51
-
52
- def before(field, value)
53
- self.clauses << QueryClause.before(field, value)
54
- self
55
- end
8
+ attr_accessor :clauses
56
9
 
57
- def before_days(field, value)
58
- self.clauses << QueryClause.before_days(field, value)
59
- self
60
- end
61
-
62
- def join(**args)
63
- self.clauses << QueryClause.join(**args)
64
- self
65
- end
66
-
67
- def regexp(field, value)
68
- self.clauses << QueryClause.regexp(field, value)
69
- self
70
- end
10
+ def initialize
11
+ @clauses = [ ]
71
12
  end
72
13
 
73
- module ClassMethods
74
- extend Forwardable
75
-
76
- delegate Api.public_instance_methods => :new_filter
77
-
78
- def has_content
79
- model "Component", "Attachment", "Target"
80
- end
81
-
82
- def is_governed_by(arg)
83
- term is_governed_by: get_uri(arg)
84
- end
85
-
86
- def is_member_of_collection(arg)
87
- term is_member_of_collection: get_uri(arg)
88
- end
89
-
90
- def is_part_of(arg)
91
- term is_part_of: get_uri(arg)
92
- end
93
-
94
- def model(*models)
95
- where active_fedora_model: models
96
- end
97
-
98
- private
99
-
100
- def get_uri(arg)
101
- case arg
102
- when ActiveFedora::Base
103
- arg.internal_uri
104
- when String
105
- arg.start_with?("info:fedora/") ? arg : "info:fedora/#{arg}"
14
+ def where(conditions)
15
+ clauses = conditions.map do |field, value|
16
+ if value.respond_to?(:each)
17
+ QueryClause.or_values(field, *value)
106
18
  else
107
- raise TypeError, "Argument must be a String or ActiveFedora::Base instance: #{arg.class}"
19
+ QueryClause.term(field, value)
108
20
  end
109
21
  end
22
+ raw *clauses
23
+ end
110
24
 
111
- def new_filter
112
- Filter.new
113
- end
25
+ # Adds clause (String) w/o escaping
26
+ def raw(*clauses)
27
+ self.clauses += clauses
28
+ self
29
+ end
114
30
 
31
+ def present(field)
32
+ raw QueryClause.present(field)
115
33
  end
116
34
 
117
- include Api
118
- extend ClassMethods
35
+ def absent(field)
36
+ raw QueryClause.absent(field)
37
+ end
38
+
39
+ def before(field, date_time)
40
+ raw QueryClause.before(field, date_time)
41
+ end
42
+
43
+ def before_days(field, days)
44
+ raw QueryClause.before_days(field, days)
45
+ end
119
46
 
120
47
  end
121
48
  end
@@ -0,0 +1,19 @@
1
+ module Ddr::Index
2
+ module Filters
3
+
4
+ HAS_CONTENT = Filter.where(Fields::ACTIVE_FEDORA_MODEL => ["Component", "Attachment", "Target"])
5
+
6
+ class << self
7
+ def is_governed_by(pid)
8
+ Filter.where(Fields::IS_GOVERNED_BY => pid)
9
+ end
10
+
11
+ def internal_uri(pid)
12
+ ActiveFedora::Base.internal_uri(pid)
13
+ end
14
+ end
15
+
16
+ private_class_method :internal_uri
17
+
18
+ end
19
+ end
@@ -1,33 +1,9 @@
1
- require "virtus"
2
- require "forwardable"
3
-
4
1
  module Ddr::Index
5
2
  class Query
6
- include Virtus.model
7
- extend Forwardable
8
- extend Deprecation
9
-
10
- attribute :q, String
11
- attribute :fields, Array[FieldAttribute], default: [ ]
12
- attribute :filters, Array[Filter], default: [ ]
13
- attribute :sort, Array[String], default: [ ]
14
- attribute :rows, Integer
15
3
 
16
- delegate [:count, :docs, :ids, :each_id, :all] => :result
17
- delegate :params => :query_params
18
-
19
- def self.build(*args, &block)
20
- new.tap do |query|
21
- query.build(*args, &block)
22
- end
23
- end
4
+ attr_reader :q, :fields, :filters, :sort, :rows
24
5
 
25
- def initialize(**args, &block)
26
- super(**args)
27
- if block_given?
28
- build(&block)
29
- end
30
- end
6
+ delegate :count, :docs, :pids, :each_pid, :all, to: :result
31
7
 
32
8
  def inspect
33
9
  "#<#{self.class.name} q=#{q.inspect}, filters=#{filters.inspect}," \
@@ -38,44 +14,21 @@ module Ddr::Index
38
14
  URI.encode_www_form(params)
39
15
  end
40
16
 
41
- def pids
42
- Deprecation.warn(QueryResult, "`pids` is deprecated; use `ids` instead.")
43
- ids
44
- end
45
-
46
- def each_pid(&block)
47
- Deprecation.warn(QueryResult, "`each_pid` is deprecated; use `each_id` instead.")
48
- each_id(&block)
17
+ def params
18
+ { q: q,
19
+ fq: filters.map(&:clauses).flatten,
20
+ fl: fields.join(","),
21
+ sort: sort.join(","),
22
+ rows: rows,
23
+ }.select { |k, v| v.present? }
49
24
  end
50
25
 
51
26
  def result
52
27
  QueryResult.new(self)
53
28
  end
54
29
 
55
- def csv
56
- CSVQueryResult.new(self)
57
- end
58
-
59
- def filter_clauses
60
- filters.map(&:clauses).flatten
61
- end
62
-
63
- def query_params
64
- QueryParams.new(self)
65
- end
66
-
67
- def build(*args, &block)
68
- QueryBuilder.new(self, *args, &block)
69
- self
70
- end
71
-
72
- def ==(other)
73
- other.instance_of?(self.class) &&
74
- other.q == self.q &&
75
- other.fields == self.fields &&
76
- other.filters == self.filters &&
77
- other.rows == self.rows &&
78
- other.sort == self.sort
30
+ def csv(**opts)
31
+ CSVQueryResult.new(self, **opts)
79
32
  end
80
33
 
81
34
  end
@@ -1,184 +1,73 @@
1
1
  module Ddr::Index
2
- #
3
- # QueryBuilder - Provides a DSL for building a Query.
4
- #
5
- # Note: Where a method receives a [field] parameter, the parameter value is
6
- # coerced to a Field instance. See FieldAttribute#coerce for details.
7
- #
8
- # *** DSL METHODS ***
9
- #
10
- # absent [field]
11
- # Adds a filter selecting documents where the field is not present (no values).
12
- #
13
- # asc [field], ...
14
- # Adds ascending orderings by the fields specified.
15
- #
16
- # See also: desc, order_by
17
- #
18
- # before [field], [date_time]
19
- # Adds a filter selecting documents where the field has a date/time before
20
- # (earlier than) the value.
21
- #
22
- # before_days [field], [int]
23
- # Adds a filter selecting documents where the field has a date/time the
24
- # specified number of days before today (now) or earlier.
25
- #
26
- # desc [field], ...
27
- # Adds descending orderings by the fields specified.
28
- #
29
- # See also: asc, order_by
30
- #
31
- # filter [filter1], ...
32
- # Adds filters to the query.
33
- #
34
- # Aliased as: filters
35
- #
36
- # filters [filter], ...
37
- # Alias for: filter
38
- #
39
- # field [field1], ...
40
- # Adds fields to result documents.
41
- # Note that all fields are returned when none is specified.
42
- #
43
- # Aliased as: fields
44
- #
45
- # fields [field], ...
46
- # Alias for: field
47
- #
48
- # id [doc_id]
49
- # For selecting a single document by ID.
50
- #
51
- # join [from: {field1}, to: {field2}, where: {condition}]
52
- # Adds a Solr join clause (see https://wiki.apache.org/solr/Join)
53
- #
54
- # limit [int]
55
- # Limits the number of documents returned by the query.
56
- #
57
- # Aliased as: rows
58
- #
59
- # model [model_name], ...
60
- # Adds a filter selecting document where ActiveFedora model equals value
61
- # or one of the values.
62
- #
63
- # negative [field], [value]
64
- # Adds a filter selecting document where field does not have the value.
65
- #
66
- # order_by [{field => order, ...}], ...
67
- # Adds ordering(s) to the query.
68
- #
69
- # Aliased as: sort
70
- #
71
- # present [field]
72
- # Adds a filter selecting document where the field has any value.
73
- #
74
- # q [query_clause]
75
- # Sets a query clause for the `q` parameter.
76
- #
77
- # raw [clause1], ...
78
- # Adds a filter of "raw" query clauses (i.e., pre-constructed).
79
- #
80
- # regexp [field], [regexp]
81
- # Adds a filter selecting documents where the field has a value
82
- # matching the regular expression.
83
- # Slashes (/) in the regexp will be escaped as required by Solr.
84
- #
85
- # rows [int]
86
- # Alias for: limit
87
- #
88
- # sort [{field => order, ...}]
89
- # Alias for: order_by
90
- #
91
- # term [{field => value, ...}]
92
- # Adds a filter of "term" query clauses for the fields and values.
93
- #
94
- # where [{field => value, ...}]
95
- # Adds a filter of "standard" query clauses.
96
- # Values will be escaped when the filter is serialized.
97
- # If a hash value is an array, that query clause will select documents
98
- # where the field matches any array entry.
99
- #
100
2
  class QueryBuilder
101
3
 
102
- attr_reader :query
4
+ def self.build
5
+ builder = new
6
+ yield builder
7
+ builder.query
8
+ end
9
+
10
+ def initialize
11
+ @q = nil
12
+ @fields = [ ]
13
+ @filters = [ ]
14
+ @sort = [ ]
15
+ @rows = nil
16
+ end
103
17
 
104
- def initialize(*args, &block)
105
- @query = args.first.is_a?(Query) ? args.shift : Query.new
106
- if block_given?
107
- instance_exec(*args, &block)
18
+ def query
19
+ Query.new.tap do |qry|
20
+ instance_variables.each do |var|
21
+ qry.instance_variable_set(var, instance_variable_get(var))
22
+ end
108
23
  end
109
24
  end
110
25
 
111
- # @param pid [String]
112
- # @return [QueryBuilder]
113
26
  def id(pid)
114
27
  q QueryClause.id(pid)
115
28
  limit 1
116
29
  end
117
30
 
118
- # @param filters [Array<Filter>]
119
- # @return [QueryBuilder]
120
- def filter(*filters)
121
- query.filters += filters
31
+ def filter(*fltrs)
32
+ @filters.push *fltrs
122
33
  self
123
34
  end
124
- alias_method :filters, :filter
125
35
 
126
- # @param fields [Array<Field>]
127
- # @return [QueryBuilder] self
128
- def field(*fields)
129
- query.fields += fields.flatten.map { |f| FieldAttribute.coerce(f) }
36
+ def fields(*flds)
37
+ @fields.push *flds
130
38
  self
131
39
  end
132
- alias_method :fields, :field
133
40
 
134
- # @param num [Integer]
135
- # @return [QueryBuilder] self
136
41
  def limit(num)
137
- query.rows = num.to_i
42
+ @rows = num
138
43
  self
139
44
  end
140
- alias_method :rows, :limit
141
45
 
142
- # @param orderings [Hash<Field, String>]
143
- # @return [QueryBuilder] self
144
- def order_by(*orderings)
145
- query.sort += orderings.first.map { |field, order| SortOrder.new(field: field, order: order) }
46
+ def order_by(field, order)
47
+ @sort << [field, order].join(" ")
146
48
  self
147
49
  end
148
- alias_method :sort, :order_by
149
50
 
150
- # @param fields [Array<Field, Symbol, String>]
151
- # @return [QueryBuilder] self
152
- def asc(*fields)
153
- query.sort += fields.map { |field| SortOrder.asc(field) }
154
- self
51
+ def asc(field)
52
+ order_by field, "asc"
155
53
  end
156
- # @param fields [Array<Field, Symbol, String>]
157
- # @return [QueryBuilder] self
158
- def desc(*fields)
159
- query.sort += fields.map { |field| SortOrder.desc(field) }
160
- self
54
+
55
+ def desc(field)
56
+ order_by field, "desc"
161
57
  end
162
58
 
163
- # @param query_clause [QueryClause, String]
164
- # @return [QueryBuilder] self
165
- def q(query_clause)
166
- query.q = query_clause
59
+ def q(q)
60
+ @q = q
167
61
  self
168
62
  end
169
63
 
170
- private
171
-
172
- def respond_to_missing?(name, include_all)
173
- Filter::ClassMethods.public_instance_methods.include?(name)
174
- end
64
+ protected
175
65
 
176
66
  def method_missing(name, *args, &block)
177
- if respond_to?(name)
178
- filter Filter.send(name, *args)
179
- else
180
- super
67
+ if Filter.respond_to? name
68
+ return filter Filter.send(name, *args, &block)
181
69
  end
70
+ super
182
71
  end
183
72
 
184
73
  end