mark_mapper 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (211) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +21 -0
  3. data/README.rdoc +39 -0
  4. data/examples/attr_accessible.rb +24 -0
  5. data/examples/attr_protected.rb +24 -0
  6. data/examples/cache_key.rb +26 -0
  7. data/examples/custom_types.rb +26 -0
  8. data/examples/identity_map.rb +30 -0
  9. data/examples/identity_map/automatic.rb +2 -0
  10. data/examples/keys.rb +42 -0
  11. data/examples/modifiers/set.rb +27 -0
  12. data/examples/plugins.rb +40 -0
  13. data/examples/querying.rb +39 -0
  14. data/examples/sample_app.rb +43 -0
  15. data/examples/scopes.rb +56 -0
  16. data/examples/validating/embedded_docs.rb +31 -0
  17. data/lib/mark_mapper.rb +125 -0
  18. data/lib/mark_mapper/config.rb +90 -0
  19. data/lib/mark_mapper/connection.rb +60 -0
  20. data/lib/mark_mapper/criteria_hash.rb +194 -0
  21. data/lib/mark_mapper/document.rb +46 -0
  22. data/lib/mark_mapper/embedded_document.rb +32 -0
  23. data/lib/mark_mapper/exceptions.rb +33 -0
  24. data/lib/mark_mapper/extensions/array.rb +27 -0
  25. data/lib/mark_mapper/extensions/boolean.rb +45 -0
  26. data/lib/mark_mapper/extensions/date.rb +29 -0
  27. data/lib/mark_mapper/extensions/duplicable.rb +86 -0
  28. data/lib/mark_mapper/extensions/float.rb +18 -0
  29. data/lib/mark_mapper/extensions/hash.rb +26 -0
  30. data/lib/mark_mapper/extensions/integer.rb +27 -0
  31. data/lib/mark_mapper/extensions/kernel.rb +11 -0
  32. data/lib/mark_mapper/extensions/nil_class.rb +18 -0
  33. data/lib/mark_mapper/extensions/object.rb +30 -0
  34. data/lib/mark_mapper/extensions/object_id.rb +18 -0
  35. data/lib/mark_mapper/extensions/set.rb +20 -0
  36. data/lib/mark_mapper/extensions/string.rb +31 -0
  37. data/lib/mark_mapper/extensions/symbol.rb +87 -0
  38. data/lib/mark_mapper/extensions/time.rb +29 -0
  39. data/lib/mark_mapper/locale/en.yml +5 -0
  40. data/lib/mark_mapper/middleware/identity_map.rb +41 -0
  41. data/lib/mark_mapper/normalizers/criteria_hash_key.rb +17 -0
  42. data/lib/mark_mapper/normalizers/criteria_hash_value.rb +66 -0
  43. data/lib/mark_mapper/normalizers/fields_value.rb +26 -0
  44. data/lib/mark_mapper/normalizers/hash_key.rb +19 -0
  45. data/lib/mark_mapper/normalizers/integer.rb +19 -0
  46. data/lib/mark_mapper/normalizers/options_hash_value.rb +83 -0
  47. data/lib/mark_mapper/normalizers/sort_value.rb +55 -0
  48. data/lib/mark_mapper/options_hash.rb +103 -0
  49. data/lib/mark_mapper/pagination.rb +6 -0
  50. data/lib/mark_mapper/pagination/collection.rb +32 -0
  51. data/lib/mark_mapper/pagination/paginator.rb +46 -0
  52. data/lib/mark_mapper/plugins.rb +22 -0
  53. data/lib/mark_mapper/plugins/accessible.rb +61 -0
  54. data/lib/mark_mapper/plugins/active_model.rb +18 -0
  55. data/lib/mark_mapper/plugins/associations.rb +96 -0
  56. data/lib/mark_mapper/plugins/associations/base.rb +98 -0
  57. data/lib/mark_mapper/plugins/associations/belongs_to_association.rb +63 -0
  58. data/lib/mark_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb +35 -0
  59. data/lib/mark_mapper/plugins/associations/belongs_to_proxy.rb +52 -0
  60. data/lib/mark_mapper/plugins/associations/collection.rb +29 -0
  61. data/lib/mark_mapper/plugins/associations/embedded_collection.rb +44 -0
  62. data/lib/mark_mapper/plugins/associations/in_array_proxy.rb +133 -0
  63. data/lib/mark_mapper/plugins/associations/many_association.rb +63 -0
  64. data/lib/mark_mapper/plugins/associations/many_documents_as_proxy.rb +28 -0
  65. data/lib/mark_mapper/plugins/associations/many_documents_proxy.rb +142 -0
  66. data/lib/mark_mapper/plugins/associations/many_embedded_polymorphic_proxy.rb +32 -0
  67. data/lib/mark_mapper/plugins/associations/many_embedded_proxy.rb +24 -0
  68. data/lib/mark_mapper/plugins/associations/many_polymorphic_proxy.rb +14 -0
  69. data/lib/mark_mapper/plugins/associations/one_as_proxy.rb +22 -0
  70. data/lib/mark_mapper/plugins/associations/one_association.rb +48 -0
  71. data/lib/mark_mapper/plugins/associations/one_embedded_polymorphic_proxy.rb +30 -0
  72. data/lib/mark_mapper/plugins/associations/one_embedded_proxy.rb +44 -0
  73. data/lib/mark_mapper/plugins/associations/one_proxy.rb +95 -0
  74. data/lib/mark_mapper/plugins/associations/proxy.rb +138 -0
  75. data/lib/mark_mapper/plugins/associations/single_association.rb +46 -0
  76. data/lib/mark_mapper/plugins/caching.rb +21 -0
  77. data/lib/mark_mapper/plugins/callbacks.rb +42 -0
  78. data/lib/mark_mapper/plugins/clone.rb +24 -0
  79. data/lib/mark_mapper/plugins/counter_cache.rb +97 -0
  80. data/lib/mark_mapper/plugins/dirty.rb +61 -0
  81. data/lib/mark_mapper/plugins/document.rb +41 -0
  82. data/lib/mark_mapper/plugins/dumpable.rb +22 -0
  83. data/lib/mark_mapper/plugins/dynamic_querying.rb +45 -0
  84. data/lib/mark_mapper/plugins/dynamic_querying/dynamic_finder.rb +44 -0
  85. data/lib/mark_mapper/plugins/embedded_callbacks.rb +81 -0
  86. data/lib/mark_mapper/plugins/embedded_document.rb +53 -0
  87. data/lib/mark_mapper/plugins/equality.rb +23 -0
  88. data/lib/mark_mapper/plugins/identity_map.rb +144 -0
  89. data/lib/mark_mapper/plugins/indexable.rb +86 -0
  90. data/lib/mark_mapper/plugins/inspect.rb +16 -0
  91. data/lib/mark_mapper/plugins/keys.rb +470 -0
  92. data/lib/mark_mapper/plugins/keys/key.rb +134 -0
  93. data/lib/mark_mapper/plugins/keys/static.rb +45 -0
  94. data/lib/mark_mapper/plugins/logger.rb +18 -0
  95. data/lib/mark_mapper/plugins/modifiers.rb +140 -0
  96. data/lib/mark_mapper/plugins/pagination.rb +16 -0
  97. data/lib/mark_mapper/plugins/partial_updates.rb +77 -0
  98. data/lib/mark_mapper/plugins/persistence.rb +79 -0
  99. data/lib/mark_mapper/plugins/protected.rb +45 -0
  100. data/lib/mark_mapper/plugins/querying.rb +173 -0
  101. data/lib/mark_mapper/plugins/querying/decorated_markmapper_query.rb +75 -0
  102. data/lib/mark_mapper/plugins/rails.rb +79 -0
  103. data/lib/mark_mapper/plugins/rails/active_record_association_adapter.rb +33 -0
  104. data/lib/mark_mapper/plugins/sci.rb +82 -0
  105. data/lib/mark_mapper/plugins/scopes.rb +28 -0
  106. data/lib/mark_mapper/plugins/serialization.rb +109 -0
  107. data/lib/mark_mapper/plugins/timestamps.rb +29 -0
  108. data/lib/mark_mapper/plugins/touch.rb +18 -0
  109. data/lib/mark_mapper/plugins/userstamps.rb +18 -0
  110. data/lib/mark_mapper/plugins/validations.rb +96 -0
  111. data/lib/mark_mapper/query.rb +278 -0
  112. data/lib/mark_mapper/railtie.rb +52 -0
  113. data/lib/mark_mapper/railtie/database.rake +65 -0
  114. data/lib/mark_mapper/translation.rb +10 -0
  115. data/lib/mark_mapper/version.rb +4 -0
  116. data/lib/rails/generators/mark_mapper/config/config_generator.rb +37 -0
  117. data/lib/rails/generators/mark_mapper/config/templates/marklogic.yml +19 -0
  118. data/lib/rails/generators/mark_mapper/model/model_generator.rb +40 -0
  119. data/lib/rails/generators/mark_mapper/model/templates/model.rb +17 -0
  120. data/spec/config/mark_mapper.yml +6 -0
  121. data/spec/examples_spec.rb +25 -0
  122. data/spec/functional/accessible_spec.rb +198 -0
  123. data/spec/functional/associations/belongs_to_polymorphic_proxy_spec.rb +64 -0
  124. data/spec/functional/associations/belongs_to_proxy_spec.rb +255 -0
  125. data/spec/functional/associations/in_array_proxy_spec.rb +349 -0
  126. data/spec/functional/associations/many_documents_as_proxy_spec.rb +230 -0
  127. data/spec/functional/associations/many_documents_proxy_spec.rb +968 -0
  128. data/spec/functional/associations/many_embedded_polymorphic_proxy_spec.rb +238 -0
  129. data/spec/functional/associations/many_embedded_proxy_spec.rb +288 -0
  130. data/spec/functional/associations/many_polymorphic_proxy_spec.rb +302 -0
  131. data/spec/functional/associations/one_as_proxy_spec.rb +489 -0
  132. data/spec/functional/associations/one_embedded_polymorphic_proxy_spec.rb +207 -0
  133. data/spec/functional/associations/one_embedded_proxy_spec.rb +100 -0
  134. data/spec/functional/associations/one_proxy_spec.rb +406 -0
  135. data/spec/functional/associations_spec.rb +48 -0
  136. data/spec/functional/caching_spec.rb +75 -0
  137. data/spec/functional/callbacks_spec.rb +330 -0
  138. data/spec/functional/counter_cache_spec.rb +235 -0
  139. data/spec/functional/dirty_spec.rb +316 -0
  140. data/spec/functional/document_spec.rb +310 -0
  141. data/spec/functional/dumpable_spec.rb +24 -0
  142. data/spec/functional/dynamic_querying_spec.rb +75 -0
  143. data/spec/functional/embedded_document_spec.rb +316 -0
  144. data/spec/functional/equality_spec.rb +20 -0
  145. data/spec/functional/extensions_spec.rb +16 -0
  146. data/spec/functional/identity_map_spec.rb +483 -0
  147. data/spec/functional/keys_spec.rb +339 -0
  148. data/spec/functional/logger_spec.rb +20 -0
  149. data/spec/functional/modifiers_spec.rb +446 -0
  150. data/spec/functional/options_hash_spec.rb +41 -0
  151. data/spec/functional/pagination_spec.rb +89 -0
  152. data/spec/functional/partial_updates_spec.rb +530 -0
  153. data/spec/functional/protected_spec.rb +199 -0
  154. data/spec/functional/querying_spec.rb +984 -0
  155. data/spec/functional/rails_spec.rb +55 -0
  156. data/spec/functional/sci_spec.rb +374 -0
  157. data/spec/functional/scopes_spec.rb +204 -0
  158. data/spec/functional/static_keys_spec.rb +153 -0
  159. data/spec/functional/timestamps_spec.rb +97 -0
  160. data/spec/functional/touch_spec.rb +125 -0
  161. data/spec/functional/userstamps_spec.rb +46 -0
  162. data/spec/functional/validations_spec.rb +416 -0
  163. data/spec/quality_spec.rb +51 -0
  164. data/spec/spec_helper.rb +150 -0
  165. data/spec/support/matchers.rb +15 -0
  166. data/spec/support/models.rb +256 -0
  167. data/spec/symbol_operator_spec.rb +70 -0
  168. data/spec/symbol_spec.rb +9 -0
  169. data/spec/unit/associations/base_spec.rb +146 -0
  170. data/spec/unit/associations/belongs_to_association_spec.rb +30 -0
  171. data/spec/unit/associations/many_association_spec.rb +64 -0
  172. data/spec/unit/associations/one_association_spec.rb +48 -0
  173. data/spec/unit/associations/proxy_spec.rb +103 -0
  174. data/spec/unit/clone_spec.rb +79 -0
  175. data/spec/unit/config_generator_spec.rb +24 -0
  176. data/spec/unit/criteria_hash_spec.rb +218 -0
  177. data/spec/unit/document_spec.rb +251 -0
  178. data/spec/unit/dynamic_finder_spec.rb +125 -0
  179. data/spec/unit/embedded_document_spec.rb +676 -0
  180. data/spec/unit/equality_spec.rb +38 -0
  181. data/spec/unit/exceptions_spec.rb +12 -0
  182. data/spec/unit/extensions_spec.rb +368 -0
  183. data/spec/unit/identity_map_middleware_spec.rb +134 -0
  184. data/spec/unit/inspect_spec.rb +47 -0
  185. data/spec/unit/key_spec.rb +276 -0
  186. data/spec/unit/keys_spec.rb +155 -0
  187. data/spec/unit/mark_mapper_spec.rb +37 -0
  188. data/spec/unit/model_generator_spec.rb +45 -0
  189. data/spec/unit/normalizers/criteria_hash_key_spec.rb +37 -0
  190. data/spec/unit/normalizers/criteria_hash_value_spec.rb +200 -0
  191. data/spec/unit/normalizers/fields_value_spec.rb +45 -0
  192. data/spec/unit/normalizers/hash_key_spec.rb +15 -0
  193. data/spec/unit/normalizers/integer_spec.rb +24 -0
  194. data/spec/unit/normalizers/options_hash_value_spec.rb +99 -0
  195. data/spec/unit/normalizers/sort_value_spec.rb +98 -0
  196. data/spec/unit/options_hash_spec.rb +64 -0
  197. data/spec/unit/pagination/collection_spec.rb +30 -0
  198. data/spec/unit/pagination/paginator_spec.rb +118 -0
  199. data/spec/unit/pagination_spec.rb +11 -0
  200. data/spec/unit/plugins_spec.rb +89 -0
  201. data/spec/unit/query_spec.rb +837 -0
  202. data/spec/unit/rails_compatibility_spec.rb +40 -0
  203. data/spec/unit/rails_reflect_on_association_spec.rb +118 -0
  204. data/spec/unit/rails_spec.rb +188 -0
  205. data/spec/unit/serialization_spec.rb +169 -0
  206. data/spec/unit/serializers/json_serializer_spec.rb +218 -0
  207. data/spec/unit/serializers/xml_serializer_spec.rb +198 -0
  208. data/spec/unit/time_zones_spec.rb +44 -0
  209. data/spec/unit/translation_spec.rb +27 -0
  210. data/spec/unit/validations_spec.rb +588 -0
  211. metadata +307 -0
@@ -0,0 +1,31 @@
1
+ $LOAD_PATH.unshift(File.expand_path('../../lib', __FILE__))
2
+ require_relative '../sample_app'
3
+ require 'pp'
4
+
5
+ MarkMapper.application.create
6
+
7
+ class Field
8
+ include MarkMapper::EmbeddedDocument
9
+ key :name
10
+ validates_presence_of :name
11
+ end
12
+
13
+ class Template
14
+ include MarkMapper::Document
15
+ key :name
16
+ many :fields
17
+
18
+ # This tells the template to validate all
19
+ # fields when validating the template.
20
+ validates_associated :fields
21
+ end
22
+
23
+ # Name is missing on embedded field
24
+ template = Template.new(:fields => [Field.new])
25
+ puts template.valid? # false
26
+
27
+ # Name is present on embedded field
28
+ template = Template.new(:fields => [Field.new(:name => 'Yay')])
29
+ puts template.valid? # true
30
+
31
+ MarkMapper.application.drop
@@ -0,0 +1,125 @@
1
+ # encoding: UTF-8
2
+ require 'marklogic'
3
+ require 'active_support'
4
+ require 'active_support/core_ext'
5
+ require 'active_model'
6
+ require "mark_mapper/railtie" if defined?(Rails)
7
+ require 'set'
8
+ require 'mark_mapper/criteria_hash'
9
+ require 'mark_mapper/options_hash'
10
+ require 'mark_mapper/query'
11
+ require 'mark_mapper/pagination'
12
+
13
+ I18n.load_path << File.expand_path('../mark_mapper/locale/en.yml', __FILE__)
14
+
15
+ module MarkMapper
16
+ autoload :Connection, 'mark_mapper/connection'
17
+
18
+ autoload :Error, 'mark_mapper/exceptions'
19
+ autoload :DocumentNotFound, 'mark_mapper/exceptions'
20
+ autoload :InvalidScheme, 'mark_mapper/exceptions'
21
+ autoload :DocumentNotValid, 'mark_mapper/exceptions'
22
+ autoload :AccessibleOrProtected, 'mark_mapper/exceptions'
23
+ autoload :InvalidKey, 'mark_mapper/exceptions'
24
+ autoload :NotSupported, 'mark_mapper/exceptions'
25
+
26
+ autoload :Document, 'mark_mapper/document'
27
+ autoload :EmbeddedDocument, 'mark_mapper/embedded_document'
28
+ autoload :Plugins, 'mark_mapper/plugins'
29
+ autoload :Translation, 'mark_mapper/translation'
30
+ autoload :Version, 'mark_mapper/version'
31
+
32
+ module Middleware
33
+ autoload :IdentityMap, 'mark_mapper/middleware/identity_map'
34
+ end
35
+
36
+ module Plugins
37
+ autoload :ActiveModel, 'mark_mapper/plugins/active_model'
38
+ autoload :Associations, 'mark_mapper/plugins/associations'
39
+ autoload :Accessible, 'mark_mapper/plugins/accessible'
40
+ autoload :Callbacks, 'mark_mapper/plugins/callbacks'
41
+ autoload :Caching, 'mark_mapper/plugins/caching'
42
+ autoload :Clone, 'mark_mapper/plugins/clone'
43
+ autoload :CounterCache, 'mark_mapper/plugins/counter_cache'
44
+ autoload :Dirty, 'mark_mapper/plugins/dirty'
45
+ autoload :Document, 'mark_mapper/plugins/document'
46
+ autoload :DynamicQuerying, 'mark_mapper/plugins/dynamic_querying'
47
+ autoload :Dumpable, 'mark_mapper/plugins/dumpable'
48
+ autoload :EmbeddedCallbacks, 'mark_mapper/plugins/embedded_callbacks'
49
+ autoload :EmbeddedDocument, 'mark_mapper/plugins/embedded_document'
50
+ autoload :Equality, 'mark_mapper/plugins/equality'
51
+ autoload :IdentityMap, 'mark_mapper/plugins/identity_map'
52
+ autoload :Inspect, 'mark_mapper/plugins/inspect'
53
+ autoload :Indexable, 'mark_mapper/plugins/indexable'
54
+ autoload :Keys, 'mark_mapper/plugins/keys'
55
+ autoload :Logger, 'mark_mapper/plugins/logger'
56
+ autoload :Modifiers, 'mark_mapper/plugins/modifiers'
57
+ autoload :Pagination, 'mark_mapper/plugins/pagination'
58
+ autoload :PartialUpdates, 'mark_mapper/plugins/partial_updates'
59
+ autoload :Persistence, 'mark_mapper/plugins/persistence'
60
+ autoload :Protected, 'mark_mapper/plugins/protected'
61
+ autoload :Querying, 'mark_mapper/plugins/querying'
62
+ autoload :Rails, 'mark_mapper/plugins/rails'
63
+ autoload :Sci, 'mark_mapper/plugins/sci'
64
+ autoload :Scopes, 'mark_mapper/plugins/scopes'
65
+ autoload :Serialization, 'mark_mapper/plugins/serialization'
66
+ autoload :Timestamps, 'mark_mapper/plugins/timestamps'
67
+ autoload :Userstamps, 'mark_mapper/plugins/userstamps'
68
+ autoload :Validations, 'mark_mapper/plugins/validations'
69
+ autoload :Touch, 'mark_mapper/plugins/touch'
70
+
71
+ module Associations
72
+ autoload :Base, 'mark_mapper/plugins/associations/base'
73
+ autoload :Collection, 'mark_mapper/plugins/associations/collection'
74
+ autoload :EmbeddedCollection, 'mark_mapper/plugins/associations/embedded_collection'
75
+ autoload :ManyAssociation, 'mark_mapper/plugins/associations/many_association'
76
+ autoload :SingleAssociation, 'mark_mapper/plugins/associations/single_association'
77
+ autoload :BelongsToAssociation, 'mark_mapper/plugins/associations/belongs_to_association'
78
+ autoload :OneAssociation, 'mark_mapper/plugins/associations/one_association'
79
+ autoload :ManyDocumentsProxy, 'mark_mapper/plugins/associations/many_documents_proxy'
80
+ autoload :BelongsToProxy, 'mark_mapper/plugins/associations/belongs_to_proxy'
81
+ autoload :BelongsToPolymorphicProxy, 'mark_mapper/plugins/associations/belongs_to_polymorphic_proxy'
82
+ autoload :ManyPolymorphicProxy, 'mark_mapper/plugins/associations/many_polymorphic_proxy'
83
+ autoload :ManyEmbeddedProxy, 'mark_mapper/plugins/associations/many_embedded_proxy'
84
+ autoload :ManyEmbeddedPolymorphicProxy, 'mark_mapper/plugins/associations/many_embedded_polymorphic_proxy'
85
+ autoload :ManyDocumentsAsProxy, 'mark_mapper/plugins/associations/many_documents_as_proxy'
86
+ autoload :OneProxy, 'mark_mapper/plugins/associations/one_proxy'
87
+ autoload :OneAsProxy, 'mark_mapper/plugins/associations/one_as_proxy'
88
+ autoload :OneEmbeddedProxy, 'mark_mapper/plugins/associations/one_embedded_proxy'
89
+ autoload :OneEmbeddedPolymorphicProxy, 'mark_mapper/plugins/associations/one_embedded_polymorphic_proxy'
90
+ autoload :InArrayProxy, 'mark_mapper/plugins/associations/in_array_proxy'
91
+ end
92
+ end
93
+
94
+ extend Connection
95
+
96
+ Methods = MarkMapper::Query::DSL.instance_methods.sort.map(&:to_sym)
97
+
98
+ def self.to_object_id(value)
99
+ return value if value.is_a?(MarkLogic::ObjectId)
100
+ return nil if value.nil? || (value.respond_to?(:empty?) && value.empty?)
101
+
102
+ if MarkLogic::ObjectId.legal?(value.to_s)
103
+ MarkLogic::ObjectId.from_string(value.to_s)
104
+ else
105
+ value
106
+ end
107
+ end
108
+
109
+ # Private
110
+ ModifierString = '$'
111
+
112
+ # Internal
113
+ def self.modifier?(key)
114
+ key.to_s[0, 1] == ModifierString
115
+ end
116
+ end
117
+
118
+ Dir[File.join(File.dirname(__FILE__), 'mark_mapper', 'extensions', '*.rb')].each do |extension|
119
+ require extension
120
+ end
121
+
122
+ # FIXME: autoload with proxy is failing, need to investigate
123
+ require 'mark_mapper/plugins/associations/proxy'
124
+
125
+ ActiveSupport.run_load_hooks(:mark_mapper, MarkMapper)
@@ -0,0 +1,90 @@
1
+ require 'yaml'
2
+
3
+ module MarkMapper
4
+
5
+ # This module defines all the configuration options for MarkMapper, including the
6
+ # database connections.
7
+ module Config
8
+ extend self
9
+
10
+ def indexes
11
+ @indexes ||= []
12
+ end
13
+
14
+ def settings
15
+ @settings ||= {
16
+ :host => 'localhost',
17
+ :port => 8003,
18
+ :manage_port => 8002,
19
+ :admin_port => 8001,
20
+ :app_name => defined?(Rails) ? Rails::Application.subclasses.first.parent.to_s.underscore : nil,
21
+ :username => nil,
22
+ :password => nil
23
+ }
24
+ end
25
+
26
+ def application
27
+ @application ||=
28
+ begin
29
+ MarkLogic::Application.new(app_name,
30
+ :port => port,
31
+ :connection => MarkLogic::Connection.new(host, port))
32
+ end
33
+ yield(@application) if block_given?
34
+ @application
35
+ end
36
+
37
+ # Load the settings from a compliant mark_mapper.yml file. This can be used for
38
+ # easy setup with frameworks other than Rails.
39
+ #
40
+ # @example Configure MarkMapper.
41
+ # MarkMapper.load!("/path/to/mark_mapper.yml")
42
+ #
43
+ # @param [ String ] path The path to the file.
44
+ # @param [ String, Symbol ] environment The environment to load.
45
+ #
46
+ # @since 0.0.1
47
+ def load!(path, environment = nil)
48
+ options = load_yaml(path, environment)
49
+ load_configuration(options) if options.present?
50
+ end
51
+
52
+ # From a hash of options, load all the configuration.
53
+ #
54
+ # @example Load the configuration.
55
+ # config.load_configuration(options)
56
+ #
57
+ # @param [ Hash ] options The configuration options.
58
+ #
59
+ # @since 0.0.1
60
+ def load_configuration(options)
61
+ options.each_pair do |key, value|
62
+ if settings.has_key? key.to_sym
63
+ settings[key.to_sym] = value
64
+ else
65
+ raise InvalidConfigurationOption, "Invalid configuration item: #{key}"
66
+ end
67
+ end
68
+ end
69
+
70
+ def env_name
71
+ return Rails.env if defined?(Rails)
72
+ ENV["RACK_ENV"] || ENV["MARKMAPPER_ENV"] || raise(Errors::NoEnvironment.new)
73
+ end
74
+
75
+ def load_yaml(path, environment = nil)
76
+ env = environment ? environment.to_s : env_name
77
+ YAML.load(ERB.new(File.new(path).read).result)[env]
78
+ end
79
+
80
+ def method_missing(name, *args)
81
+ attr = name.to_s
82
+ sym = attr.delete("=").to_sym
83
+ if attr.include?("=")
84
+ settings[sym] = args.first
85
+ else
86
+ settings[sym]
87
+ end
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,60 @@
1
+ # encoding: UTF-8
2
+ require 'uri'
3
+
4
+ module MarkMapper
5
+ module Connection
6
+ @@connection = nil
7
+ @@database = nil
8
+ @@database_name = nil
9
+ @@config = nil
10
+
11
+ # @api public
12
+ def connection
13
+ @@connection ||= application.connection
14
+ end
15
+
16
+ def connection?
17
+ !!connection
18
+ end
19
+
20
+ # @api public
21
+ def connection=(new_connection)
22
+ @@connection = new_connection
23
+ end
24
+
25
+ # @api public
26
+ def logger
27
+ MarkLogic.logger
28
+ end
29
+
30
+ def application=(new_app)
31
+ @@application = new_app
32
+ end
33
+
34
+ def application
35
+ @@application
36
+ end
37
+
38
+ # @api public
39
+ def database=(name)
40
+ @@database = nil
41
+ @@database_name = name
42
+ end
43
+
44
+ # @api public
45
+ def database
46
+ return nil if @@database_name.blank?
47
+
48
+ @@database ||= MarkLogic::Database.new(@@database_name, connection)
49
+ end
50
+
51
+ def config=(hash)
52
+ @@config = hash
53
+ end
54
+
55
+ def config
56
+ raise 'Set config before connecting. MarkMapper.config = {...}' unless defined?(@@config)
57
+ @@config
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,194 @@
1
+ # encoding: UTF-8
2
+ require 'set'
3
+ require 'mark_mapper/normalizers/criteria_hash_value'
4
+ require 'mark_mapper/normalizers/criteria_hash_key'
5
+
6
+ module MarkMapper
7
+ class CriteriaHash
8
+ # Private: The Hash that stores query criteria
9
+ attr_reader :source
10
+
11
+ # Private: The Hash that stores options
12
+ attr_reader :options
13
+
14
+ # Internal: Used to determine if criteria keys match simple id lookup.
15
+ SimpleIdQueryKeys = [:_id].to_set
16
+
17
+ # Internal: Used to determine if criteria keys match simple id and type
18
+ # lookup (for single collection inheritance).
19
+ SimpleIdAndTypeQueryKeys = [:_id, :_type].to_set
20
+
21
+ # Internal: Used to quickly check if it is possible that the
22
+ # criteria hash is simple.
23
+ SimpleQueryMaxSize = [SimpleIdQueryKeys.size, SimpleIdAndTypeQueryKeys.size].max
24
+
25
+ # Public
26
+ def initialize(hash={}, options={})
27
+ @source, @options = {}, options
28
+ hash.each { |key, value| self[key] = value }
29
+ end
30
+
31
+ def initialize_copy(original)
32
+ super
33
+ @options = @options.dup
34
+ @source = @source.dup
35
+ @source.each do |key, value|
36
+ self[key] = value.clone if value.duplicable?
37
+ end
38
+ end
39
+
40
+ # Public
41
+ def [](key)
42
+ @source[key]
43
+ end
44
+
45
+ # Public
46
+ # The contents of this make me sad...need to clean it up
47
+ def []=(key, value)
48
+ normalized_key = normalized_key(key)
49
+
50
+ if key.is_a?(SymbolOperator)
51
+ operator = :"$#{key.operator}"
52
+ normalized_value = normalized_value(normalized_key, operator, value)
53
+ @source[normalized_key] ||= {}
54
+ @source[normalized_key][operator] = normalized_value
55
+ else
56
+ if key == :conditions
57
+ value.each { |k, v| self[k] = v }
58
+ else
59
+ normalized_value = normalized_value(normalized_key, normalized_key, value)
60
+ @source[normalized_key] = normalized_value
61
+ end
62
+ end
63
+ end
64
+
65
+ # Public
66
+ def keys
67
+ @source.keys
68
+ end
69
+
70
+ # Public
71
+ def ==(other)
72
+ @source == other.source
73
+ end
74
+
75
+ # Public
76
+ def to_hash
77
+ @source
78
+ end
79
+
80
+ # Public
81
+ def merge(other)
82
+ self.class.new hash_merge(@source, other.source)
83
+ end
84
+
85
+ # Public
86
+ def merge!(other)
87
+ merge(other).to_hash.each do |key, value|
88
+ self[key] = value
89
+ end
90
+ self
91
+ end
92
+
93
+ # Public: The definition of simple is querying by only _id or _id and _type.
94
+ # If this is the case, you can use IdentityMap in library to not perform
95
+ # query and instead just return from map.
96
+ #
97
+ # Returns true or false
98
+ def simple?
99
+ return false if keys.size > SimpleQueryMaxSize
100
+ key_set = keys.to_set
101
+ key_set == SimpleIdQueryKeys || key_set == SimpleIdAndTypeQueryKeys
102
+ end
103
+
104
+ def object_id?(key)
105
+ object_ids.include?(key.to_sym)
106
+ end
107
+
108
+ # Private
109
+ def object_ids
110
+ @options[:object_ids] ||= []
111
+ end
112
+
113
+ # Private
114
+ def object_ids=(value)
115
+ raise ArgumentError unless value.is_a?(Array)
116
+ @options[:object_ids] = value.flatten
117
+ end
118
+
119
+ private
120
+
121
+ # Private
122
+ def hash_merge(oldhash, newhash)
123
+ merge_compound_or_clauses!(oldhash, newhash)
124
+ oldhash.merge(newhash) do |key, oldval, newval|
125
+ old_is_hash = oldval.instance_of? Hash
126
+ new_is_hash = newval.instance_of? Hash
127
+
128
+ if old_is_hash && new_is_hash
129
+ hash_merge(oldval, newval)
130
+ elsif old_is_hash
131
+ modifier_merge(oldval, newval)
132
+ elsif new_is_hash
133
+ modifier_merge(newval, oldval)
134
+ else
135
+ merge_values_into_array(oldval, newval)
136
+ end
137
+ end
138
+ end
139
+
140
+ def merge_compound_or_clauses!(oldhash, newhash)
141
+ old_or = oldhash[:$or]
142
+ new_or = newhash[:$or]
143
+ if old_or && new_or
144
+ oldhash[:$and] ||= []
145
+ oldhash[:$and] << {:$or => oldhash.delete(:$or)}
146
+ oldhash[:$and] << {:$or => newhash.delete(:$or)}
147
+ elsif new_or && oldhash[:$and]
148
+ if oldhash[:$and].any? {|v| v.key? :$or }
149
+ oldhash[:$and] << {:$or => newhash.delete(:$or)}
150
+ end
151
+ elsif old_or && newhash[:$and]
152
+ if newhash[:$and].any? {|v| v.key? :$or }
153
+ newhash[:$and] << {:$or => oldhash.delete(:$or)}
154
+ end
155
+ end
156
+ end
157
+
158
+ # Private
159
+ def modifier_merge(hash, value)
160
+ if modifier_key = hash.keys.detect { |k| MarkMapper.modifier?(k) }
161
+ hash[modifier_key].concat( Array(value) ).uniq
162
+ end
163
+ end
164
+
165
+ # Private
166
+ def merge_values_into_array(value, other_value)
167
+ Array(value).concat(Array(other_value)).uniq
168
+ end
169
+
170
+ # Private
171
+ def normalized_key(key)
172
+ key_normalizer.call(key)
173
+ end
174
+
175
+ # Private
176
+ def key_normalizer
177
+ @key_normalizer ||= @options.fetch(:key_normalizer) {
178
+ Normalizers::CriteriaHashKey.new
179
+ }
180
+ end
181
+
182
+ # Private
183
+ def normalized_value(parent_key, key, value)
184
+ value_normalizer.call(parent_key, key, value)
185
+ end
186
+
187
+ # Private
188
+ def value_normalizer
189
+ @value_normalizer ||= @options.fetch(:value_normalizer) {
190
+ Normalizers::CriteriaHashValue.new(self)
191
+ }
192
+ end
193
+ end
194
+ end