Class: CDQManagedObject

Inherits:
CoreDataQueryManagedObjectBase
  • Object
show all
Extended by:
CDQ
Includes:
CDQ
Defined in:
motion/managed_object.rb

Overview

CDQ Extensions for your custom entity objects. This is mostly convenience and syntactic sugar – you can access every feature using the cdq(Class).method syntax, but this enables the nicer-looking and more convenient Class.method style. Any method availble on cdq(Class) is now available directly on Class.

If there is a conflict between a CDQ method and one of yours, or one of Core Data's, your code will always win. In that case you can get at the CDQ method by calling Class.cdq.method.

Examples:

MyEntity.where(:name).eq("John").limit(2)
MyEntity.first
MyEntity.create(name: "John")
MyEntity.sort_by(:title)[4]

class MyEntity < CDQManagedObject
  scope :last_week, where(:created_at).ge(date.delta(weeks: -2)).and.lt(date.delta(weeks: -1))
end

MyEntity.last_week.where(:created_by => john)

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CDQ

cdq

Class Method Details

.attribute_namesObject


84
85
86
# File 'motion/managed_object.rb', line 84

def attribute_names
  self.entity_description.attributesByName.keys
end

.cdq(obj = nil) ⇒ Object


88
89
90
91
92
93
94
# File 'motion/managed_object.rb', line 88

def cdq(obj = nil)
  if obj
    super(obj)
  else
    @cdq_object ||= super(nil)
  end
end

.cdq_initialized?Boolean

Returns:

  • (Boolean)

96
97
98
# File 'motion/managed_object.rb', line 96

def cdq_initialized?
  !@cdq_object.nil?
end

.destroy_allObject


73
74
75
76
77
# File 'motion/managed_object.rb', line 73

def destroy_all
  self.all.array.each do |instance|
    instance.destroy
  end
end

.destroy_all!Object


79
80
81
82
# File 'motion/managed_object.rb', line 79

def destroy_all!
  destroy_all
  cdq.save
end

.inherited(klass) ⇒ Object

:nodoc:


31
32
33
34
35
36
37
# File 'motion/managed_object.rb', line 31

def inherited(klass) #:nodoc:
  cdq(klass).entity_description.relationshipsByName.each do |name, rdesc|
    if rdesc.isToMany
      klass.defineRelationshipMethod(name)
    end
  end
end

.method_missing(name, *args, &block) ⇒ Object

Pass any unknown methods on to cdq.


61
62
63
# File 'motion/managed_object.rb', line 61

def method_missing(name, *args, &block)
  cdq.send(name, *args, &block)
end

.new(*args) ⇒ Object


55
56
57
# File 'motion/managed_object.rb', line 55

def new(*args)
  cdq.new(*args)
end

.respond_to?(name) ⇒ Boolean

Returns:

  • (Boolean)

65
66
67
68
69
70
71
# File 'motion/managed_object.rb', line 65

def respond_to?(name)
  if cdq_initialized?
    super(name) || cdq.respond_to?(name)
  else
    super(name)
  end
end

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

Creates a CDQ scope, but also defines a method on the class that returns the query directly.


42
43
44
45
46
47
48
49
50
51
52
53
# File 'motion/managed_object.rb', line 42

def scope(name, query = nil, &block)
  cdq.scope(name, query, &block)
  if query
    self.class.send(:define_method, name) do
      where(query)
    end
  else
    self.class.send(:define_method, name) do |*args|
      where(block.call(*args))
    end
  end
end

Instance Method Details

#attributesObject

Returns a hash of attributes for the object


114
115
116
117
118
119
120
# File 'motion/managed_object.rb', line 114

def attributes
  h = {}
  entity.attributesByName.each do |name, desc|
    h[name] = send name
  end
  h
end

#destroyObject

Register this object for destruction with the current context. Will not actually be removed until the context is saved.


105
106
107
# File 'motion/managed_object.rb', line 105

def destroy
  managedObjectContext.deleteObject(self)
end

#inspectObject


109
110
111
# File 'motion/managed_object.rb', line 109

def inspect
  description
end

#log(log_type = nil) ⇒ Object


122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'motion/managed_object.rb', line 122

def log(log_type = nil)
  out = "\nOID: "
  out << oid
  out << "\n"

  atts = entity.attributesByName
  rels = entity.relationshipsByName

  width = (atts.keys.map(&:length) + rels.keys.map(&:length)).max || 0

  atts.each do |name, desc|
    out << "  #{name.ljust(width)} : "
    out << send(name).inspect[0,95 - width]
    out << "\n"
  end

  rels.each do |name, desc|
    rel = CDQRelationshipQuery.new(self, name, nil, context: managedObjectContext)
    if desc.isToMany
      out << "  #{name.ljust(width)} : "
      out << rel.count.to_s
      out << ' (count)'
    else
      out << "  #{name.ljust(width)} : "
      out << (rel.first && rel.first.oid || "nil")
    end
  end
  out << "\n"

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

#oidObject


173
174
175
# File 'motion/managed_object.rb', line 173

def oid
  objectID.URIRepresentation.absoluteString.inspect
end

#ordered_set?(name) ⇒ Boolean

Returns:

  • (Boolean)

158
159
160
161
162
163
# File 'motion/managed_object.rb', line 158

def ordered_set?(name)
  # isOrdered is returning 0/1 instead of documented BOOL
  ordered = entity.relationshipsByName[name].isOrdered
  return true if ordered == true || ordered == 1
  return false if ordered == false || ordered == 0
end

#set_to_extend(name) ⇒ Object


165
166
167
168
169
170
171
# File 'motion/managed_object.rb', line 165

def set_to_extend(name)
  if ordered_set?(name)
    mutableOrderedSetValueForKey(name)
  else
    mutableSetValueForKey(name)
  end
end