trivet 1.0 → 1.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 +4 -4
- data/README.md +2 -1
- data/lib/trivet.rb +33 -38
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b09cc26cad8b1fbc420bb6868a184dbd29d51816ad584deb8196c97d8a69b396
|
4
|
+
data.tar.gz: a70bbc909538386b6dc2e3fb18a7de6cb1d8c2220a40b961c8bba8c5ef7bda4f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 91990c0931cf9a24efc58ea659e78a29080b7cf66acd82677f79a6329e0debeb4483d9dcdaf45731e6bcbf3baa624eedecc09d31249d1911abfadb32fe4bdbc9
|
7
|
+
data.tar.gz: 3a2842b26cee8dfb2f2777220c03224274f37bf21e0412312efdddc2c15ad3aa8f22fc5e351bc10fe974dc756c7fdd6079a389a0a28a5f75dd327365db5289ec
|
data/README.md
CHANGED
@@ -76,4 +76,5 @@ mike@idocs.com
|
|
76
76
|
|
77
77
|
| version | date | notes |
|
78
78
|
|---------|---------------|-------------------------------------------------------|
|
79
|
-
| 1.0 | June 18, 2020 | Initial upload. |
|
79
|
+
| 1.0 | June 18, 2020 | Initial upload. |
|
80
|
+
| 1.1 | June 22, 2020 | Consolidated can_have_children? and can_have_child? into allow_child?. |
|
data/lib/trivet.rb
CHANGED
@@ -10,7 +10,7 @@ require 'forwardable'
|
|
10
10
|
# Trivet::Node to learn about this package.
|
11
11
|
module Trivet
|
12
12
|
# Version
|
13
|
-
VERSION = '1.
|
13
|
+
VERSION = '1.1'
|
14
14
|
|
15
15
|
#---------------------------------------------------------------------------
|
16
16
|
# query control constants
|
@@ -121,21 +121,6 @@ class Trivet::Node
|
|
121
121
|
#---------------------------------------------------------------------------
|
122
122
|
|
123
123
|
|
124
|
-
#---------------------------------------------------------------------------
|
125
|
-
# can_have_children?
|
126
|
-
#
|
127
|
-
|
128
|
-
# This method indicates if the node can have any children. By default this
|
129
|
-
# method always return true. Override this method in your own class to
|
130
|
-
# set more fine grained rules.
|
131
|
-
def can_have_children?
|
132
|
-
return true
|
133
|
-
end
|
134
|
-
#
|
135
|
-
# can_have_children?
|
136
|
-
#---------------------------------------------------------------------------
|
137
|
-
|
138
|
-
|
139
124
|
#---------------------------------------------------------------------------
|
140
125
|
# parent=
|
141
126
|
#
|
@@ -862,17 +847,37 @@ class Trivet::Node
|
|
862
847
|
|
863
848
|
|
864
849
|
#---------------------------------------------------------------------------
|
865
|
-
#
|
850
|
+
# shortcut to create allow_child? that returns false
|
851
|
+
#
|
852
|
+
|
853
|
+
# This method provides a concise way to override Trivet::Node#allow_child?
|
854
|
+
# so that it always returns false. Just add this to your class:
|
855
|
+
#
|
856
|
+
# self.no_children()
|
857
|
+
#
|
858
|
+
def self.no_children()
|
859
|
+
self.child_levels = []
|
860
|
+
|
861
|
+
self.define_method('allow_child?') do
|
862
|
+
return false
|
863
|
+
end
|
864
|
+
end
|
865
|
+
#
|
866
|
+
# shortcut to create allow_child? that returns false
|
867
|
+
#---------------------------------------------------------------------------
|
868
|
+
|
869
|
+
|
870
|
+
#---------------------------------------------------------------------------
|
871
|
+
# allow_child?
|
866
872
|
#
|
867
873
|
|
868
|
-
# This
|
869
|
-
#
|
870
|
-
|
871
|
-
def can_have_child?(child)
|
874
|
+
# This method is called when a node or other object is added to a node. By
|
875
|
+
# default, always returns true. Override this method to create custom rules.
|
876
|
+
def allow_child?(child)
|
872
877
|
return true
|
873
878
|
end
|
874
879
|
#
|
875
|
-
#
|
880
|
+
# allow_child?
|
876
881
|
#---------------------------------------------------------------------------
|
877
882
|
|
878
883
|
|
@@ -937,8 +942,7 @@ end
|
|
937
942
|
|
938
943
|
# Objects of this class act like an array of the children of a node. However,
|
939
944
|
# unlike an array, attempts to add children result in calling
|
940
|
-
# Trivet::Node#
|
941
|
-
# to check if the child is allowed.
|
945
|
+
# Trivet::Node#allow_child? to check if the child is allowed.
|
942
946
|
class Trivet::Childset
|
943
947
|
#---------------------------------------------------------------------------
|
944
948
|
# delegate
|
@@ -1003,16 +1007,14 @@ class Trivet::Childset
|
|
1003
1007
|
# methods for adding a child to the array
|
1004
1008
|
#
|
1005
1009
|
|
1006
|
-
# Adds a child to the end of the array. Calls
|
1007
|
-
# Trivet::Node#can_have_children? and Trivet::Node#can_have_child? to
|
1010
|
+
# Adds a child to the end of the array. Calls Trivet::Node#allow_child? to
|
1008
1011
|
# check if the child can be added. Does nothing if the node is already a
|
1009
1012
|
# child.
|
1010
1013
|
def push(new_child, opts={})
|
1011
1014
|
return add_child new_child, 'last', opts
|
1012
1015
|
end
|
1013
1016
|
|
1014
|
-
# Adds a child to the end of the array. Calls
|
1015
|
-
# Trivet::Node#can_have_children? and Trivet::Node#can_have_child? to
|
1017
|
+
# Adds a child to the end of the array. Calls Trivet::Node#allow_child? to
|
1016
1018
|
# check if the child can be added. Does nothing if the node is already a
|
1017
1019
|
# child.
|
1018
1020
|
def append(new_child, opts={})
|
@@ -1024,16 +1026,14 @@ class Trivet::Childset
|
|
1024
1026
|
return append(new_child)
|
1025
1027
|
end
|
1026
1028
|
|
1027
|
-
# Adds a child to the beginning of the array. Calls
|
1028
|
-
# Trivet::Node#can_have_children? and Trivet::Node#can_have_child? to
|
1029
|
+
# Adds a child to the beginning of the array. Calls Trivet::Node#allow_child? to
|
1029
1030
|
# check if the child can be added. Does nothing if the node is already a
|
1030
1031
|
# child.
|
1031
1032
|
def unshift(new_child, opts={})
|
1032
1033
|
return add_child new_child, 'first', opts
|
1033
1034
|
end
|
1034
1035
|
|
1035
|
-
# Inserts the child at the position indicated by index. Calls
|
1036
|
-
# Trivet::Node#can_have_children? and Trivet::Node#can_have_child? to
|
1036
|
+
# Inserts the child at the position indicated by index. Calls Trivet::Node#allow_child? to
|
1037
1037
|
# check if the child can be added. Does nothing if the node is already a
|
1038
1038
|
# child.
|
1039
1039
|
def insert(index, new_child, opts={})
|
@@ -1166,12 +1166,7 @@ class Trivet::Childset
|
|
1166
1166
|
opts = {'recurse'=>true}.merge(opts)
|
1167
1167
|
|
1168
1168
|
# check if this parent is allowed to have this child
|
1169
|
-
if not @node.
|
1170
|
-
raise 'parent-cannot-have-children: ' + @node.to_s + ' / ' + new_child.to_s
|
1171
|
-
end
|
1172
|
-
|
1173
|
-
# check if this parent is allowed to have this child
|
1174
|
-
if not @node.can_have_child?(new_child)
|
1169
|
+
if not @node.allow_child?(new_child)
|
1175
1170
|
raise 'parent-cannot-have-this-child: ' + @node.to_s + ' / ' + new_child.to_s
|
1176
1171
|
end
|
1177
1172
|
|