Class: CDQ::CDQTargetedQuery

Inherits:
CDQQuery show all
Includes:
Enumerable
Defined in:
motion/cdq.rb,
motion/cdq/targeted_query.rb

Direct Known Subclasses

CDQCollectionProxy, CDQRelationshipQuery

Instance Attribute Summary collapse

Attributes inherited from CDQQuery

#predicate, #sort_descriptors

Instance Method Summary collapse

Methods inherited from CDQQuery

#and, #limit, #offset, #or, #sort_by

Methods inherited from CDQObject

#background, #contexts, #find, #models, #reset!, #save, #setup, #stores

Methods included from CDQ

#cdq

Constructor Details

#initialize(entity_description, target_class = nil, opts = {}) ⇒ CDQTargetedQuery

Create a new CDQTargetedContext. Takes an entity description, an optional implementation class, and a hash of options that will be passed to the CDQQuery constructor.


14
15
16
17
18
19
20
21
# File 'motion/cdq/targeted_query.rb', line 14

def initialize(entity_description, target_class = nil, opts = {})
  @entity_description = entity_description
  @target_class = target_class ||
    NSClassFromString(entity_description.managedObjectClassName) ||
    CDQManagedObject
  @context = opts.delete(:context)
  super(opts)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

Any unknown method will be checked against the list of named scopes.


146
147
148
149
150
151
152
153
154
155
156
# File 'motion/cdq/targeted_query.rb', line 146

def method_missing(name, *args)
  scope = named_scopes[name]
  case scope
  when CDQQuery
    where(scope)
  when Proc
    where(scope.call(*args))
  else
    super(name, *args)
  end
end

Instance Attribute Details

#entity_descriptionObject (readonly)

Returns the value of attribute entity_description


8
9
10
# File 'motion/cdq/targeted_query.rb', line 8

def entity_description
  @entity_description
end

Instance Method Details

#[](index, length = nil) ⇒ Object

Fetch a single entity from the query by index. If the optional length parameter is supplied, fetch a range of length length starting at index

Causes execution.


75
76
77
78
79
80
81
# File 'motion/cdq/targeted_query.rb', line 75

def [](index, length = nil)
  if length
    offset(index).limit(length).array
  else
    offset(index).first
  end
end

#allObject

Convenience method for referring to all matching entities. No-op. You must still call array or another executing method


57
58
59
# File 'motion/cdq/targeted_query.rb', line 57

def all
  self
end

#arrayObject Also known as: to_a

Return all matching entities.

Causes execution.


46
47
48
49
50
51
# File 'motion/cdq/targeted_query.rb', line 46

def array
  raise("No context has been set.  Probably need to run cdq.setup") unless context
  with_error_object([]) do |error|
    context.executeFetchRequest(fetch_request, error:error)
  end
end

#contextObject

The current context, taken from the environment or overriden by in_context


25
26
27
# File 'motion/cdq/targeted_query.rb', line 25

def context
  @context || contexts.current
end

#countObject Also known as: length, size

Return the number of matching entities.

Causes execution.


33
34
35
36
37
38
# File 'motion/cdq/targeted_query.rb', line 33

def count
  raise("No context has been set.  Probably need to run cdq.setup") unless context
  with_error_object(0) do |error|
    context.countForFetchRequest(fetch_request, error:error)
  end
end

#create(*args) ⇒ Object

Create a new entity in the current context. Accepts a hash of attributes that will be assigned to the newly-created entity. Does not save the context.


113
114
115
# File 'motion/cdq/targeted_query.rb', line 113

def create(*args)
  new(*args)
end

#each(*args, &block) ⇒ Object

Iterate over each entity matched by the query. You can also use any method from the Enumerable module in the standard library that does not depend on ordering.

Causes execution.


88
89
90
# File 'motion/cdq/targeted_query.rb', line 88

def each(*args, &block)
  array.each(*args, &block)
end

#fetch_requestObject

Returns the fully-contstructed fetch request, which can be executed outside of CDQ.


94
95
96
97
98
99
# File 'motion/cdq/targeted_query.rb', line 94

def fetch_request
  super.tap do |req|
    req.entity = @entity_description
    req.predicate ||= NSPredicate.predicateWithValue(true)
  end
end

#firstObject

Return the first entity matching the query.

Causes execution.


65
66
67
# File 'motion/cdq/targeted_query.rb', line 65

def first
  limit(1).array.first
end

#in_context(context) ⇒ Object

Override the context in which to perform this query. This forever forces the specified context for this particular query, so if you save the it for later use (such as defining a scope) bear in mind that changes in the default context will have no effect when running this.


140
141
142
# File 'motion/cdq/targeted_query.rb', line 140

def in_context(context)
  clone(context: context)
end

#log(log_type = nil) ⇒ Object


158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'motion/cdq/targeted_query.rb', line 158

def log(log_type = nil)
  out = "\n\n                              ATTRIBUTES"
  out <<   " \n Name                 | type                | default                       |"
  line =   " \n- - - - - - - - - - - | - - - - - - - - - - | - - - - - - - - - - - - - - - |"
  out << line

  entity_description.attributesByName.each do |name, desc|
    out << " \n #{name.ljust(21)}|"
    out << " #{desc.attributeValueClassName.ljust(20)}|"
    out << " #{desc.defaultValue.to_s.ljust(30)}|"
  end

  out << line
  out << "\n\n"

  self.each do |o|
    out << o.log(:string)
  end

  if log_type == :string
    out
  else
    NSLog out
  end
end

#new(opts = {}) ⇒ Object

Create a new entity in the current context. Accepts a hash of attributes that will be assigned to the newly-created entity. Does not save the context.


104
105
106
107
108
# File 'motion/cdq/targeted_query.rb', line 104

def new(opts = {})
  @target_class.alloc.initWithEntity(@entity_description, insertIntoManagedObjectContext: context).tap do |entity|
    opts.each { |k, v| entity.send("#{k}=", v) }
  end
end

#scope(name, query = nil, &block) ⇒ Object

Create a named scope. The query is any valid CDQ query.

Example:

cdq('Author').scope(:first_published, cdq(:published).eq(true).sort_by(:published_at).limit(1))

cdq('Author').first_published.first => #<Author>


125
126
127
128
129
130
131
132
133
# File 'motion/cdq/targeted_query.rb', line 125

def scope(name, query = nil, &block)
  if query.nil? && block_given?
    named_scopes[name] = block
  elsif query
    named_scopes[name] = query
  else
    raise ArgumentError.new("You must supply a query OR a block that returns a query to scope")
  end
end