mongoid-sleeping_king_studios 0.2.0 → 0.3.1

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: 69ea55c009d98431ff13407f84c1b210fc5ac89d
4
- data.tar.gz: cf358c4f2146e0b3bfcad4b92bb1697ecaa3e565
3
+ metadata.gz: 9872d41d59ce51e97ead17d8219802dd80df2069
4
+ data.tar.gz: 008ca4394448aa380c101d1aad7b719ca80d7416
5
5
  SHA512:
6
- metadata.gz: 2cf1a558a348b53842e181242b37368f2ee50f2fc3af52f8172a851d46f26841ac68f8801ef1350462ce751fba7c251cbbee65a1d9caf3d503ea6fe7d4c0e9bb
7
- data.tar.gz: 8a67abe9b88d4ddd7879b3e5317adbc704bd7af0d603e44d53c5aae910c5099bc275b7b77cf5c98a2a8b573c95a8d0106329cedf074c5aa765de9080d270f833
6
+ metadata.gz: 3a8324ecbcdc29f818bf8e2b83b82b48b1df78e1c0ad45938000acf86b388e35fb0eefb8816dfe956b6f63ae8af482aad997f61e352e1a27842060a1c1032c2c
7
+ data.tar.gz: 67391ada74283759fd019872dbcb2ca97817c72f2e39c35ff52f226fd157529c4917bf436b0165f2b87bd4eabd80ebb90a6a0a5e8904c793d06494f25424e86a
data/README.md CHANGED
@@ -47,6 +47,36 @@ Sets up a basic tree structure by adding belongs_to :parent and has_many
47
47
  include Mongoid::SleepingKingStudios::Tree
48
48
  end # class
49
49
 
50
+ #### Options
51
+
52
+ To customise the created #parent and #children relations, you can define
53
+ ::options_for_parent and ::options_for_children class methods before including
54
+ the Tree concern. These methods must return a hash if defined.
55
+
56
+ As of 0.3.1, you can change the name of the class methods used to find the
57
+ customised options by setting Tree.options_for_parent_name and
58
+ Tree.options_for_children_name.
59
+
60
+ class EvilEmployee
61
+ include Mongoid::Document
62
+
63
+ def self.options_for_parent
64
+ { :relation_name => :overlord }
65
+ end # class method options_for_parent
66
+
67
+ def self.options_for_children
68
+ { :relation_name => :minions, :dependent => :destroy }
69
+ end # class method options_for_children
70
+
71
+ include Mongoid::SleepingKingStudios::Tree
72
+ end # class
73
+
74
+ Available options include the default Mongoid options for a :belongs_to and a
75
+ :has_many relationship, respectively. In addition, you can set a :relation_name
76
+ option to change the name of the created relation (see example above). The
77
+ concern will automatically update the respective :inverse_of options to match
78
+ the updated relation names.
79
+
50
80
  ## License
51
81
 
52
82
  RSpec::SleepingKingStudios is released under the
@@ -12,10 +12,61 @@ module Mongoid::SleepingKingStudios
12
12
  # include Mongoid::SleepingKingStudios::Tree
13
13
  # end # class
14
14
  #
15
+ # Since 0.3.0, you can customise the generated :parent and :children
16
+ # relations by defining optional class methods ::options_for_parent and
17
+ # ::options_for_children, which must return hashes of valid options. These
18
+ # must be defined prior to including this mixin, or the default options will
19
+ # be applied instead.
20
+ #
21
+ # In addition, you can customise the names of the relations by adding a
22
+ # :relation_name key to either ::options_for hash. The concern will
23
+ # automatically update the respective :inverse_of options to match the
24
+ # updated relation names.
25
+ #
26
+ # Since 0.3.1, you can additionally customise the name of the class methods
27
+ # used to determine the customised options by changing the value of
28
+ # Tree.options_for_parent_name and Tree.options_for_children_name.
29
+ #
30
+ # @example Setting up the tree with alternate relation names:
31
+ # class EvilEmployee
32
+ # include Mongoid::Document
33
+ #
34
+ # def self.options_for_parent
35
+ # { :relation_name => "overlord" }
36
+ # end # class method options_for_parent
37
+ #
38
+ # def self.options_for_children
39
+ # { :relation_name => "minions", :dependent => :destroy }
40
+ # end # class method options_for_children
41
+ #
42
+ # include Mongoid::SleepingKingStudios::Tree
43
+ # end # class
44
+ #
15
45
  # @since 0.2.0
16
46
  module Tree
17
47
  extend ActiveSupport::Concern
18
48
 
49
+ class << self
50
+ # Gets the name of the method on the base module that is used to find
51
+ # customisation options for the :parent relation.
52
+ #
53
+ # @return [Symbol] By default, returns :options_for_parent.
54
+ #
55
+ # @since 0.3.1
56
+ attr_accessor :options_for_parent_name
57
+
58
+ # Gets the name of the method on the base module that is used to find
59
+ # customisation options for the :children relation.
60
+ #
61
+ # @return [Symbol] By default, returns :options_for_children.
62
+ #
63
+ # @since 0.3.1
64
+ attr_accessor :options_for_children_name
65
+ end # class << self
66
+
67
+ self.options_for_parent_name = :options_for_parent
68
+ self.options_for_children_name = :options_for_children
69
+
19
70
  # @!method parent
20
71
  # Returns the parent object, or nil if the object is a root.
21
72
  #
@@ -27,8 +78,17 @@ module Mongoid::SleepingKingStudios
27
78
  # @return [Array<Tree>]
28
79
 
29
80
  included do |base|
30
- belongs_to :parent, :class_name => base.name, :inverse_of => :children
31
- has_many :children, :class_name => base.name, :inverse_of => :parent
81
+ p_opts = { :relation_name => :parent, :class_name => base.name }
82
+ c_opts = { :relation_name => :children, :class_name => base.name }
83
+
84
+ p_opts.update(send Tree.options_for_parent_name) if respond_to?(Tree.options_for_parent_name)
85
+ c_opts.update(send Tree.options_for_children_name) if respond_to?(Tree.options_for_children_name)
86
+
87
+ p_opts.update :inverse_of => c_opts[:relation_name]
88
+ c_opts.update :inverse_of => p_opts[:relation_name]
89
+
90
+ belongs_to p_opts.delete(:relation_name), p_opts
91
+ has_many c_opts.delete(:relation_name), c_opts
32
92
  end # included
33
93
 
34
94
  # Class methods added to the base class via #extend.
@@ -51,14 +111,14 @@ module Mongoid::SleepingKingStudios
51
111
 
52
112
  # Returns true if the object is a leaf object, e.g. has no child objects.
53
113
  #
54
- # @return [Boolean] true if the object is a leaf; otherwise false
114
+ # @return [Boolean] True if the object has no children; otherwise false.
55
115
  def leaf?
56
116
  children.empty?
57
117
  end # method leaf?
58
118
 
59
119
  # Returns true if the object is a root object, e.g. has no parent object.
60
120
  #
61
- # @return [Boolean] true if the object is a root; otherwise false
121
+ # @return [Boolean] True if the object has no parent; otherwise false.
62
122
  def root?
63
123
  parent.nil?
64
124
  end # method root?
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Mongoid
4
4
  module SleepingKingStudios
5
- VERSION = '0.2.0'
5
+ VERSION = '0.3.1'
6
6
  end # module
7
7
  end # module
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid-sleeping_king_studios
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rob "Merlin" Smith
@@ -58,14 +58,14 @@ dependencies:
58
58
  requirements:
59
59
  - - '>='
60
60
  - !ruby/object:Gem::Version
61
- version: 1.0.0.rc.2
61
+ version: 1.0.0.rc.3
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - '>='
67
67
  - !ruby/object:Gem::Version
68
- version: 1.0.0.rc.2
68
+ version: 1.0.0.rc.3
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: factory_girl
71
71
  requirement: !ruby/object:Gem::Requirement
@@ -122,6 +122,20 @@ dependencies:
122
122
  - - '>='
123
123
  - !ruby/object:Gem::Version
124
124
  version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: yard
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - '>='
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - '>='
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
125
139
  description: |2
126
140
  A collection of concerns and extensions to add functionality to Mongoid
127
141
  documents and collections. The features can be included individually or by
@@ -158,7 +172,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
158
172
  version: '0'
159
173
  requirements: []
160
174
  rubyforge_project:
161
- rubygems_version: 2.0.3
175
+ rubygems_version: 2.0.6
162
176
  signing_key:
163
177
  specification_version: 4
164
178
  summary: A collection of Mongoid concerns and extensions.