Module: CDQ

Extended by:
CDQ
Included in:
CDQ, CDQObject, CDQManagedObject, CDQManagedObject
Defined in:
motion/cdq/targeted_query.rb,
motion/cdq.rb,
motion/cdq/query.rb,
motion/cdq/model.rb,
motion/cdq/store.rb,
motion/cdq/object.rb,
motion/cdq/config.rb,
motion/cdq/context.rb,
motion/cdq/deprecation.rb,
motion/cdq/object_proxy.rb,
motion/cdq/collection_proxy.rb,
motion/cdq/partial_predicate.rb,
motion/cdq/relationship_query.rb

Overview

:nodoc:

Defined Under Namespace

Modules: Deprecation Classes: CDQCollectionProxy, CDQConfig, CDQContextManager, CDQModelManager, CDQObject, CDQObjectProxy, CDQPartialPredicate, CDQQuery, CDQRelationshipQuery, CDQStoreManager, CDQTargetedQuery

Instance Method Summary collapse

Instance Method Details

#cdq(obj = nil) ⇒ Object

The master method that will lift a variety of arguments into the CDQ ecosystem. What it returns depends on the type of the argument passed:

Class: Finds an entity with the same name as the class and returns a targeted query. (This is also used internally for dedicated models.)

cdq(Author).where(:attribute).eq(1).limit(3)

String: Finds an entity with the name provided in the string and returns a targeted query.

cdq('Author').where(:attribute).eq(1).limit(3)

Symbol: Returns an untargeted partial predicate. This is useful for nested queries, and for defining scopes.

Author.scope :singletons, cdq(:attribute).eq(1)
Author.where( cdq(:attribute).eq(1).or.eq(3) ).and(:name).ne("Roger")

CDQObject: returns the object itself (no-op).

NSManagedObject: wraps the object in a CDQObjectProxy, which permits cdq-style queries on the object's relationships.

emily_dickinson = Author.first
cdq(emily_dickinson).articles.where(:page_count).lt(5).array

Array: wraps the array in a CDQCollectionProxy, which lets you run queries relative to the members of the collection.

emily_dickinson = Author.first
edgar_allen_poe = Author.all[4]
charles_dickens = Author.all[7]
cdq([emily_dickinson, edgar_allen_poe, charles_dickens]).where(:avg_rating).eq(1)

46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'motion/cdq.rb', line 46

def cdq(obj = nil)
  obj ||= self

  @@base_object ||= CDQObject.new

  case obj
  when Class
    if obj.isSubclassOfClass(NSManagedObject)
      entities = NSDictionary.dictionaryWithDictionary(
        @@base_object.models.current.entitiesByName)
      entity_name = obj.name.split("::").last
      # NOTE attempt to look up the entity
      entity_description =
        entities[entity_name] ||
        entities[obj.ancestors[1].name]
      if entity_description.nil?
        raise "Cannot find an entity named #{obj.name}"
      end
      CDQTargetedQuery.new(entity_description, obj)
    else
      @@base_object
    end
  when String
    entities = NSDictionary.dictionaryWithDictionary(
      @@base_object.models.current.entitiesByName)
    entity_description = entities[obj]
    target_class = NSClassFromString(entity_description.managedObjectClassName)
    if entity_description.nil?
      raise "Cannot find an entity named #{obj}"
    end
    CDQTargetedQuery.new(entity_description, target_class)
  when NSEntityDescription
    entity_description = obj
    target_class = NSClassFromString(entity_description.managedObjectClassName)
    CDQTargetedQuery.new(entity_description, target_class)
  when Symbol
    CDQPartialPredicate.new(obj, CDQQuery.new)
  when CDQObject
    obj
  when NSManagedObject
    CDQObjectProxy.new(obj)
  when Array
    if obj.first.class.isSubclassOfClass(NSManagedObject)
      CDQCollectionProxy.new(obj, obj.first.entity)
    else
      @@base_object
    end
  else
    @@base_object
  end
end