bbcloud 0.11.2 → 0.12.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (198) hide show
  1. data/.gitignore +3 -2
  2. data/Gemfile +3 -0
  3. data/README +11 -1
  4. data/README.rdoc +11 -1
  5. data/Rakefile +12 -0
  6. data/bbcloud.gemspec +10 -10
  7. data/bin/brightbox-accounts +7 -2
  8. data/bin/brightbox-cloudips +7 -2
  9. data/bin/brightbox-config +7 -2
  10. data/bin/brightbox-images +7 -2
  11. data/bin/brightbox-lbs +7 -2
  12. data/bin/brightbox-servers +7 -2
  13. data/bin/brightbox-types +7 -2
  14. data/bin/brightbox-users +7 -2
  15. data/bin/brightbox-zones +7 -2
  16. data/lib/bbcloud.rb +43 -0
  17. data/lib/bbcloud/accounts.rb +5 -3
  18. data/lib/bbcloud/command_generator.rb +50 -0
  19. data/lib/bbcloud/commands/accounts-list.rb +13 -11
  20. data/lib/bbcloud/commands/accounts-reset-ftp-password.rb +26 -23
  21. data/lib/bbcloud/commands/accounts-show.rb +20 -16
  22. data/lib/bbcloud/commands/cloudips-create.rb +16 -14
  23. data/lib/bbcloud/commands/cloudips-destroy.rb +29 -26
  24. data/lib/bbcloud/commands/cloudips-list.rb +13 -11
  25. data/lib/bbcloud/commands/cloudips-map.rb +48 -46
  26. data/lib/bbcloud/commands/cloudips-show.rb +15 -12
  27. data/lib/bbcloud/commands/cloudips-unmap.rb +28 -25
  28. data/lib/bbcloud/commands/config-client-add.rb +33 -31
  29. data/lib/bbcloud/commands/config-client-default.rb +18 -16
  30. data/lib/bbcloud/commands/config-client-list.rb +19 -16
  31. data/lib/bbcloud/commands/config-client-remove.rb +18 -16
  32. data/lib/bbcloud/commands/images-destroy.rb +18 -16
  33. data/lib/bbcloud/commands/images-list.rb +20 -18
  34. data/lib/bbcloud/commands/images-register.rb +21 -19
  35. data/lib/bbcloud/commands/images-show.rb +17 -15
  36. data/lib/bbcloud/commands/lbs-add-nodes.rb +15 -13
  37. data/lib/bbcloud/commands/lbs-create.rb +65 -63
  38. data/lib/bbcloud/commands/lbs-destroy.rb +13 -11
  39. data/lib/bbcloud/commands/lbs-list.rb +13 -11
  40. data/lib/bbcloud/commands/lbs-remove-nodes.rb +15 -13
  41. data/lib/bbcloud/commands/lbs-show.rb +15 -13
  42. data/lib/bbcloud/commands/lbs-update.rb +59 -57
  43. data/lib/bbcloud/commands/servers-activate-cloud.rb +18 -16
  44. data/lib/bbcloud/commands/servers-create.rb +80 -77
  45. data/lib/bbcloud/commands/servers-destroy.rb +17 -15
  46. data/lib/bbcloud/commands/servers-list.rb +14 -14
  47. data/lib/bbcloud/commands/servers-show.rb +39 -37
  48. data/lib/bbcloud/commands/servers-shutdown.rb +14 -12
  49. data/lib/bbcloud/commands/servers-snapshot.rb +14 -12
  50. data/lib/bbcloud/commands/servers-start.rb +15 -13
  51. data/lib/bbcloud/commands/servers-stop.rb +14 -12
  52. data/lib/bbcloud/commands/types-list.rb +13 -11
  53. data/lib/bbcloud/commands/types-show.rb +18 -16
  54. data/lib/bbcloud/commands/users-list.rb +13 -11
  55. data/lib/bbcloud/commands/users-show.rb +17 -15
  56. data/lib/bbcloud/commands/users-update.rb +27 -25
  57. data/lib/bbcloud/commands/zones-list.rb +13 -11
  58. data/lib/bbcloud/config.rb +125 -111
  59. data/lib/bbcloud/error_parser.rb +34 -0
  60. data/lib/bbcloud/fog_extensions.rb +19 -0
  61. data/lib/bbcloud/gli_global_hooks.rb +56 -0
  62. data/lib/bbcloud/logging.rb +40 -0
  63. data/lib/bbcloud/ruby_core_ext.rb +9 -0
  64. data/lib/bbcloud/servers.rb +7 -2
  65. data/lib/bbcloud/tables.rb +78 -73
  66. data/lib/bbcloud/vendor/gli/.gitignore +10 -0
  67. data/lib/bbcloud/vendor/gli/.rvmrc +1 -0
  68. data/lib/bbcloud/vendor/gli/Gemfile +5 -0
  69. data/lib/bbcloud/vendor/gli/LICENSE.txt +201 -0
  70. data/lib/bbcloud/vendor/gli/README.rdoc +54 -0
  71. data/lib/bbcloud/vendor/gli/Rakefile +84 -0
  72. data/lib/bbcloud/vendor/gli/bin/gli +72 -0
  73. data/lib/bbcloud/vendor/gli/bin/report_on_rake_results +10 -0
  74. data/lib/bbcloud/vendor/gli/bin/test_all_rubies.sh +2 -0
  75. data/lib/bbcloud/vendor/gli/features/gli_executable.feature +88 -0
  76. data/lib/bbcloud/vendor/gli/features/gli_init.feature +122 -0
  77. data/lib/bbcloud/vendor/gli/features/step_definitions/gli_executable_steps.rb +12 -0
  78. data/lib/bbcloud/vendor/gli/features/step_definitions/gli_init_steps.rb +4 -0
  79. data/lib/bbcloud/vendor/gli/features/support/env.rb +21 -0
  80. data/lib/bbcloud/vendor/gli/gli.cheat +80 -0
  81. data/lib/bbcloud/vendor/gli/gli.gemspec +46 -0
  82. data/lib/bbcloud/vendor/gli/gli.rdoc +51 -0
  83. data/lib/bbcloud/vendor/gli/lib/gli.rb +560 -0
  84. data/lib/bbcloud/vendor/gli/lib/gli/command.rb +124 -0
  85. data/lib/bbcloud/vendor/gli/lib/gli/command_line_token.rb +58 -0
  86. data/lib/bbcloud/vendor/gli/lib/gli/copy_options_to_aliases.rb +33 -0
  87. data/lib/bbcloud/vendor/gli/lib/gli/exceptions.rb +45 -0
  88. data/lib/bbcloud/vendor/gli/lib/gli/flag.rb +67 -0
  89. data/lib/bbcloud/vendor/gli/lib/gli/options.rb +19 -0
  90. data/lib/bbcloud/vendor/gli/lib/gli/switch.rb +63 -0
  91. data/lib/bbcloud/vendor/gli/lib/gli/terminal.rb +79 -0
  92. data/lib/bbcloud/vendor/gli/lib/gli_version.rb +3 -0
  93. data/lib/bbcloud/vendor/gli/lib/support/help.rb +180 -0
  94. data/lib/bbcloud/vendor/gli/lib/support/initconfig.rb +34 -0
  95. data/lib/bbcloud/vendor/gli/lib/support/rdoc.rb +119 -0
  96. data/lib/bbcloud/vendor/gli/lib/support/scaffold.rb +284 -0
  97. data/lib/bbcloud/vendor/gli/test/config.yaml +9 -0
  98. data/lib/bbcloud/vendor/gli/test/gli.reek +116 -0
  99. data/lib/bbcloud/vendor/gli/test/roodi.yaml +9 -0
  100. data/lib/bbcloud/vendor/gli/test/tc_command.rb +329 -0
  101. data/lib/bbcloud/vendor/gli/test/tc_flag.rb +67 -0
  102. data/lib/bbcloud/vendor/gli/test/tc_gli.rb +429 -0
  103. data/lib/bbcloud/vendor/gli/test/tc_options.rb +31 -0
  104. data/lib/bbcloud/vendor/gli/test/tc_parsing.rb +279 -0
  105. data/lib/bbcloud/vendor/gli/test/tc_switch.rb +80 -0
  106. data/lib/bbcloud/vendor/gli/test/tc_terminal.rb +91 -0
  107. data/lib/bbcloud/version.rb +1 -1
  108. data/pkg/bbcloud-0.12.0.gem +0 -0
  109. data/spec/fixtures/vcr_cassettes/server_list.yml +117 -0
  110. data/spec/servers_spec.rb +36 -0
  111. data/spec/spec_helper.rb +24 -0
  112. data/spec/support/common_helpers.rb +47 -0
  113. metadata +129 -135
  114. data/lib/bbcloud/cli.rb +0 -160
  115. data/lib/bbcloud/vendor/fog/lib/fog.rb +0 -14
  116. data/lib/bbcloud/vendor/fog/lib/fog/compute.rb +0 -43
  117. data/lib/bbcloud/vendor/fog/lib/fog/compute/brightbox.rb +0 -162
  118. data/lib/bbcloud/vendor/fog/lib/fog/compute/models/brightbox/account.rb +0 -51
  119. data/lib/bbcloud/vendor/fog/lib/fog/compute/models/brightbox/cloud_ip.rb +0 -47
  120. data/lib/bbcloud/vendor/fog/lib/fog/compute/models/brightbox/cloud_ips.rb +0 -34
  121. data/lib/bbcloud/vendor/fog/lib/fog/compute/models/brightbox/flavor.rb +0 -33
  122. data/lib/bbcloud/vendor/fog/lib/fog/compute/models/brightbox/flavors.rb +0 -28
  123. data/lib/bbcloud/vendor/fog/lib/fog/compute/models/brightbox/image.rb +0 -56
  124. data/lib/bbcloud/vendor/fog/lib/fog/compute/models/brightbox/images.rb +0 -28
  125. data/lib/bbcloud/vendor/fog/lib/fog/compute/models/brightbox/load_balancer.rb +0 -50
  126. data/lib/bbcloud/vendor/fog/lib/fog/compute/models/brightbox/load_balancers.rb +0 -28
  127. data/lib/bbcloud/vendor/fog/lib/fog/compute/models/brightbox/server.rb +0 -108
  128. data/lib/bbcloud/vendor/fog/lib/fog/compute/models/brightbox/servers.rb +0 -29
  129. data/lib/bbcloud/vendor/fog/lib/fog/compute/models/brightbox/user.rb +0 -39
  130. data/lib/bbcloud/vendor/fog/lib/fog/compute/models/brightbox/users.rb +0 -29
  131. data/lib/bbcloud/vendor/fog/lib/fog/compute/models/brightbox/zone.rb +0 -21
  132. data/lib/bbcloud/vendor/fog/lib/fog/compute/models/brightbox/zones.rb +0 -29
  133. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/activate_console_server.rb +0 -20
  134. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/add_listeners_load_balancer.rb +0 -20
  135. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/add_nodes_load_balancer.rb +0 -20
  136. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/create_api_client.rb +0 -19
  137. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/create_cloud_ip.rb +0 -19
  138. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/create_image.rb +0 -19
  139. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/create_load_balancer.rb +0 -19
  140. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/create_server.rb +0 -19
  141. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/destroy_api_client.rb +0 -20
  142. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/destroy_cloud_ip.rb +0 -20
  143. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/destroy_image.rb +0 -20
  144. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/destroy_load_balancer.rb +0 -20
  145. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/destroy_server.rb +0 -20
  146. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/get_account.rb +0 -19
  147. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/get_api_client.rb +0 -20
  148. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/get_cloud_ip.rb +0 -20
  149. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/get_image.rb +0 -20
  150. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/get_interface.rb +0 -20
  151. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/get_load_balancer.rb +0 -20
  152. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/get_server.rb +0 -20
  153. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/get_server_type.rb +0 -20
  154. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/get_user.rb +0 -20
  155. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/get_zone.rb +0 -20
  156. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/list_api_clients.rb +0 -19
  157. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/list_cloud_ips.rb +0 -19
  158. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/list_images.rb +0 -19
  159. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/list_load_balancers.rb +0 -19
  160. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/list_server_types.rb +0 -19
  161. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/list_servers.rb +0 -19
  162. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/list_users.rb +0 -19
  163. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/list_zones.rb +0 -19
  164. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/map_cloud_ip.rb +0 -20
  165. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/remove_listeners_load_balancer.rb +0 -20
  166. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/remove_nodes_load_balancer.rb +0 -20
  167. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/reset_ftp_password_account.rb +0 -19
  168. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/resize_server.rb +0 -19
  169. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/shutdown_server.rb +0 -20
  170. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/snapshot_server.rb +0 -20
  171. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/start_server.rb +0 -20
  172. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/stop_server.rb +0 -20
  173. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/unmap_cloud_ip.rb +0 -20
  174. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/update_account.rb +0 -20
  175. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/update_api_client.rb +0 -21
  176. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/update_image.rb +0 -21
  177. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/update_load_balancer.rb +0 -21
  178. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/update_server.rb +0 -21
  179. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/update_user.rb +0 -21
  180. data/lib/bbcloud/vendor/fog/lib/fog/core.rb +0 -32
  181. data/lib/bbcloud/vendor/fog/lib/fog/core/attributes.rb +0 -178
  182. data/lib/bbcloud/vendor/fog/lib/fog/core/collection.rb +0 -127
  183. data/lib/bbcloud/vendor/fog/lib/fog/core/connection.rb +0 -35
  184. data/lib/bbcloud/vendor/fog/lib/fog/core/credentials.rb +0 -92
  185. data/lib/bbcloud/vendor/fog/lib/fog/core/deprecation.rb +0 -23
  186. data/lib/bbcloud/vendor/fog/lib/fog/core/errors.rb +0 -20
  187. data/lib/bbcloud/vendor/fog/lib/fog/core/hmac.rb +0 -48
  188. data/lib/bbcloud/vendor/fog/lib/fog/core/mock.rb +0 -68
  189. data/lib/bbcloud/vendor/fog/lib/fog/core/model.rb +0 -57
  190. data/lib/bbcloud/vendor/fog/lib/fog/core/parser.rb +0 -99
  191. data/lib/bbcloud/vendor/fog/lib/fog/core/provider.rb +0 -18
  192. data/lib/bbcloud/vendor/fog/lib/fog/core/scp.rb +0 -67
  193. data/lib/bbcloud/vendor/fog/lib/fog/core/service.rb +0 -175
  194. data/lib/bbcloud/vendor/fog/lib/fog/core/ssh.rb +0 -120
  195. data/lib/bbcloud/vendor/fog/lib/fog/core/time.rb +0 -27
  196. data/lib/bbcloud/vendor/fog/lib/fog/core/wait_for.rb +0 -17
  197. data/lib/bbcloud/vendor/fog/lib/fog/providers.rb +0 -9
  198. data/lib/bbcloud/vendor/fog/lib/fog/providers/brightbox.rb +0 -9
@@ -1,20 +0,0 @@
1
- module Fog
2
- module Brightbox
3
- class Compute
4
- class Real
5
-
6
- def start_server(identifier, options = {})
7
- return nil if identifier.nil? || identifier == ""
8
- request(
9
- :expects => [202],
10
- :method => 'POST',
11
- :path => "/1.0/servers/#{identifier}/start",
12
- :headers => {"Content-Type" => "application/json"},
13
- :body => options.to_json
14
- )
15
- end
16
-
17
- end
18
- end
19
- end
20
- end
@@ -1,20 +0,0 @@
1
- module Fog
2
- module Brightbox
3
- class Compute
4
- class Real
5
-
6
- def stop_server(identifier, options = {})
7
- return nil if identifier.nil? || identifier == ""
8
- request(
9
- :expects => [202],
10
- :method => 'POST',
11
- :path => "/1.0/servers/#{identifier}/stop",
12
- :headers => {"Content-Type" => "application/json"},
13
- :body => options.to_json
14
- )
15
- end
16
-
17
- end
18
- end
19
- end
20
- end
@@ -1,20 +0,0 @@
1
- module Fog
2
- module Brightbox
3
- class Compute
4
- class Real
5
-
6
- def unmap_cloud_ip(identifier, options = {})
7
- return nil if identifier.nil? || identifier == ""
8
- request(
9
- :expects => [202],
10
- :method => 'POST',
11
- :path => "/1.0/cloud_ips/#{identifier}/unmap",
12
- :headers => {"Content-Type" => "application/json"},
13
- :body => options.to_json
14
- )
15
- end
16
-
17
- end
18
- end
19
- end
20
- end
@@ -1,20 +0,0 @@
1
- module Fog
2
- module Brightbox
3
- class Compute
4
- class Real
5
-
6
- def update_account(options = {})
7
- return nil if options.empty? || options.nil?
8
- request(
9
- :expects => [200],
10
- :method => 'PUT',
11
- :path => "/1.0/account",
12
- :headers => {"Content-Type" => "application/json"},
13
- :body => options.to_json
14
- )
15
- end
16
-
17
- end
18
- end
19
- end
20
- end
@@ -1,21 +0,0 @@
1
- module Fog
2
- module Brightbox
3
- class Compute
4
- class Real
5
-
6
- def update_api_client(identifier, options = {})
7
- return nil if identifier.nil? || identifier == ""
8
- return nil if options.empty? || options.nil?
9
- request(
10
- :expects => [200],
11
- :method => 'PUT',
12
- :path => "/1.0/api_clients/#{identifier}",
13
- :headers => {"Content-Type" => "application/json"},
14
- :body => options.to_json
15
- )
16
- end
17
-
18
- end
19
- end
20
- end
21
- end
@@ -1,21 +0,0 @@
1
- module Fog
2
- module Brightbox
3
- class Compute
4
- class Real
5
-
6
- def update_image(identifier, options = {})
7
- return nil if identifier.nil? || identifier == ""
8
- return nil if options.empty? || options.nil?
9
- request(
10
- :expects => [200],
11
- :method => 'PUT',
12
- :path => "/1.0/images/#{identifier}",
13
- :headers => {"Content-Type" => "application/json"},
14
- :body => options.to_json
15
- )
16
- end
17
-
18
- end
19
- end
20
- end
21
- end
@@ -1,21 +0,0 @@
1
- module Fog
2
- module Brightbox
3
- class Compute
4
- class Real
5
-
6
- def update_load_balancer(identifier, options = {})
7
- return nil if identifier.nil? || identifier == ""
8
- return nil if options.empty? || options.nil?
9
- request(
10
- :expects => [202],
11
- :method => 'PUT',
12
- :path => "/1.0/load_balancers/#{identifier}",
13
- :headers => {"Content-Type" => "application/json"},
14
- :body => options.to_json
15
- )
16
- end
17
-
18
- end
19
- end
20
- end
21
- end
@@ -1,21 +0,0 @@
1
- module Fog
2
- module Brightbox
3
- class Compute
4
- class Real
5
-
6
- def update_server(identifier, options = {})
7
- return nil if identifier.nil? || identifier == ""
8
- return nil if options.empty? || options.nil?
9
- request(
10
- :expects => [200],
11
- :method => 'PUT',
12
- :path => "/1.0/servers/#{identifier}",
13
- :headers => {"Content-Type" => "application/json"},
14
- :body => options.to_json
15
- )
16
- end
17
-
18
- end
19
- end
20
- end
21
- end
@@ -1,21 +0,0 @@
1
- module Fog
2
- module Brightbox
3
- class Compute
4
- class Real
5
-
6
- def update_user(identifier, options = {})
7
- return nil if identifier.nil? || identifier == ""
8
- return nil if options.empty? || options.nil?
9
- request(
10
- :expects => [200],
11
- :method => 'PUT',
12
- :path => "/1.0/users/#{identifier}",
13
- :headers => {"Content-Type" => "application/json"},
14
- :body => options.to_json
15
- )
16
- end
17
-
18
- end
19
- end
20
- end
21
- end
@@ -1,32 +0,0 @@
1
- __LIB_DIR__ = File.expand_path(File.join(File.dirname(__FILE__), '..'))
2
-
3
- $LOAD_PATH.unshift __LIB_DIR__ unless
4
- $LOAD_PATH.include?(__LIB_DIR__) ||
5
- $LOAD_PATH.include?(File.expand_path(__LIB_DIR__))
6
-
7
- # external core dependencies
8
- require 'rubygems'
9
- require 'base64'
10
- require 'cgi'
11
- require 'excon'
12
- require 'formatador'
13
- require 'time'
14
- require 'timeout'
15
-
16
- # internal core dependencies
17
- require 'fog/core/attributes'
18
- require 'fog/core/collection'
19
- require 'fog/core/connection'
20
- require 'fog/core/credentials'
21
- require 'fog/core/deprecation'
22
- require 'fog/core/errors'
23
- require 'fog/core/hmac'
24
- require 'fog/core/model'
25
- require 'fog/core/mock'
26
- # require 'fog/core/parser'
27
- require 'fog/core/provider'
28
- require 'fog/core/service'
29
- require 'fog/core/ssh'
30
- require 'fog/core/scp'
31
- require 'fog/core/time'
32
- require 'fog/core/wait_for'
@@ -1,178 +0,0 @@
1
- module Fog
2
- module Attributes
3
- module ClassMethods
4
-
5
- def _load(marshalled)
6
- new(Marshal.load(marshalled))
7
- end
8
-
9
- def aliases
10
- @aliases ||= {}
11
- end
12
-
13
- def attributes
14
- @attributes ||= []
15
- end
16
-
17
- def attribute(name, options = {})
18
- class_eval <<-EOS, __FILE__, __LINE__
19
- def #{name}
20
- attributes[:#{name}]
21
- end
22
- EOS
23
- case options[:type]
24
- when :boolean
25
- class_eval <<-EOS, __FILE__, __LINE__
26
- def #{name}=(new_#{name})
27
- attributes[:#{name}] = case new_#{name}
28
- when 'true'
29
- true
30
- when 'false'
31
- false
32
- end
33
- end
34
- EOS
35
- when :float
36
- class_eval <<-EOS, __FILE__, __LINE__
37
- def #{name}=(new_#{name})
38
- attributes[:#{name}] = new_#{name}.to_f
39
- end
40
- EOS
41
- when :integer
42
- class_eval <<-EOS, __FILE__, __LINE__
43
- def #{name}=(new_#{name})
44
- attributes[:#{name}] = new_#{name}.to_i
45
- end
46
- EOS
47
- when :string
48
- class_eval <<-EOS, __FILE__, __LINE__
49
- def #{name}=(new_#{name})
50
- attributes[:#{name}] = new_#{name}.to_s
51
- end
52
- EOS
53
- when :time
54
- class_eval <<-EOS, __FILE__, __LINE__
55
- def #{name}=(new_#{name})
56
- attributes[:#{name}] = if new_#{name}.nil? || new_#{name} == "" || new_#{name}.is_a?(Time)
57
- new_#{name}
58
- else
59
- Time.parse(new_#{name})
60
- end
61
- end
62
- EOS
63
- when :array
64
- class_eval <<-EOS, __FILE__, __LINE__
65
- def #{name}=(new_#{name})
66
- attributes[:#{name}] = [*new_#{name}]
67
- end
68
- EOS
69
- else
70
- if squash = options[:squash]
71
- class_eval <<-EOS, __FILE__, __LINE__
72
- def #{name}=(new_data)
73
- if new_data.is_a?(Hash)
74
- if new_data[:#{squash}] || new_data["#{squash}"]
75
- attributes[:#{name}] = new_data[:#{squash}] || new_data["#{squash}"]
76
- else
77
- attributes[:#{name}] = [ new_data ]
78
- end
79
- else
80
- attributes[:#{name}] = new_data
81
- end
82
- end
83
- EOS
84
- else
85
- class_eval <<-EOS, __FILE__, __LINE__
86
- def #{name}=(new_#{name})
87
- attributes[:#{name}] = new_#{name}
88
- end
89
- EOS
90
- end
91
- end
92
- @attributes ||= []
93
- @attributes |= [name]
94
- for new_alias in [*options[:aliases]]
95
- aliases[new_alias] = name
96
- end
97
- end
98
-
99
- def identity(name, options = {})
100
- @identity = name
101
- self.attribute(name, options)
102
- end
103
-
104
- def ignore_attributes(*args)
105
- @ignored_attributes = args
106
- end
107
-
108
- def ignored_attributes
109
- @ignored_attributes ||= []
110
- end
111
-
112
- end
113
-
114
- module InstanceMethods
115
-
116
- def _dump(level)
117
- Marshal.dump(attributes)
118
- end
119
-
120
- def attributes
121
- @attributes ||= {}
122
- end
123
-
124
- def identity
125
- send(self.class.instance_variable_get('@identity'))
126
- end
127
-
128
- def identity=(new_identity)
129
- send("#{self.class.instance_variable_get('@identity')}=", new_identity)
130
- end
131
-
132
- def merge_attributes(new_attributes = {})
133
- for key, value in new_attributes
134
- unless self.class.ignored_attributes.include?(key)
135
- if aliased_key = self.class.aliases[key]
136
- send("#{aliased_key}=", value)
137
- elsif (public_methods | private_methods).detect {|method| ["#{key}=", :"#{key}="].include?(method)}
138
- send("#{key}=", value)
139
- else
140
- attributes[key] = value
141
- end
142
- end
143
- end
144
- self
145
- end
146
-
147
- def new_record?
148
- !identity
149
- end
150
-
151
- # check that the attributes specified in args exist and is not nil
152
- def requires(*args)
153
- missing = []
154
- for arg in [:connection] | args
155
- missing << arg unless send("#{arg}")
156
- end
157
- unless missing.empty?
158
- if missing.length == 1
159
- raise(ArgumentError, "#{missing.first} is required for this operation")
160
- else
161
- raise(ArgumentError, "#{missing[0...-1].join(", ")} and #{missing[-1]} are required for this operation")
162
- end
163
- end
164
- end
165
-
166
- private
167
-
168
- def remap_attributes(attributes, mapping)
169
- for key, value in mapping
170
- if attributes.key?(key)
171
- attributes[value] = attributes.delete(key)
172
- end
173
- end
174
- end
175
-
176
- end
177
- end
178
- end
@@ -1,127 +0,0 @@
1
- module Fog
2
- class Collection < Array
3
-
4
- extend Fog::Attributes::ClassMethods
5
- include Fog::Attributes::InstanceMethods
6
-
7
- Array.public_instance_methods(false).each do |method|
8
- unless [:reject, :select].include?(method.to_sym)
9
- class_eval <<-RUBY
10
- def #{method}(*args)
11
- unless @loaded
12
- lazy_load
13
- end
14
- super
15
- end
16
- RUBY
17
- end
18
- end
19
-
20
- %w[reject select].each do |method|
21
- class_eval <<-RUBY
22
- def #{method}(*args)
23
- unless @loaded
24
- lazy_load
25
- end
26
- data = super
27
- result = self.clone.clear.concat(data)
28
- end
29
- RUBY
30
- end
31
-
32
- def self.model(new_model=nil)
33
- if new_model == nil
34
- @model
35
- else
36
- @model = new_model
37
- end
38
- end
39
-
40
- attr_accessor :connection
41
-
42
- remove_method :clear
43
- def clear
44
- @loaded = true
45
- super
46
- end
47
-
48
- def create(attributes = {})
49
- object = new(attributes)
50
- object.save
51
- object
52
- end
53
-
54
- def initialize(attributes = {})
55
- @loaded = false
56
- merge_attributes(attributes)
57
- end
58
-
59
- remove_method :inspect
60
- def inspect
61
- Thread.current[:formatador] ||= Formatador.new
62
- data = "#{Thread.current[:formatador].indentation}<#{self.class.name}\n"
63
- Thread.current[:formatador].indent do
64
- unless self.class.attributes.empty?
65
- data << "#{Thread.current[:formatador].indentation}"
66
- data << self.class.attributes.map {|attribute| "#{attribute}=#{send(attribute).inspect}"}.join(",\n#{Thread.current[:formatador].indentation}")
67
- data << "\n"
68
- end
69
- data << "#{Thread.current[:formatador].indentation}["
70
- unless self.empty?
71
- data << "\n"
72
- Thread.current[:formatador].indent do
73
- data << self.map {|member| member.inspect}.join(",\n")
74
- data << "\n"
75
- end
76
- data << Thread.current[:formatador].indentation
77
- end
78
- data << "]\n"
79
- end
80
- data << "#{Thread.current[:formatador].indentation}>"
81
- data
82
- end
83
-
84
- def load(objects)
85
- clear
86
- for object in objects
87
- self << new(object)
88
- end
89
- self
90
- end
91
-
92
- def model
93
- self.class.instance_variable_get('@model')
94
- end
95
-
96
- def new(attributes = {})
97
- model.new(
98
- attributes.merge(
99
- :collection => self,
100
- :connection => connection
101
- )
102
- )
103
- end
104
-
105
- def reload
106
- clear
107
- lazy_load
108
- self
109
- end
110
-
111
- def table(attributes = nil)
112
- Formatador.display_table(self.map {|instance| instance.attributes}, attributes)
113
- end
114
-
115
- def to_json
116
- require 'json'
117
- self.map {|member| member.attributes}.to_json
118
- end
119
-
120
- private
121
-
122
- def lazy_load
123
- self.all
124
- end
125
-
126
- end
127
- end