dread 0.0.5 → 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1e06797613f10604b7ea856d50664648ae1eb999
4
- data.tar.gz: 2c377cdc0bd7c79c7db3c765c7e511aa0f48bfff
3
+ metadata.gz: 34cd6aee3f85a657ebabe35bc350cbf3ccc2f95d
4
+ data.tar.gz: e72c6897aeec2fb15e4209c322ab0778151a9778
5
5
  SHA512:
6
- metadata.gz: 8a031041bb4c6bf978d29071140325d4a83c7f67c77b7a734fbd48639a2c36a20ff2f10e7aa0cdeffb824b7118796772bcf3d01c9da11deaebfd3f3501fbdffc
7
- data.tar.gz: 11e389c525b76b70be42940f196af19db6b7b7c1460f3dda6cb12cbdc237b6bfbe2b933c526ed66b6ec7e4f3315d76b4375ab5f5e9dd04ab393d2927800c6a2e
6
+ metadata.gz: d2f0d84dfc7d199d42cadc2e1f028f8c3afacc6d4ae5c1d7b0fc0ee3c3ca04af8a444cc4ec5263b9b5c442b1cbe5e75b0c5cbe947523ef8e84dc1ceb943b51a6
7
+ data.tar.gz: c498def53e5c72d80f86873b3dde2b033d4c00c08771bf8e8a4dc35123a496bfe917c997217ca35b6e102f55439e744cf8607e8ca7e8ae5f10bc098241c2bd7a
data/README.md CHANGED
@@ -1,3 +1,4 @@
1
1
  # Dread
2
2
 
3
- [![Build Status](https://travis-ci.org/DamirSvrtan/dread.svg?branch=master)](https://travis-ci.org/DamirSvrtan/dread)
3
+ [![Build Status](https://travis-ci.org/DamirSvrtan/dread.svg?branch=master)](https://travis-ci.org/DamirSvrtan/dread)
4
+ [![Code Climate](https://codeclimate.com/github/DamirSvrtan/dread/badges/gpa.svg)](https://codeclimate.com/github/DamirSvrtan/dread)
data/lib/dread/graph.rb CHANGED
@@ -6,23 +6,15 @@ module Dread
6
6
  attr_reader :clazz, :dependable_collection
7
7
 
8
8
  def initialize(clazz_data, pluralized=false)
9
- set_and_verify_clazz(clazz_data)
9
+ set_and_verify_clazz_and_relation(clazz_data)
10
10
  @pluralized = pluralized
11
+ @@tracker ||= []
11
12
  end
12
13
 
13
- # { user: { tweets: { comments: {} }, comments: {}, account_setting: {} } }
14
+ # { user: { tweets: { comments: {} }, comments: {}, setting: {} } }
14
15
  def dependable_collection
15
- @dependable_collection ||= { relation_name.to_sym => Hash.new.tap do |relation_hash|
16
- @clazz.reflections.each do |assoc_name, assoc_data|
17
- if assoc_data.options[:dependent] == :delete
18
- relation_hash[assoc_name] = {}
19
- elsif assoc_data.options[:dependent] == :destroy
20
- relation_hash.merge!(
21
- Graph.new(assoc_data, assoc_data.macro == :has_many).dependable_collection)
22
- end
23
- end
24
- end
25
- }
16
+ @dependable_collection ||=
17
+ { @relation.to_sym => collect_dependables }
26
18
  end
27
19
 
28
20
  def draw(output='console_output')
@@ -34,31 +26,52 @@ module Dread
34
26
 
35
27
  private
36
28
 
37
- def set_and_verify_clazz(clazz_data)
38
- clazz_name = ClazzName(clazz_data)
29
+ def track!(reflection)
30
+ @@tracker << reflection
31
+ end
32
+
33
+ def tracked?(reflection)
34
+ @@tracker.include? reflection
35
+ end
36
+
37
+ def set_and_verify_clazz_and_relation(clazz_data)
39
38
  begin
40
- @clazz = clazz_name.constantize
39
+ set_clazz_and_relation(clazz_data)
41
40
  rescue NameError => e
42
- raise Error.new("Unable to find class called #{clazz_name}")
41
+ raise Error.new("Unable to find class called #{clazz_data.classify}")
43
42
  end
44
43
  end
45
44
 
46
- def ClazzName(clazz_data)
45
+ def set_clazz_and_relation(clazz_data)
47
46
  case clazz_data
48
47
  when ActiveRecord::Reflection::AssociationReflection
49
- clazz_data.class_name || clazz_data.table_name
48
+ @clazz = (clazz_data.class_name || clazz_data.table_name).constantize
49
+ @relation = clazz_data.name
50
50
  when String
51
- clazz_data.classify
51
+ @clazz = (clazz_data.classify).constantize
52
+ @relation = clazz_data
52
53
  when NilClass
53
- raise Error.new('Please pass a env var called class to proceed.')
54
+ raise Error.new('Please pass a env var called class to proceed. E.g: rake dread class=user')
54
55
  else
55
56
  raise Error.new("Unable to proceed with #{clazz_data.class}")
56
57
  end
57
58
  end
58
59
 
59
- def relation_name
60
- relation = @pluralized ? @clazz.to_s.pluralize : @clazz.to_s
61
- relation.underscore
60
+ def collect_dependables
61
+ Hash.new.tap do |relation_hash|
62
+ @clazz.reflections.each do |assoc_name, assoc_data|
63
+ case assoc_data.options[:dependent]
64
+ when :delete
65
+ relation_hash[assoc_name] = {}
66
+ when :destroy
67
+ # unless tracked?(assoc_data)
68
+ # track!(assoc_data)
69
+ relation_hash.merge!(
70
+ Graph.new(assoc_data, assoc_data.macro == :has_many).dependable_collection)
71
+ # end
72
+ end
73
+ end
74
+ end
62
75
  end
63
76
 
64
77
  end
data/lib/dread/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Dread
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
data/test/dread_test.rb CHANGED
@@ -32,8 +32,8 @@ class DreadTest < ActiveSupport::TestCase
32
32
  dread_graph = Dread::Graph.new('user')
33
33
  dependable_collection = dread_graph.dependable_collection
34
34
  dependable_collection.assert_valid_keys(:user)
35
- dependable_collection[:user].assert_valid_keys(:tweets, :comments, :setting)
36
- dependable_collection[:user][:tweets].assert_valid_keys(:comments)
35
+ dependable_collection[:user].assert_valid_keys(:tweets, :comments, :setting, :avatar)
36
+ dependable_collection[:user][:tweets].assert_valid_keys(:comments, :pictures)
37
37
  end
38
38
 
39
39
  end
@@ -1,4 +1,5 @@
1
1
  class Comment < ActiveRecord::Base
2
2
  belongs_to :user
3
3
  belongs_to :tweet
4
+ has_many :pictures, dependent: :destroy, as: :imageable
4
5
  end
@@ -0,0 +1,3 @@
1
+ class Picture < ActiveRecord::Base
2
+ belongs_to :imageable, polymorphic: true
3
+ end
@@ -1,4 +1,5 @@
1
1
  class Tweet < ActiveRecord::Base
2
2
  belongs_to :user
3
3
  has_many :comments, dependent: :destroy
4
+ has_many :pictures, dependent: :destroy, as: :imageable
4
5
  end
@@ -1,5 +1,6 @@
1
1
  class User < ActiveRecord::Base
2
- has_many :tweets, dependent: :destroy
3
- has_many :comments, dependent: :destroy
4
- has_one :setting, dependent: :delete, class_name: 'AccountSetting'
2
+ has_many :tweets, dependent: :destroy
3
+ has_many :comments, dependent: :destroy
4
+ has_one :avatar, dependent: :destroy, class_name: 'Picture', as: :imageable
5
+ has_one :setting, dependent: :destroy, class_name: 'AccountSetting'
5
6
  end
@@ -0,0 +1,11 @@
1
+ class CreatePictures < ActiveRecord::Migration
2
+ def change
3
+ create_table :pictures do |t|
4
+ t.string :url
5
+ t.integer :imageable_id
6
+ t.string :imageable_type
7
+
8
+ t.timestamps
9
+ end
10
+ end
11
+ end
@@ -11,7 +11,7 @@
11
11
  #
12
12
  # It's strongly recommended that you check this file into your version control system.
13
13
 
14
- ActiveRecord::Schema.define(version: 20140914191415) do
14
+ ActiveRecord::Schema.define(version: 20140915161852) do
15
15
 
16
16
  create_table "account_settings", force: true do |t|
17
17
  t.string "time_zone"
@@ -34,6 +34,14 @@ ActiveRecord::Schema.define(version: 20140914191415) do
34
34
  t.datetime "updated_at"
35
35
  end
36
36
 
37
+ create_table "pictures", force: true do |t|
38
+ t.string "url"
39
+ t.integer "imageable_id"
40
+ t.string "imageable_type"
41
+ t.datetime "created_at"
42
+ t.datetime "updated_at"
43
+ end
44
+
37
45
  create_table "tweets", force: true do |t|
38
46
  t.integer "user_id"
39
47
  t.string "content"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dread
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Damir Svrtan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-09-14 00:00:00.000000000 Z
11
+ date: 2014-09-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -92,6 +92,7 @@ files:
92
92
  - test/dummy/app/models/account_setting.rb
93
93
  - test/dummy/app/models/comment.rb
94
94
  - test/dummy/app/models/country.rb
95
+ - test/dummy/app/models/picture.rb
95
96
  - test/dummy/app/models/tweet.rb
96
97
  - test/dummy/app/models/user.rb
97
98
  - test/dummy/bin/bundle
@@ -121,6 +122,7 @@ files:
121
122
  - test/dummy/db/migrate/20140913143724_create_comments.rb
122
123
  - test/dummy/db/migrate/20140914191313_create_countries.rb
123
124
  - test/dummy/db/migrate/20140914191415_create_account_settings.rb
125
+ - test/dummy/db/migrate/20140915161852_create_pictures.rb
124
126
  - test/dummy/db/schema.rb
125
127
  - test/dummy/lib/assets/.keep
126
128
  - test/dummy/log/.keep
@@ -160,6 +162,7 @@ test_files:
160
162
  - test/dummy/app/models/account_setting.rb
161
163
  - test/dummy/app/models/comment.rb
162
164
  - test/dummy/app/models/country.rb
165
+ - test/dummy/app/models/picture.rb
163
166
  - test/dummy/app/models/tweet.rb
164
167
  - test/dummy/app/models/user.rb
165
168
  - test/dummy/bin/bundle
@@ -189,6 +192,7 @@ test_files:
189
192
  - test/dummy/db/migrate/20140913143724_create_comments.rb
190
193
  - test/dummy/db/migrate/20140914191313_create_countries.rb
191
194
  - test/dummy/db/migrate/20140914191415_create_account_settings.rb
195
+ - test/dummy/db/migrate/20140915161852_create_pictures.rb
192
196
  - test/dummy/db/schema.rb
193
197
  - test/dummy/lib/assets/.keep
194
198
  - test/dummy/log/.keep