Module: Graphoid::MongoidDriver
- Defined in:
- lib/graphoid/drivers/mongoid.rb
Class Method Summary collapse
- .belongs_to?(type) ⇒ Boolean
- .class_of(relation) ⇒ Object
- .eager_load(selection, model) ⇒ Object
- .embedded_in?(type) ⇒ Boolean
- .embeds_many?(type) ⇒ Boolean
- .embeds_one?(type) ⇒ Boolean
- .execute_and(scope, parsed) ⇒ Object
- .execute_or(scope, list) ⇒ Object
- .fields_of(model) ⇒ Object
- .has_and_belongs_to_many?(type) ⇒ Boolean
- .has_many?(type) ⇒ Boolean
- .has_one?(type) ⇒ Boolean
- .inverse_name_of(relation) ⇒ Object
- .parse(attribute, value, operator) ⇒ Object
- .relate_embedded(scope, relation, filters) ⇒ Object
- .relate_many(scope, relation, value, operator) ⇒ Object
- .relate_one(scope, relation, value) ⇒ Object
- .relation_type(relation) ⇒ Object
- .relations_of(model) ⇒ Object
- .skip(result, skip) ⇒ Object
- .through?(type) ⇒ Boolean
- .types_map ⇒ Object
Class Method Details
.belongs_to?(type) ⇒ Boolean
16 17 18 |
# File 'lib/graphoid/drivers/mongoid.rb', line 16 def belongs_to?(type) type == Mongoid::Association::Referenced::BelongsTo end |
.class_of(relation) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/graphoid/drivers/mongoid.rb', line 57 def class_of(relation) { Mongoid::Relations::Referenced::ManyToMany => ManyToMany, Mongoid::Relations::Referenced::Many => HasMany, Mongoid::Relations::Referenced::One => HasOne, Mongoid::Relations::Referenced::In => BelongsTo, Mongoid::Relations::Embedded::Many => EmbedsMany, Mongoid::Relations::Embedded::One => EmbedsOne, Mongoid::Relations::Embedded::In => Relation }[relation.relation] || Relation end |
.eager_load(selection, model) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/graphoid/drivers/mongoid.rb', line 89 def eager_load(selection, model) referenced_relations = [ Mongoid::Relations::Referenced::ManyToMany, Mongoid::Relations::Referenced::Many, Mongoid::Relations::Referenced::One, Mongoid::Relations::Referenced::In ] properties = Graphoid::Queries::Processor.children_of(selection) inclusions = Utils.symbolize(properties) Relation.relations_of(model).each do |name, relation| name = relation.name next if inclusions.exclude?(name) || referenced_relations.exclude?(association.relation) subselection = properties[name.to_s.camelize(:lower)] children = Utils.symbolize(Graphoid::Queries::Processor.children_of(subselection)) relations = relation.class_name.constantize.reflections.values.map(&:name) if (relations & children).empty? model = model.includes(name) else model = model.includes(name, with: -> (instance) { Graphoid::Queries::Processor.eager_load(subselection, instance) }) end end model end |
.embedded_in?(type) ⇒ Boolean
32 33 34 |
# File 'lib/graphoid/drivers/mongoid.rb', line 32 def (type) type == Mongoid::Association::Embedded::EmbeddedIn end |
.embeds_many?(type) ⇒ Boolean
28 29 30 |
# File 'lib/graphoid/drivers/mongoid.rb', line 28 def (type) type == Mongoid::Association::Embedded::EmbedsMany end |
.embeds_one?(type) ⇒ Boolean
24 25 26 |
# File 'lib/graphoid/drivers/mongoid.rb', line 24 def (type) type == Mongoid::Association::Embedded::EmbedsOne end |
.execute_and(scope, parsed) ⇒ Object
118 119 120 |
# File 'lib/graphoid/drivers/mongoid.rb', line 118 def execute_and(scope, parsed) scope.and(parsed) end |
.execute_or(scope, list) ⇒ Object
122 123 124 125 126 127 |
# File 'lib/graphoid/drivers/mongoid.rb', line 122 def execute_or(scope, list) list.map! do |object| Graphoid::Queries::Processor.execute(scope, object).selector end scope.any_of(list) end |
.fields_of(model) ⇒ Object
73 74 75 |
# File 'lib/graphoid/drivers/mongoid.rb', line 73 def fields_of(model) model.respond_to?(:fields) ? model.fields.values : [] end |
.has_and_belongs_to_many?(type) ⇒ Boolean
8 9 10 |
# File 'lib/graphoid/drivers/mongoid.rb', line 8 def has_and_belongs_to_many?(type) type == Mongoid::Association::Referenced::HasAndBelongsToMany end |
.has_many?(type) ⇒ Boolean
12 13 14 |
# File 'lib/graphoid/drivers/mongoid.rb', line 12 def has_many?(type) type == Mongoid::Association::Referenced::HasMany end |
.has_one?(type) ⇒ Boolean
20 21 22 |
# File 'lib/graphoid/drivers/mongoid.rb', line 20 def has_one?(type) type == Mongoid::Association::Referenced::HasOne end |
.inverse_name_of(relation) ⇒ Object
69 70 71 |
# File 'lib/graphoid/drivers/mongoid.rb', line 69 def inverse_name_of(relation) relation.inverse_of end |
.parse(attribute, value, operator) ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/graphoid/drivers/mongoid.rb', line 129 def parse(attribute, value, operator) field = attribute.name parsed = {} case operator when "gt", "gte", "lt", "lte", "in", "nin" parsed[field.to_sym.send(operator)] = value when "regex" parsed[field.to_sym] = Regexp.new(value.to_s, Regexp::IGNORECASE) when "contains" parsed[field.to_sym] = Regexp.new(Regexp.quote(value.to_s), Regexp::IGNORECASE) when "not" if value.present? && !value.is_a?(Numeric) parsed[field.to_sym.send(operator)] = Regexp.new(Regexp.quote(value.to_s), Regexp::IGNORECASE) else parsed[field.to_sym.send(:nin)] = [value] end else parsed[field.to_sym] = value end parsed end |
.relate_embedded(scope, relation, filters) ⇒ Object
151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/graphoid/drivers/mongoid.rb', line 151 def (scope, relation, filters) # TODO: this way of fetching this is not recursive as the regular fields # because the structure of the query is embeeded.field = value # we need more brain cells on this problem because it does not allow # to filter things using OR/AND parsed = {} filters.each do |key, value| operation = Operation.new(scope, key, value) attribute = OpenStruct.new(name: "#{relation.name}.#{operation.operand}") obj = parse(attribute, value, operation.operator).first parsed[obj[0]] = obj[1] end parsed end |
.relate_many(scope, relation, value, operator) ⇒ Object
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/graphoid/drivers/mongoid.rb', line 185 def relate_many(scope, relation, value, operator) field_name = relation.inverse_name || scope.name.underscore target = Graphoid::Queries::Processor.execute(relation.klass, value).to_a if relation. # TODO: not implemented at all. end if relation.many_to_many? field_name = field_name.to_s.singularize + "_ids" ids = target.map(&(field_name.to_sym)) ids.flatten!.uniq! else field_name = field_name.to_s + "_id" ids = target.map(&(field_name.to_sym)) end parsed = {} if operator == "none" parsed[:id.nin] = ids elsif operator == "some" parsed[:id.in] = ids elsif operator == "every" # missing implementation end parsed end |
.relate_one(scope, relation, value) ⇒ Object
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/graphoid/drivers/mongoid.rb', line 166 def relate_one(scope, relation, value) field = relation.name parsed = {} if relation. parsed = (scope, relation, value) end if relation.belongs_to? parsed = relation.exec(scope, value) end if relation.has_one? parsed = relation.exec(scope, value) end parsed end |
.relation_type(relation) ⇒ Object
85 86 87 |
# File 'lib/graphoid/drivers/mongoid.rb', line 85 def relation_type(relation) relation.relation end |
.relations_of(model) ⇒ Object
77 78 79 |
# File 'lib/graphoid/drivers/mongoid.rb', line 77 def relations_of(model) model.relations end |
.skip(result, skip) ⇒ Object
81 82 83 |
# File 'lib/graphoid/drivers/mongoid.rb', line 81 def skip(result, skip) result.skip(skip) end |
.through?(type) ⇒ Boolean
4 5 6 |
# File 'lib/graphoid/drivers/mongoid.rb', line 4 def through?(type) false end |
.types_map ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/graphoid/drivers/mongoid.rb', line 36 def types_map { BSON::ObjectId => GraphQL::Types::ID, Mongoid::Boolean => GraphQL::Types::Boolean, #Graphoid::Upload => ApolloUploadServer::Upload, Boolean => GraphQL::Types::Boolean, Float => GraphQL::Types::Float, Integer => GraphQL::Types::Int, String => GraphQL::Types::String, Object => GraphQL::Types::String, Symbol => GraphQL::Types::String, DateTime => Graphoid::Scalars::DateTime, Time => Graphoid::Scalars::DateTime, Date => Graphoid::Scalars::DateTime, Array => Graphoid::Scalars::Array, Hash => Graphoid::Scalars::Hash } end |