jss-api 0.5.4
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.
- data/CHANGES.md +4 -0
- data/LICENSE.txt +174 -0
- data/README.md +368 -0
- data/THANKS.md +6 -0
- data/lib/jss-api.rb +549 -0
- data/lib/jss-api/api_connection.rb +326 -0
- data/lib/jss-api/api_object.rb +590 -0
- data/lib/jss-api/api_object/advanced_search.rb +389 -0
- data/lib/jss-api/api_object/advanced_search/advanced_computer_search.rb +95 -0
- data/lib/jss-api/api_object/advanced_search/advanced_mobile_device_search.rb +96 -0
- data/lib/jss-api/api_object/advanced_search/advanced_user_search.rb +95 -0
- data/lib/jss-api/api_object/building.rb +92 -0
- data/lib/jss-api/api_object/category.rb +147 -0
- data/lib/jss-api/api_object/computer.rb +852 -0
- data/lib/jss-api/api_object/creatable.rb +98 -0
- data/lib/jss-api/api_object/criteriable.rb +189 -0
- data/lib/jss-api/api_object/criteriable/criteria.rb +231 -0
- data/lib/jss-api/api_object/criteriable/criterion.rb +228 -0
- data/lib/jss-api/api_object/department.rb +93 -0
- data/lib/jss-api/api_object/distribution_point.rb +490 -0
- data/lib/jss-api/api_object/extendable.rb +221 -0
- data/lib/jss-api/api_object/extension_attribute.rb +457 -0
- data/lib/jss-api/api_object/extension_attribute/computer_extension_attribute.rb +362 -0
- data/lib/jss-api/api_object/extension_attribute/mobile_device_extension_attribute.rb +189 -0
- data/lib/jss-api/api_object/extension_attribute/user_extension_attribute.rb +117 -0
- data/lib/jss-api/api_object/group.rb +380 -0
- data/lib/jss-api/api_object/group/computer_group.rb +124 -0
- data/lib/jss-api/api_object/group/mobile_device_group.rb +139 -0
- data/lib/jss-api/api_object/group/user_group.rb +139 -0
- data/lib/jss-api/api_object/ldap_server.rb +535 -0
- data/lib/jss-api/api_object/locatable.rb +268 -0
- data/lib/jss-api/api_object/matchable.rb +97 -0
- data/lib/jss-api/api_object/mobile_device.rb +556 -0
- data/lib/jss-api/api_object/netboot_server.rb +148 -0
- data/lib/jss-api/api_object/network_segment.rb +414 -0
- data/lib/jss-api/api_object/package.rb +760 -0
- data/lib/jss-api/api_object/peripheral.rb +335 -0
- data/lib/jss-api/api_object/peripheral_type.rb +295 -0
- data/lib/jss-api/api_object/policy.rb +882 -0
- data/lib/jss-api/api_object/purchasable.rb +316 -0
- data/lib/jss-api/api_object/removable_macaddr.rb +98 -0
- data/lib/jss-api/api_object/scopable.rb +136 -0
- data/lib/jss-api/api_object/scopable/scope.rb +621 -0
- data/lib/jss-api/api_object/script.rb +631 -0
- data/lib/jss-api/api_object/site.rb +93 -0
- data/lib/jss-api/api_object/software_update_server.rb +109 -0
- data/lib/jss-api/api_object/updatable.rb +117 -0
- data/lib/jss-api/api_object/uploadable.rb +138 -0
- data/lib/jss-api/api_object/user.rb +272 -0
- data/lib/jss-api/client.rb +500 -0
- data/lib/jss-api/compatibility.rb +66 -0
- data/lib/jss-api/composer.rb +171 -0
- data/lib/jss-api/configuration.rb +301 -0
- data/lib/jss-api/db_connection.rb +243 -0
- data/lib/jss-api/exceptions.rb +83 -0
- data/lib/jss-api/ruby_extensions.rb +35 -0
- data/lib/jss-api/ruby_extensions/filetest.rb +43 -0
- data/lib/jss-api/ruby_extensions/hash.rb +79 -0
- data/lib/jss-api/ruby_extensions/ipaddr.rb +91 -0
- data/lib/jss-api/ruby_extensions/pathname.rb +77 -0
- data/lib/jss-api/ruby_extensions/string.rb +43 -0
- data/lib/jss-api/ruby_extensions/time.rb +63 -0
- data/lib/jss-api/server.rb +108 -0
- data/lib/jss-api/version.rb +31 -0
- metadata +219 -0
@@ -0,0 +1,243 @@
|
|
1
|
+
### Copyright 2014 Pixar
|
2
|
+
###
|
3
|
+
### Licensed under the Apache License, Version 2.0 (the "Apache License")
|
4
|
+
### with the following modification; you may not use this file except in
|
5
|
+
### compliance with the Apache License and the following modification to it:
|
6
|
+
### Section 6. Trademarks. is deleted and replaced with:
|
7
|
+
###
|
8
|
+
### 6. Trademarks. This License does not grant permission to use the trade
|
9
|
+
### names, trademarks, service marks, or product names of the Licensor
|
10
|
+
### and its affiliates, except as required to comply with Section 4(c) of
|
11
|
+
### the License and to reproduce the content of the NOTICE file.
|
12
|
+
###
|
13
|
+
### You may obtain a copy of the Apache License at
|
14
|
+
###
|
15
|
+
### http://www.apache.org/licenses/LICENSE-2.0
|
16
|
+
###
|
17
|
+
### Unless required by applicable law or agreed to in writing, software
|
18
|
+
### distributed under the Apache License with the above modification is
|
19
|
+
### distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
20
|
+
### KIND, either express or implied. See the Apache License for the specific
|
21
|
+
### language governing permissions and limitations under the Apache License.
|
22
|
+
###
|
23
|
+
###
|
24
|
+
|
25
|
+
###
|
26
|
+
module JSS
|
27
|
+
|
28
|
+
|
29
|
+
|
30
|
+
#####################################
|
31
|
+
### Module Variables
|
32
|
+
#####################################
|
33
|
+
|
34
|
+
#####################################
|
35
|
+
### Classes
|
36
|
+
#####################################
|
37
|
+
|
38
|
+
###
|
39
|
+
### A mysql connection to the JSS database.
|
40
|
+
###
|
41
|
+
### This is a singleton class, only one can exist at a time, and it
|
42
|
+
### is created, but not connected, automatically when the module loads.
|
43
|
+
###
|
44
|
+
### Use it via the JSS::DB_CNX constant (for connection metadata)
|
45
|
+
### and the JSS::DB_CNX.db attribute (which contains the actual mysql
|
46
|
+
### query interface) for making queries
|
47
|
+
###
|
48
|
+
### Direct MySQL access is minimal and discouraged, since it
|
49
|
+
### bypasses the API, and can be very dangerous. However, it's necessary
|
50
|
+
### to overcome some limitations of the API or to access custom tables.
|
51
|
+
###
|
52
|
+
### While a database connction isn't required for most things,
|
53
|
+
### warnings will be sent to stderr when functionality is limited due to
|
54
|
+
### a lack of a database connection i.e. when JSS::DB_CNX.connected? == false
|
55
|
+
###
|
56
|
+
### To make a connection with credentials, just call the #connect method thus:
|
57
|
+
### JSS::DB_CNX.connect :server => 'server.company.com', :user => "user", :pw => "pw"
|
58
|
+
###
|
59
|
+
### Other options include:
|
60
|
+
### :db_name => which database to connect to, defaults to 'jamfsoftware'
|
61
|
+
### :port => tcp port for connection to server, defaults to the standard mysql port.
|
62
|
+
### :connect_timeout => seconds to wait before giving up on connection, defaults to 120
|
63
|
+
### :read_timeout => seconds to wait before giving up on recieving data, defaults to 120
|
64
|
+
### :write_timeout => seconds to wait before giving up on sending data, defaults to 120
|
65
|
+
### :timeout => sets all three timeouts to the same value, defaults to 120
|
66
|
+
###
|
67
|
+
### Calling JSS::DB_CNX.connect again will re-use any values not provided.
|
68
|
+
### but will create a new connection.
|
69
|
+
###
|
70
|
+
class DBConnection
|
71
|
+
|
72
|
+
include Singleton
|
73
|
+
|
74
|
+
#####################################
|
75
|
+
### Class Constants
|
76
|
+
#####################################
|
77
|
+
|
78
|
+
### The name of the JSS database on the mysql server
|
79
|
+
DEFAULT_DB_NAME = "jamfsoftware"
|
80
|
+
|
81
|
+
### give the connection a 120 second timeout, for really slow
|
82
|
+
### net connections (like... from airplanes)
|
83
|
+
DFT_TIMEOUT = 120
|
84
|
+
|
85
|
+
###
|
86
|
+
DFT_SOCKET = '/var/mysql/mysql.sock'
|
87
|
+
|
88
|
+
### the strftime format for reading/writing dates in the db
|
89
|
+
SQL_DATE_FORMAT = "%Y-%m-%d %H:%M:%S"
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
attr_reader :server
|
94
|
+
attr_reader :port
|
95
|
+
attr_reader :socket
|
96
|
+
attr_reader :user
|
97
|
+
attr_reader :db_name
|
98
|
+
attr_reader :connect_timeout
|
99
|
+
attr_reader :read_timeout
|
100
|
+
attr_reader :write_timeout
|
101
|
+
attr_reader :connected
|
102
|
+
|
103
|
+
|
104
|
+
def initialize ()
|
105
|
+
require 'mysql'
|
106
|
+
@mysql = Mysql.init
|
107
|
+
@connected = false
|
108
|
+
end #init
|
109
|
+
|
110
|
+
###
|
111
|
+
### Connect to the JSS MySQL database.
|
112
|
+
###
|
113
|
+
### @param args[Hash] the keyed arguments for connection.
|
114
|
+
###
|
115
|
+
### @option args :server[String] Required, the hostname of the JSS API server
|
116
|
+
###
|
117
|
+
### @option args :port[Integer] the port number to connect with, defaults to the default Mysql TCP port
|
118
|
+
###
|
119
|
+
### @option args :socket[String,Pathname] when the server is 'localhost', the path to the connection socket.
|
120
|
+
###
|
121
|
+
### @option args :db_name[String] the name of the database to use, defaults to 'jamfsoftware'
|
122
|
+
###
|
123
|
+
### @option args :user[String] Required, the mysql user to connect as
|
124
|
+
###
|
125
|
+
### @option args :pw[String,Symbol] Required, the password for that user, or :prompt, or :stdin
|
126
|
+
### If :prompt, the user is promted on the commandline to enter the password for the :user.
|
127
|
+
### If :stdin#, the password is read from a line of std in represented by the digit at #,
|
128
|
+
### so :stdin3 reads the passwd from the third line of standard input. defaults to line 2,
|
129
|
+
### if no digit is supplied. see {JSS.stdin}
|
130
|
+
###
|
131
|
+
### @option args :connect_timeout[Integer] the number of seconds to wait for an initial response, defaults to 120
|
132
|
+
###
|
133
|
+
### @option args :read_timeout[Integer] the number of seconds before read-request times out, defaults to 120
|
134
|
+
###
|
135
|
+
### @option args :write_timeout[Integer] the number of seconds before write-request times out, defaults to 120
|
136
|
+
###
|
137
|
+
### @option args :timeout[Integer] used for any of the timeouts that aren't explicitly set.
|
138
|
+
###
|
139
|
+
### @return [true] the connection was successfully made.
|
140
|
+
###
|
141
|
+
def connect(args = {})
|
142
|
+
|
143
|
+
# settings from config if they aren't in the args
|
144
|
+
args[:server] ||= JSS::CONFIG.db_server_name
|
145
|
+
args[:port] ||= JSS::CONFIG.db_server_port
|
146
|
+
args[:socket] ||= JSS::CONFIG.db_server_socket
|
147
|
+
args[:db_name] ||= JSS::CONFIG.db_name
|
148
|
+
args[:user] ||= JSS::CONFIG.db_username
|
149
|
+
args[:connect_timeout] ||= JSS::CONFIG.db_connect_timeout
|
150
|
+
args[:read_timeout] ||= JSS::CONFIG.db_read_timeout
|
151
|
+
args[:write_timeout] ||= JSS::CONFIG.db_write_timeout
|
152
|
+
|
153
|
+
### if one timeout was given, use it for all three
|
154
|
+
args[:connect_timeout] ||= args[:timeout]
|
155
|
+
args[:read_timeout] ||= args[:timeout]
|
156
|
+
args[:write_timeout] ||= args[:timeout]
|
157
|
+
|
158
|
+
### if these weren't given, use the defaults
|
159
|
+
args[:connect_timeout] ||= DFT_TIMEOUT
|
160
|
+
args[:read_timeout] ||= DFT_TIMEOUT
|
161
|
+
args[:write_timeout] ||= DFT_TIMEOUT
|
162
|
+
args[:port] ||= Mysql::MYSQL_TCP_PORT
|
163
|
+
args[:socket] ||= DFT_SOCKET
|
164
|
+
args[:db_name] ||= DEFAULT_DB_NAME
|
165
|
+
|
166
|
+
@mysql.close if connected?
|
167
|
+
|
168
|
+
@server = args[:server]
|
169
|
+
@port = args[:port]
|
170
|
+
@socket = args[:socket]
|
171
|
+
@mysql_name = args[:db_name]
|
172
|
+
@user = args[:user]
|
173
|
+
@connect_timeout = args[:connect_timeout]
|
174
|
+
@read_timeout = args[:read_timeout]
|
175
|
+
@write_timeout = args[:write_timeout]
|
176
|
+
|
177
|
+
# make sure we have a user
|
178
|
+
raise JSS::MissingDataError, "No JSS user specified, or listed in configuration." unless args[:user]
|
179
|
+
|
180
|
+
# passwd from prompt, stdin, or args?
|
181
|
+
raise JSS::MissingDataError, "Missing :pw (or :prompt/:stdin) for user '#{@user}'" unless args[:pw]
|
182
|
+
|
183
|
+
@pw = if args[:pw] == :prompt
|
184
|
+
JSS.prompt_for_password "Enter the password for the MySQL user '#{@user}':"
|
185
|
+
elsif args[:pw].is_a?(Symbol) and args[:pw].to_s.start_with?('stdin')
|
186
|
+
args[:pw].to_s =~ /^stdin(\d+)$/
|
187
|
+
line = $1
|
188
|
+
line ||= 2
|
189
|
+
JSS.stdin line
|
190
|
+
else
|
191
|
+
args[:pw]
|
192
|
+
end
|
193
|
+
|
194
|
+
|
195
|
+
|
196
|
+
@mysql = Mysql.init
|
197
|
+
|
198
|
+
@mysql.options Mysql::OPT_CONNECT_TIMEOUT, @connect_timeout
|
199
|
+
@mysql.options Mysql::OPT_READ_TIMEOUT, @read_timeout
|
200
|
+
@mysql.options Mysql::OPT_WRITE_TIMEOUT, @write_timeout
|
201
|
+
|
202
|
+
@mysql.connect @server, @user , @pw , @mysql_name, @port, @socket
|
203
|
+
|
204
|
+
@connected = true
|
205
|
+
end # reconnect
|
206
|
+
|
207
|
+
###
|
208
|
+
### @return [Mysql] The mysql database connection itself
|
209
|
+
###
|
210
|
+
def db
|
211
|
+
raise JSS::InvalidConnectionError, "No database connection. Please use JSS::DB_CNX.connect" unless JSS::DB_CNX.connected?
|
212
|
+
@mysql
|
213
|
+
end
|
214
|
+
|
215
|
+
###
|
216
|
+
### close the connection to the database
|
217
|
+
### it'll have to be re-connected before using again
|
218
|
+
###
|
219
|
+
def disconnect
|
220
|
+
@mysql.close if connected?
|
221
|
+
@connected = false
|
222
|
+
nil
|
223
|
+
end # disconnect
|
224
|
+
|
225
|
+
#### Aliases
|
226
|
+
|
227
|
+
alias connected? connected
|
228
|
+
|
229
|
+
end # class DBConnection
|
230
|
+
|
231
|
+
### The single instance of the DBConnection
|
232
|
+
DB_CNX = DBConnection.instance
|
233
|
+
|
234
|
+
###
|
235
|
+
### @return [Mysql] The mysql database available through the DBConnection.instance
|
236
|
+
###
|
237
|
+
def self.db
|
238
|
+
DB_CNX.db
|
239
|
+
end
|
240
|
+
|
241
|
+
end # module
|
242
|
+
|
243
|
+
|
@@ -0,0 +1,83 @@
|
|
1
|
+
### Copyright 2014 Pixar
|
2
|
+
###
|
3
|
+
### Licensed under the Apache License, Version 2.0 (the "Apache License")
|
4
|
+
### with the following modification; you may not use this file except in
|
5
|
+
### compliance with the Apache License and the following modification to it:
|
6
|
+
### Section 6. Trademarks. is deleted and replaced with:
|
7
|
+
###
|
8
|
+
### 6. Trademarks. This License does not grant permission to use the trade
|
9
|
+
### names, trademarks, service marks, or product names of the Licensor
|
10
|
+
### and its affiliates, except as required to comply with Section 4(c) of
|
11
|
+
### the License and to reproduce the content of the NOTICE file.
|
12
|
+
###
|
13
|
+
### You may obtain a copy of the Apache License at
|
14
|
+
###
|
15
|
+
### http://www.apache.org/licenses/LICENSE-2.0
|
16
|
+
###
|
17
|
+
### Unless required by applicable law or agreed to in writing, software
|
18
|
+
### distributed under the Apache License with the above modification is
|
19
|
+
### distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
20
|
+
### KIND, either express or implied. See the Apache License for the specific
|
21
|
+
### language governing permissions and limitations under the Apache License.
|
22
|
+
###
|
23
|
+
###
|
24
|
+
|
25
|
+
###
|
26
|
+
module JSS
|
27
|
+
|
28
|
+
#####################################
|
29
|
+
### Exceptions
|
30
|
+
#####################################
|
31
|
+
|
32
|
+
###
|
33
|
+
### MissingDataError - raise this error when we
|
34
|
+
### are missing args, or other simliar stuff.
|
35
|
+
###
|
36
|
+
class MissingDataError < RuntimeError; end
|
37
|
+
|
38
|
+
###
|
39
|
+
### InvalidDataError - raise this error when
|
40
|
+
### a data item isn't what we expected.
|
41
|
+
###
|
42
|
+
class InvalidDataError < RuntimeError; end
|
43
|
+
|
44
|
+
###
|
45
|
+
### InvalidConnectionError - raise this error when we
|
46
|
+
### don't have a usable connection to a network service, or
|
47
|
+
### don't have proper authentication/authorization.
|
48
|
+
###
|
49
|
+
class InvalidConnectionError < RuntimeError; end
|
50
|
+
|
51
|
+
###
|
52
|
+
### NoSuchItemError - raise this error when
|
53
|
+
### a desired item doesn't exist.
|
54
|
+
###
|
55
|
+
class NoSuchItemError < RuntimeError; end
|
56
|
+
|
57
|
+
###
|
58
|
+
### AlreadyExistsError - raise this error when
|
59
|
+
### trying to create something that already exists.
|
60
|
+
###
|
61
|
+
class AlreadyExistsError < RuntimeError; end
|
62
|
+
|
63
|
+
###
|
64
|
+
### FileServiceError - raise this error when
|
65
|
+
### there's a problem accessing file service on a
|
66
|
+
### distribution point.
|
67
|
+
###
|
68
|
+
class FileServiceError < RuntimeError; end
|
69
|
+
|
70
|
+
###
|
71
|
+
### UnmanagedError - raise this when we
|
72
|
+
### try to do something managerial to
|
73
|
+
### an unmanaged object
|
74
|
+
###
|
75
|
+
class UnmanagedError < RuntimeError; end
|
76
|
+
|
77
|
+
###
|
78
|
+
### UnsupportedError - raise this when we
|
79
|
+
### try to do something not yet supported
|
80
|
+
###
|
81
|
+
class UnsupportedError < RuntimeError; end
|
82
|
+
|
83
|
+
end # module JSS
|
@@ -0,0 +1,35 @@
|
|
1
|
+
### Copyright 2014 Pixar
|
2
|
+
###
|
3
|
+
### Licensed under the Apache License, Version 2.0 (the "Apache License")
|
4
|
+
### with the following modification; you may not use this file except in
|
5
|
+
### compliance with the Apache License and the following modification to it:
|
6
|
+
### Section 6. Trademarks. is deleted and replaced with:
|
7
|
+
###
|
8
|
+
### 6. Trademarks. This License does not grant permission to use the trade
|
9
|
+
### names, trademarks, service marks, or product names of the Licensor
|
10
|
+
### and its affiliates, except as required to comply with Section 4(c) of
|
11
|
+
### the License and to reproduce the content of the NOTICE file.
|
12
|
+
###
|
13
|
+
### You may obtain a copy of the Apache License at
|
14
|
+
###
|
15
|
+
### http://www.apache.org/licenses/LICENSE-2.0
|
16
|
+
###
|
17
|
+
### Unless required by applicable law or agreed to in writing, software
|
18
|
+
### distributed under the Apache License with the above modification is
|
19
|
+
### distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
20
|
+
### KIND, either express or implied. See the Apache License for the specific
|
21
|
+
### language governing permissions and limitations under the Apache License.
|
22
|
+
###
|
23
|
+
###
|
24
|
+
|
25
|
+
### All monkey-patched convenience methods are either prefixed with "jss_" or
|
26
|
+
### if they are type coercions, "to_jss_"
|
27
|
+
###
|
28
|
+
|
29
|
+
###
|
30
|
+
require "jss-api/ruby_extensions/filetest.rb"
|
31
|
+
require "jss-api/ruby_extensions/hash.rb"
|
32
|
+
require "jss-api/ruby_extensions/ipaddr.rb"
|
33
|
+
require "jss-api/ruby_extensions/pathname.rb"
|
34
|
+
require "jss-api/ruby_extensions/time.rb"
|
35
|
+
require "jss-api/ruby_extensions/string.rb"
|
@@ -0,0 +1,43 @@
|
|
1
|
+
### Copyright 2014 Pixar
|
2
|
+
###
|
3
|
+
### Licensed under the Apache License, Version 2.0 (the "Apache License")
|
4
|
+
### with the following modification; you may not use this file except in
|
5
|
+
### compliance with the Apache License and the following modification to it:
|
6
|
+
### Section 6. Trademarks. is deleted and replaced with:
|
7
|
+
###
|
8
|
+
### 6. Trademarks. This License does not grant permission to use the trade
|
9
|
+
### names, trademarks, service marks, or product names of the Licensor
|
10
|
+
### and its affiliates, except as required to comply with Section 4(c) of
|
11
|
+
### the License and to reproduce the content of the NOTICE file.
|
12
|
+
###
|
13
|
+
### You may obtain a copy of the Apache License at
|
14
|
+
###
|
15
|
+
### http://www.apache.org/licenses/LICENSE-2.0
|
16
|
+
###
|
17
|
+
### Unless required by applicable law or agreed to in writing, software
|
18
|
+
### distributed under the Apache License with the above modification is
|
19
|
+
### distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
20
|
+
### KIND, either express or implied. See the Apache License for the specific
|
21
|
+
### language governing permissions and limitations under the Apache License.
|
22
|
+
###
|
23
|
+
###
|
24
|
+
|
25
|
+
###
|
26
|
+
module FileTest
|
27
|
+
#############################################
|
28
|
+
### FileTest.file? returns true if
|
29
|
+
### the item is a symlink pointing to a regular file.
|
30
|
+
###
|
31
|
+
### This test, real_file?, returns true if the item is
|
32
|
+
### a regular file but NOT a symlink.
|
33
|
+
###
|
34
|
+
def FileTest.jss_real_file?(path)
|
35
|
+
FileTest.file?(path) and not FileTest.symlink?(path)
|
36
|
+
end # real_file?
|
37
|
+
end # module FileTest
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
|
@@ -0,0 +1,79 @@
|
|
1
|
+
### Copyright 2014 Pixar
|
2
|
+
###
|
3
|
+
### Licensed under the Apache License, Version 2.0 (the "Apache License")
|
4
|
+
### with the following modification; you may not use this file except in
|
5
|
+
### compliance with the Apache License and the following modification to it:
|
6
|
+
### Section 6. Trademarks. is deleted and replaced with:
|
7
|
+
###
|
8
|
+
### 6. Trademarks. This License does not grant permission to use the trade
|
9
|
+
### names, trademarks, service marks, or product names of the Licensor
|
10
|
+
### and its affiliates, except as required to comply with Section 4(c) of
|
11
|
+
### the License and to reproduce the content of the NOTICE file.
|
12
|
+
###
|
13
|
+
### You may obtain a copy of the Apache License at
|
14
|
+
###
|
15
|
+
### http://www.apache.org/licenses/LICENSE-2.0
|
16
|
+
###
|
17
|
+
### Unless required by applicable law or agreed to in writing, software
|
18
|
+
### distributed under the Apache License with the above modification is
|
19
|
+
### distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
20
|
+
### KIND, either express or implied. See the Apache License for the specific
|
21
|
+
### language governing permissions and limitations under the Apache License.
|
22
|
+
###
|
23
|
+
###
|
24
|
+
|
25
|
+
###
|
26
|
+
class Hash
|
27
|
+
|
28
|
+
###
|
29
|
+
### Convert Hash values to nil.
|
30
|
+
###
|
31
|
+
### With no block, values equalling the String, or any member of the Array, given
|
32
|
+
### will be converted to nil. Equality is evaluated with == and Array#include?
|
33
|
+
###
|
34
|
+
### With a block, if the result of the block evaluates to true, the value is converted to nil.
|
35
|
+
###
|
36
|
+
### Subhashes are ignored unless recurse is true.
|
37
|
+
###
|
38
|
+
### @param to_nils[String,Array] Hash values equal to (==) these become nil. Defaults to empty string
|
39
|
+
###
|
40
|
+
### @param recurse[Boolean] should sub-Hashes be nillified?
|
41
|
+
###
|
42
|
+
### @yield [value] Hash values for which the block returns true will become nil.
|
43
|
+
###
|
44
|
+
### @return [Hash] the hash with the desired values converted to nil
|
45
|
+
###
|
46
|
+
### @example
|
47
|
+
### hash = {:foo => '', :bar => {:baz => '' }}
|
48
|
+
### hash.jss_nillify! # {:foo => nil, :bar => {:baz => '' }}
|
49
|
+
###
|
50
|
+
### hash = {:foo => '', :bar => {:baz => '' }}
|
51
|
+
### hash.jss_nillify! '', :recurse # {:foo => nil, :bar => {:baz => nil }}
|
52
|
+
###
|
53
|
+
### hash = {:foo => 123, :bar => {:baz => '', :bim => "123" }}
|
54
|
+
### hash.jss_nillify! ['', 123], :recurse # {:foo => nil, :bar => {:baz => nil, :bim => "123" }}
|
55
|
+
###
|
56
|
+
### hash = {:foo => 123, :bar => {:baz => '', :bim => "123" }}
|
57
|
+
### hash.jss_nillify!(:anything, :recurse){|v| v.to_i == 123 } # {:foo => nil, :bar => {:baz => '', :bim => nil }}
|
58
|
+
###
|
59
|
+
def jss_nillify!(to_nils = '', recurse = false, &block )
|
60
|
+
|
61
|
+
nillify_these = [] << to_nils
|
62
|
+
nillify_these.flatten!
|
63
|
+
|
64
|
+
self.each_pair do |k,v|
|
65
|
+
if v.class == Hash
|
66
|
+
v.jss_nillify!(to_nils, recurse, &block)
|
67
|
+
next
|
68
|
+
end
|
69
|
+
do_it = if block_given?
|
70
|
+
yield v
|
71
|
+
else
|
72
|
+
nillify_these.include? v
|
73
|
+
end
|
74
|
+
self[k] = nil if do_it
|
75
|
+
end # each pair
|
76
|
+
end # def nillify
|
77
|
+
end # class
|
78
|
+
|
79
|
+
|