cdq 0.1.8 → 0.1.9

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- ZTVmOGNjMzAwMmQzYTdjMDMyYjViYWU2MGEzYTc1MTdiNGJiMzM4OA==
4
+ YTZiZDlhMzI5ODBiZWVmYjYyZWViNDA4YzhhODMyZDEwOTRlOWUyYQ==
5
5
  data.tar.gz: !binary |-
6
- ZjRmYzhkNDk1MmI1ZWFiODg1YTkzNmUwMTZmNGU5NTVhYzFiOWE3YQ==
6
+ MzBmOGI2MGQxY2RjYzFlZGNkMDdlNzg4NDc5MTAxY2QxZWIwODkzOA==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- NDliNjIxOWQxMTMzODA2NTcxMmQxMTQ0Zjg3NGI2OTA4YTczZTkwZTdmN2Rm
10
- MzViMGM3ZDRhZTU1MDliN2E3ZDQ4YTJiMmRhNzZmNjlhOWM2MTI3NzJlYmIz
11
- ZDRkNzEzMDhiNTZjZTg3MGFhMmE4MGU3MjRlMDNlY2FjZTgzNzk=
9
+ MGUxZDhiODMyYjE5NjlkMzc1YTJjMTc0Mzg2MDc4NjhhNDAxOTk5OTRhMzg3
10
+ MWU0NGVjODUyZDAzODNlZDlkYTllNTRhMjBmYzQ0Y2Q5ODBjYWU5MDFhNzVk
11
+ MzEyMGFiNDQ2ODAyNjhiODBkZWI4YmUxNWRmOWNhYzhkYzBmZmU=
12
12
  data.tar.gz: !binary |-
13
- NGZiYTcyYjlkNjU0ZWZjZDk1Y2MyODBhN2JlNDg5ZDc3ZDU0Y2I4ZjgwNjUx
14
- NTM1MmMwNTlmZTNlMWE1ZGIyNWFmNjI3YTVmNjk5NGI0NDFlZWVlNzc4MmI1
15
- YjQ5ZDVmMzI0MTUzNTdhOTdlZjMxNjQ5OGM2NDIwOTJlMDczNDc=
13
+ ZjUwMmNlMzdjM2VkYjU2MWQyMzk1ZmY1ZTRiNWJiZDllYTZiZjJmM2Y0MDE1
14
+ NmFlMTIyZTU4M2I2ZGY4NDljOTY2NTk4MWU4NzRmZGJjMmI4NjAxNDU4YjU5
15
+ NDRmNDdlNDZkZDRmMjk1Mzc0OGNmMzZjZWE3OTY4MmM5M2Q5NzY=
@@ -1,4 +1,5 @@
1
1
 
2
+ require 'fileutils'
2
3
 
3
4
  module CDQ
4
5
  class Generator
@@ -57,7 +58,7 @@ module CDQ
57
58
  puts " Using existing directory: #{@in_app_path}"
58
59
  else
59
60
  puts " \u0394 Creating directory: #{@in_app_path}"
60
- Dir.mkdir(@in_app_path) unless @dry_run
61
+ FileUtils.mkdir_p(@in_app_path) unless @dry_run
61
62
  end
62
63
 
63
64
  results = load_and_parse_erb(template_file_path_and_name)
@@ -1,4 +1,4 @@
1
1
 
2
2
  module CDQ
3
- VERSION = '0.1.8'
3
+ VERSION = '0.1.9'
4
4
  end
@@ -51,7 +51,9 @@ module CDQ
51
51
  case obj
52
52
  when Class
53
53
  if obj.isSubclassOfClass(NSManagedObject)
54
- entity_description = @@base_object.models.current.entitiesByName[obj.name]
54
+ entity_description =
55
+ @@base_object.models.current.entitiesByName[obj.name] ||
56
+ @@base_object.models.current.entitiesByName[obj.ancestors[1].name]
55
57
  if entity_description.nil?
56
58
  raise "Cannot find an entity named #{obj.name}"
57
59
  end
@@ -9,7 +9,8 @@ module CDQ
9
9
  # root of your resources directory. It supports the following top-level keys:
10
10
  #
11
11
  # [name] The root name for both database and model
12
- # [database_name] The root name for the database file (relative to the documents directory)
12
+ # [database_dir] The root name for the database directory (NSDocumentDirectory or NSApplicationSupportDirectory)
13
+ # [database_name] The root name for the database file (relative to the database_dir)
13
14
  # [model_name] The root name for the model file (relative to the bundle directory)
14
15
  #
15
16
  # Using the config file is not necessary. If you do not include it, the bundle display name
@@ -21,7 +22,7 @@ module CDQ
21
22
  #
22
23
  class CDQConfig
23
24
 
24
- attr_reader :config_file, :database_name, :model_name, :name
25
+ attr_reader :config_file, :database_name, :database_dir, :model_name, :name
25
26
 
26
27
  def initialize(config_file)
27
28
  case config_file
@@ -39,12 +40,13 @@ module CDQ
39
40
  end
40
41
 
41
42
  @name = h['name'] || h[:name] || NSBundle.mainBundle.objectForInfoDictionaryKey("CFBundleDisplayName")
43
+ @database_dir = search_directory_for h['database_dir'] || h[:database_dir]
42
44
  @database_name = h['database_name'] || h[:database_name] || name
43
45
  @model_name = h['model_name'] || h[:model_name] || name
44
46
  end
45
47
 
46
48
  def database_url
47
- dir = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, true).last
49
+ dir = NSSearchPathForDirectoriesInDomains(database_dir, NSUserDomainMask, true).last
48
50
  path = File.join(dir, database_name + '.sqlite')
49
51
  NSURL.fileURLWithPath(path)
50
52
  end
@@ -60,6 +62,20 @@ module CDQ
60
62
  new(cf_file)
61
63
  end
62
64
  end
65
+
66
+
67
+ private
68
+
69
+ def search_directory_for dir_name
70
+ supported_dirs = {
71
+ "NSDocumentDirectory" => NSDocumentDirectory,
72
+ :NSDocumentDirectory => NSDocumentDirectory,
73
+ "NSApplicationSupportDirectory" => NSApplicationSupportDirectory,
74
+ :NSApplicationSupportDirectory => NSApplicationSupportDirectory,
75
+ }
76
+ supported_dirs[dir_name] || NSDocumentDirectory
77
+ end
78
+
63
79
  end
64
80
 
65
81
  end
@@ -6,8 +6,15 @@ module CDQ
6
6
  def initialize(owner, name, set = nil, opts = {})
7
7
  @owner = owner
8
8
  @relationship_name = name
9
- @set = set || @owner.send(name)
9
+ @set = set
10
10
  relationship = owner.entity.relationshipsByName[name]
11
+ if relationship.isToMany
12
+ if @owner.ordered_set?(name)
13
+ @set ||= @owner.mutableOrderedSetValueForKey(name)
14
+ else
15
+ @set ||= @owner.mutableSetValueForKey(name)
16
+ end
17
+ end
11
18
  @inverse_rel = relationship.inverseRelationship
12
19
  entity_description = relationship.destinationEntity
13
20
  target_class = constantize(entity_description.managedObjectClassName)
@@ -30,15 +37,16 @@ module CDQ
30
37
  # Add an existing object to the relationship
31
38
  #
32
39
  def add(obj)
33
- if @inverse_rel.isToMany
34
- obj.send(@inverse_rel.name).addObject(@owner)
35
- else
36
- obj.send("#{@inverse_rel.name}=", @owner)
37
- end
38
40
  @set.addObject obj
39
41
  end
40
42
  alias_method :<<, :add
41
43
 
44
+ # Remove objects from the relationship
45
+ #
46
+ def remove(obj)
47
+ @set.removeObject obj
48
+ end
49
+
42
50
  def self.extend_set(set, owner, name)
43
51
  set.extend SetExt
44
52
  set.extend Enumerable
@@ -87,6 +95,10 @@ module CDQ
87
95
  @__query__.new(opts)
88
96
  end
89
97
 
98
+ def remove(opts = {})
99
+ @__query__.remove(opts)
100
+ end
101
+
90
102
  def where(*args)
91
103
  @__query__.where(*args)
92
104
  end
@@ -33,6 +33,7 @@ module CDQ
33
33
  options = { NSMigratePersistentStoresAutomaticallyOption => true,
34
34
  NSInferMappingModelAutomaticallyOption => true }
35
35
  url = @config.database_url
36
+ mkdir_p File.dirname(url.path)
36
37
  store = coordinator.addPersistentStoreWithType(NSSQLiteStoreType,
37
38
  configuration:nil,
38
39
  URL:url,
@@ -47,6 +48,17 @@ module CDQ
47
48
  coordinator
48
49
  end
49
50
  end
51
+
52
+ def mkdir_p dir
53
+ error = Pointer.new(:object)
54
+ m = NSFileManager.defaultManager
55
+ r = m.createDirectoryAtPath dir, withIntermediateDirectories:true, attributes:nil, error:error
56
+ unless r
57
+ NSLog "#{error[0].localizedDescription}"
58
+ raise error[0].localizedDescription
59
+ end
60
+ end
61
+
50
62
  end
51
63
 
52
64
  end
@@ -83,8 +83,8 @@ module CDQ #:nodoc:
83
83
  #
84
84
  # Causes execution.
85
85
  #
86
- def each(&block)
87
- array.each(&block)
86
+ def each(*args, &block)
87
+ array.each(*args, &block)
88
88
  end
89
89
 
90
90
  # Returns the fully-contstructed fetch request, which can be executed outside of CDQ.
@@ -159,10 +159,7 @@ module CDQ #:nodoc:
159
159
  line = " \n- - - - - - - - - - - | - - - - - - - - - - | - - - - - - - - - - - - - - - |"
160
160
  out << line
161
161
 
162
- abn = entity_description.attributesByName
163
- rbn = entity_description.relationshipsByName
164
-
165
- abn.each do |name, desc|
162
+ entity_description.attributesByName.each do |name, desc|
166
163
  out << " \n #{name.ljust(21)}|"
167
164
  out << " #{desc.attributeValueClassName.ljust(20)}|"
168
165
  out << " #{desc.defaultValue.to_s.ljust(30)}|"
@@ -172,32 +169,7 @@ module CDQ #:nodoc:
172
169
  out << "\n\n"
173
170
 
174
171
  self.each do |o|
175
- out << "OID: "
176
- out << oid(o)
177
- out << "\n"
178
-
179
- awidth = abn.keys.map(&:length).max
180
- rwidth = rbn.keys.map(&:length).max
181
- width = [awidth, rwidth].max
182
-
183
- abn.each do |name, desc|
184
- out << " #{name.ljust(width)} : "
185
- out << o.send(name).inspect[0,95 - width]
186
- out << "\n"
187
- end
188
- rbn.each do |name, desc|
189
- rel = CDQRelationshipQuery.new(o, name)
190
- if desc.isToMany
191
- out << " #{name.ljust(width)} : "
192
- out << rel.count.to_s
193
- out << ' (count)'
194
- else
195
- out << " #{name.ljust(width)} : "
196
- out << oid(rel.first)
197
- end
198
- out << "\n"
199
- end
200
- out << "\n"
172
+ out << o.log(:string)
201
173
  end
202
174
 
203
175
  if log_type == :string
@@ -205,15 +177,12 @@ module CDQ #:nodoc:
205
177
  else
206
178
  NSLog out
207
179
  end
208
-
209
- rescue Exception => e
210
- p e
211
180
  end
212
181
 
213
182
  private
214
183
 
215
184
  def oid(obj)
216
- obj ? obj.objectID.URIRepresentation.absoluteString.inspect : "nil"
185
+ obj ? obj.oid : "nil"
217
186
  end
218
187
 
219
188
  def named_scopes
@@ -36,12 +36,6 @@ class CDQManagedObject < CoreDataQueryManagedObjectBase
36
36
  end
37
37
  end
38
38
 
39
- # Shortcut to look up the entity description for this class
40
- #
41
- def entity_description
42
- cdq.models.current.entitiesByName[name]
43
- end
44
-
45
39
  # Creates a CDQ scope, but also defines a method on the class that returns the
46
40
  # query directly.
47
41
  #
@@ -68,10 +62,21 @@ class CDQManagedObject < CoreDataQueryManagedObjectBase
68
62
  cdq.send(name, *args, &block)
69
63
  end
70
64
 
71
- def responds_to?(name)
65
+ def respond_to?(name)
72
66
  super || cdq.respond_to?(name)
73
67
  end
74
68
 
69
+ def destroy_all
70
+ self.all.array.each do |instance|
71
+ instance.destroy
72
+ end
73
+ end
74
+
75
+ def destroy_all!
76
+ destroy_all
77
+ cdq.save
78
+ end
79
+
75
80
  end
76
81
 
77
82
  # Register this object for destruction with the current context. Will not
@@ -85,14 +90,70 @@ class CDQManagedObject < CoreDataQueryManagedObjectBase
85
90
  description
86
91
  end
87
92
 
93
+ def log(log_type = nil)
94
+ out = "\nOID: "
95
+ out << oid
96
+ out << "\n"
97
+
98
+ atts = entity.attributesByName
99
+ rels = entity.relationshipsByName
100
+
101
+ width = (atts.keys.map(&:length) + rels.keys.map(&:length)).max || 0
102
+
103
+ atts.each do |name, desc|
104
+ out << " #{name.ljust(width)} : "
105
+ out << send(name).inspect[0,95 - width]
106
+ out << "\n"
107
+ end
108
+
109
+ rels.each do |name, desc|
110
+ rel = CDQRelationshipQuery.new(self, name, nil, context: managedObjectContext)
111
+ if desc.isToMany
112
+ out << " #{name.ljust(width)} : "
113
+ out << rel.count.to_s
114
+ out << ' (count)'
115
+ else
116
+ out << " #{name.ljust(width)} : "
117
+ out << (rel.first && rel.first.oid || "nil")
118
+ end
119
+ end
120
+ out << "\n"
121
+
122
+ if log_type == :string
123
+ out
124
+ else
125
+ NSLog out
126
+ end
127
+ end
128
+
129
+ def ordered_set?(name)
130
+ # isOrdered is returning 0/1 instead of documented BOOL
131
+ ordered = entity.relationshipsByName[name].isOrdered
132
+ return true if ordered == true || ordered == 1
133
+ return false if ordered == false || ordered == 0
134
+ end
135
+
136
+ def set_to_extend(name)
137
+ if ordered_set?(name)
138
+ mutableOrderedSetValueForKey(name)
139
+ else
140
+ mutableSetValueForKey(name)
141
+ end
142
+ end
143
+
144
+ def oid
145
+ objectID.URIRepresentation.absoluteString.inspect
146
+ end
147
+
88
148
  protected
89
149
 
90
150
  # Called from method that's dynamically added from
91
151
  # +[CoreDataManagedObjectBase defineRelationshipMethod:]
92
152
  def relationshipByName(name)
93
153
  willAccessValueForKey(name)
94
- set = CDQRelationshipQuery.extend_set(primitiveValueForKey(name), self, name)
154
+ set = CDQRelationshipQuery.extend_set(set_to_extend(name), self, name)
95
155
  didAccessValueForKey(name)
96
156
  set
97
157
  end
158
+
98
159
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cdq
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.8
4
+ version: 0.1.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - infinitered
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-03-11 00:00:00.000000000 Z
12
+ date: 2014-06-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: ruby-xcdm