sequel_polymorphic 0.2.2 → 0.3.0

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: 58157571be86e9246c399eb573e4a7d8e1bca04f
4
- data.tar.gz: 4507cd3a9daec3bebca3fa2523f1919ee01255bb
3
+ metadata.gz: 40692adf771cb2a12a5b63b1fdcec4751b8b5129
4
+ data.tar.gz: a46f8b67073537358ce9ce3963afe9872f891ece
5
5
  SHA512:
6
- metadata.gz: 5de6bb6bbe1696dddb2b208f834aabc7c7f7019a2aaafbb7ae5a462a91f69df15f8bd52603aa0ea9c0dd13c0caa0899c5628be449c9f7f9b9d4cdf07f9b052f0
7
- data.tar.gz: 75b40c41d1e09cc14fcff7ec602e7ce0168f1d8871af6b094cd1b130461ea8fe12e4d3af85fa06ea0c6fbb506dc402ae39f0c235ceb815ec91ed42dbb371f2f1
6
+ metadata.gz: 4ef4dece915847592c3d950a120c2c871cd9c87d2d93f220740835ab97a4d4e77fca11d006ffc39750ae6c953bfbae1849b3a452ed3768ea8af87d0d96871aab
7
+ data.tar.gz: 86071652a3e9eec1c8216b3f2539fc0e2e1191613614a23706f3e7dcbd7f709bc3bc66a43fd5d1ca7b5df63d56bf892bebeb217d0195b15485d69078a7ce5e5a
data/CHANGELOG.md ADDED
@@ -0,0 +1,55 @@
1
+ # 0.3.0 (2016-12-12) 0.3.0
2
+
3
+ * **Feature**: *add one-to-one polymorphic associations support*.
4
+
5
+ * **Feature**: *relax Sequel version dependency to any 4.x version*.
6
+
7
+ * **Feature**: *all unrecognised options are passed to underlying methods now*.
8
+
9
+ For instance you can set an association class with `one_to_many :nested_assets, :as => :attachable, :class => Nested::Asset`.
10
+
11
+ * **Enhancements**: many code improvements:
12
+
13
+ * Use [Minitest](https://github.com/seattlerb/minitest) instead of [RSpec](http://rspec.info/) for unit testing.
14
+ * Add code coverage (via [SimpleCov](https://github.com/colszowka/simplecov)).
15
+ * Add [Rake](https://github.com/ruby/rake) as a dependency (though it has been already used).
16
+ * Code improvements.
17
+ * Tests improvements.
18
+
19
+ # 0.2.2 (2015-08-07)
20
+
21
+ * **Fix**: *errors if model's or through model's class name is CamelCased*.
22
+
23
+ I.e. `model.commentable` caused an error if `CamelCasedClassName.many_to_one :commentable, :polymorphic => true` has been used.
24
+ I.e. `model.tags` caused an error if `Model.many_to_many :tags, :through => :camel_cased_through_class, :as => :taggable` has been used.
25
+
26
+ See [#9](https://github.com/jackdempsey/sequel_polymorphic/issues/9)
27
+ and [#11](https://github.com/jackdempsey/sequel_polymorphic/issues/11).
28
+
29
+ # 0.2.1 (2015-08-04)
30
+
31
+ * **Fix**: *many-to-one `#association` method and eager loader error*.
32
+
33
+ See [#9](https://github.com/jackdempsey/sequel_polymorphic/issues/9).
34
+
35
+ # 0.2.0 (2015-06-10)
36
+
37
+ * **Breaking change**: *don't raise an exception if many-to-one association is absent*.
38
+
39
+ Before: A `NameError` is raised by eager loader if association is absent.
40
+
41
+ Now: `nil` is returned.
42
+
43
+ See [#8](https://github.com/jackdempsey/sequel_polymorphic/issues/8).
44
+
45
+ # 0.1.1 (2015-06-09)
46
+
47
+ * **Fix**: *a `NameError` exception is raised if many-to-one association and Sequel >=4.15.0 are used*.
48
+
49
+ See [#7](https://github.com/jackdempsey/sequel_polymorphic/issues/7).
50
+
51
+ * Code enhancements.
52
+
53
+ # 0.1.0 (2014-09-06)
54
+
55
+ Initial release.
@@ -1,7 +1,8 @@
1
1
  module Sequel
2
2
  module Plugins
3
3
  module Polymorphic
4
- # Apply the plugin to the model.
4
+ # TODO: make everything really Sequel-way!
5
+
5
6
  def self.apply(model, options = {})
6
7
  end
7
8
 
@@ -11,16 +12,17 @@ module Sequel
11
12
 
12
13
  module ClassMethods
13
14
 
14
- # Example: Comment.many_to_one :commentable, polymorphic: true
15
+ # Creates a many-to-one relationship.
16
+ # Example: Comment.many_to_one :commentable, :polymorphic => true
15
17
  def many_to_one(*args, &block)
16
18
  able, options = *args
17
19
  options ||= {}
18
20
 
19
21
  if options[:polymorphic]
20
- model = underscore(self.to_s) # comment
21
- plural_model = pluralize(model) # comments
22
+ model = underscore(self.to_s)
23
+ plural_model = pluralize(model)
22
24
 
23
- associate(:many_to_one, able,
25
+ association_options = {
24
26
  :reciprocal => plural_model.to_sym,
25
27
  :reciprocal_type => :many_to_one,
26
28
  :setter => (proc do |able_instance|
@@ -51,8 +53,8 @@ module Sequel
51
53
  end
52
54
  end
53
55
  end)
54
- )
55
-
56
+ }.merge(options)
57
+ associate(:many_to_one, able, association_options)
56
58
  else
57
59
  associate(:many_to_one, *args, &block)
58
60
  end
@@ -61,8 +63,9 @@ module Sequel
61
63
  alias :belongs_to :many_to_one
62
64
 
63
65
 
64
- # Creates a one-to-many relationship. NB: Removing/clearing nullifies the *able fields in the related objects
65
- # Example: Post.one_to_many :comments, as: :commentable
66
+ # Creates a one-to-many relationship.
67
+ # Note: Removing/clearing nullifies the *able fields in the related objects.
68
+ # Example: Post.one_to_many :awesome_comments, :as => :commentable
66
69
  def one_to_many(*args, &block)
67
70
  collection_name, options = *args
68
71
  options ||= {}
@@ -72,7 +75,7 @@ module Sequel
72
75
  able_type = :"#{able}_type"
73
76
  many_dataset_name = :"#{collection_name}_dataset"
74
77
 
75
- associate(:one_to_many, collection_name,
78
+ association_options = {
76
79
  :key => able_id,
77
80
  :reciprocal => able,
78
81
  :reciprocal_type => :one_to_many,
@@ -80,8 +83,8 @@ module Sequel
80
83
  :adder => proc { |many_of_instance| many_of_instance.update(able_id => pk, able_type => self.class.to_s) },
81
84
  :remover => proc { |many_of_instance| many_of_instance.update(able_id => nil, able_type => nil) },
82
85
  :clearer => proc { send(many_dataset_name).update(able_id => nil, able_type => nil) }
83
- )
84
-
86
+ }.merge(options)
87
+ associate(:one_to_many, collection_name, association_options)
85
88
  else
86
89
  associate(:one_to_many, *args, &block)
87
90
  end
@@ -90,7 +93,8 @@ module Sequel
90
93
  alias :has_many :one_to_many
91
94
 
92
95
 
93
- # Creates a many-to-many relationship. NB: Removing/clearing the collection deletes the instances in the through relationship (as opposed to nullifying the *able fields as in the one-to-many)
96
+ # Creates a many-to-many relationship.
97
+ # Note: Removing/clearing the collection deletes the instances in the through relationship (as opposed to nullifying the *able fields as in the one-to-many).
94
98
  # Example: Post.many_to_many :tags, :through => :taggings, :as => :taggable
95
99
  def many_to_many(*args, &block)
96
100
  collection_name, options = *args
@@ -99,23 +103,55 @@ module Sequel
99
103
  if through = (options[:through] || options[:join_table]) and able = options[:as]
100
104
  able_id = :"#{able}_id"
101
105
  able_type = :"#{able}_type"
102
- collection_singular = singularize(collection_name.to_s).to_sym # => tag
106
+ collection_singular = singularize(collection_name.to_s).to_sym
103
107
  collection_singular_id = :"#{collection_singular}_id"
104
- through_klass = constantize(singularize(camelize(through.to_s))) # => Tagging
108
+ through_klass = constantize(singularize(camelize(through.to_s)))
105
109
 
106
- associate(:many_to_many, collection_name,
110
+ association_options = {
107
111
  :left_key => able_id,
108
112
  :join_table => through,
109
113
  :conditions => {able_type => self.to_s},
110
114
  :adder => proc { |many_of_instance| through_klass.create(collection_singular_id => many_of_instance.pk, able_id => pk, able_type => self.class.to_s) },
111
115
  :remover => proc { |many_of_instance| through_klass.where(collection_singular_id => many_of_instance.pk, able_id => pk, able_type => self.class.to_s).delete },
112
116
  :clearer => proc { through_klass.where(able_id => pk, able_type => self.class.to_s).delete }
113
- )
117
+ }.merge(options)
118
+ associate(:many_to_many, collection_name, association_options)
114
119
 
115
120
  else
116
121
  associate(:many_to_many, *args, &block)
117
122
  end
118
123
  end
124
+
125
+
126
+ # Creates a one-to-one relationship.
127
+ # Note: Removing/clearing nullifies the *able fields in the related objects.
128
+ # Example: Article.one_to_one :post, as: :postable
129
+ def one_to_one(*args, &block)
130
+ collection_name, options = *args
131
+ options ||= {}
132
+
133
+ if able = options[:as]
134
+ able_id = :"#{able}_id"
135
+ able_type = :"#{able}_type"
136
+ many_dataset_name = :"#{collection_name}_dataset"
137
+
138
+ association_options = {
139
+ :key => able_id,
140
+ :reciprocal => able,
141
+ :reciprocal_type => :one_to_one,
142
+ :conditions => {able_type => self.to_s},
143
+ :adder => proc { |many_of_instance| many_of_instance.update(able_id => pk, able_type => self.class.to_s) },
144
+ :remover => proc { |many_of_instance| many_of_instance.update(able_id => nil, able_type => nil) },
145
+ :clearer => proc { send(many_dataset_name).update(able_id => nil, able_type => nil) }
146
+ }.merge(options)
147
+ associate(:one_to_one, collection_name, association_options)
148
+ else
149
+ associate(:one_to_one, *args, &block)
150
+ end
151
+ end
152
+
153
+ alias :has_one :one_to_one
154
+
119
155
  end # ClassMethods
120
156
  end # Polymorphic
121
157
  end # Plugins
@@ -2,7 +2,7 @@ module Sequel
2
2
  module Plugins
3
3
  module Polymorphic
4
4
 
5
- VERSION = '0.2.2'
5
+ VERSION = '0.3.0'
6
6
 
7
7
  end # Polymorphic
8
8
  end # Plugins
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sequel_polymorphic
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jack Dempsey
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2015-08-07 00:00:00.000000000 Z
13
+ date: 2016-12-22 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: sequel
@@ -18,42 +18,84 @@ dependencies:
18
18
  requirements:
19
19
  - - "~>"
20
20
  - !ruby/object:Gem::Version
21
- version: '4.0'
21
+ version: '4'
22
22
  type: :runtime
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
25
25
  requirements:
26
26
  - - "~>"
27
27
  - !ruby/object:Gem::Version
28
- version: '4.0'
28
+ version: '4'
29
29
  - !ruby/object:Gem::Dependency
30
- name: rspec
30
+ name: minitest
31
31
  requirement: !ruby/object:Gem::Requirement
32
32
  requirements:
33
33
  - - "~>"
34
34
  - !ruby/object:Gem::Version
35
- version: 2.14.1
35
+ version: '5.8'
36
36
  type: :development
37
37
  prerelease: false
38
38
  version_requirements: !ruby/object:Gem::Requirement
39
39
  requirements:
40
40
  - - "~>"
41
41
  - !ruby/object:Gem::Version
42
- version: 2.14.1
42
+ version: '5.8'
43
+ - !ruby/object:Gem::Dependency
44
+ name: minitest-hooks
45
+ requirement: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: '1.2'
50
+ type: :development
51
+ prerelease: false
52
+ version_requirements: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - "~>"
55
+ - !ruby/object:Gem::Version
56
+ version: '1.2'
43
57
  - !ruby/object:Gem::Dependency
44
58
  name: sqlite3
45
59
  requirement: !ruby/object:Gem::Requirement
46
60
  requirements:
47
61
  - - "~>"
48
62
  - !ruby/object:Gem::Version
49
- version: 1.3.10
63
+ version: '1.3'
64
+ type: :development
65
+ prerelease: false
66
+ version_requirements: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - "~>"
69
+ - !ruby/object:Gem::Version
70
+ version: '1.3'
71
+ - !ruby/object:Gem::Dependency
72
+ name: simplecov
73
+ requirement: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - "~>"
76
+ - !ruby/object:Gem::Version
77
+ version: '0.10'
78
+ type: :development
79
+ prerelease: false
80
+ version_requirements: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - "~>"
83
+ - !ruby/object:Gem::Version
84
+ version: '0.10'
85
+ - !ruby/object:Gem::Dependency
86
+ name: rake
87
+ requirement: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - "~>"
90
+ - !ruby/object:Gem::Version
91
+ version: '11.3'
50
92
  type: :development
51
93
  prerelease: false
52
94
  version_requirements: !ruby/object:Gem::Requirement
53
95
  requirements:
54
96
  - - "~>"
55
97
  - !ruby/object:Gem::Version
56
- version: 1.3.10
98
+ version: '11.3'
57
99
  description: A gem that provides Sequel::Models with polymorphic association capabilities
58
100
  email:
59
101
  - jack.dempsey@gmail.com
@@ -64,7 +106,9 @@ extensions: []
64
106
  extra_rdoc_files:
65
107
  - README.md
66
108
  - LICENSE
109
+ - CHANGELOG.md
67
110
  files:
111
+ - CHANGELOG.md
68
112
  - LICENSE
69
113
  - README.md
70
114
  - lib/sequel_polymorphic.rb
@@ -90,7 +134,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
90
134
  version: '0'
91
135
  requirements: []
92
136
  rubyforge_project:
93
- rubygems_version: 2.4.5
137
+ rubygems_version: 2.5.2
94
138
  signing_key:
95
139
  specification_version: 4
96
140
  summary: A gem that provides Sequel::Models with polymorphic association capabilities