Skip to content

Commit

Permalink
Refactored: used ActiveSupport to generalize many methods
Browse files Browse the repository at this point in the history
  • Loading branch information
joeyates committed Nov 12, 2010
1 parent a45092b commit 738a271
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 43 deletions.
15 changes: 2 additions & 13 deletions lib/rightstuff/array_instance.rb
Expand Up @@ -9,19 +9,8 @@ def initialize( client, item )
super super
end end


def self.collection_xpath def active?
'/instances/instance' @attributes[ :state ] != 'stopped'
end

def method_missing( name , *args, &block )
result = super
return result unless result.nil?
return nil if @attributes[ :state ] == 'stopped'
return @attributes[ name ]
end

def id
@attributes[ :href ].split( '/' ).last
end end


def inputs def inputs
Expand Down
20 changes: 20 additions & 0 deletions lib/rightstuff/base.rb
@@ -1,10 +1,19 @@
require 'rubygems' if RUBY_VERSION < '1.9' require 'rubygems' if RUBY_VERSION < '1.9'
require 'active_support/all'
require 'nokogiri' require 'nokogiri'


module Rightstuff module Rightstuff


class Base class Base


def self.type
name.demodulize.underscore
end

def self.collection_xpath
"/#{ type.pluralize }/#{ type }"
end

def self.load_collection( client, doc ) def self.load_collection( client, doc )
doc.xpath( self.collection_xpath ).collect do | item | doc.xpath( self.collection_xpath ).collect do | item |
self.new( client, item ) self.new( client, item )
Expand All @@ -31,6 +40,17 @@ def initialize( client, item )
@attributes = Base.extract_attributes( item ) @attributes = Base.extract_attributes( item )
end end


def id
@attributes[ :href ].split( '/' ).last
end

def method_missing( name , *args, &block )
result = super
return result unless result.nil?
return nil if ! active?
return @attributes[ name ]
end

def method_missing( name, *args, &block ) def method_missing( name, *args, &block )
return @attributes[ name ] return @attributes[ name ]
end end
Expand Down
20 changes: 4 additions & 16 deletions lib/rightstuff/server.rb
@@ -1,5 +1,5 @@
module Rightstuff module Rightstuff

# http://support.rightscale.com/15-References/RightScale_API_Reference_Guide/02-Management/02-Servers
class Server < Base class Server < Base


attr_reader :attributes attr_reader :attributes
Expand All @@ -10,20 +10,8 @@ def initialize( client, item )
super super
end end


def self.collection_xpath def active?
'/servers/server' @attributes[ :state ] == 'operational'
end

def method_missing( name , *args, &block )
result = super
return result unless result.nil?
return nil unless @attributes[ :state ] == 'operational'
settings unless @settings
return @attributes[ name ]
end

def id
@attributes[ :href ].split( '/' ).last
end end


def inputs def inputs
Expand All @@ -34,7 +22,7 @@ def inputs


def settings def settings
return @settings if @settings return @settings if @settings
doc = @client.get_rest( 'servers/' + id + '/settings' ) doc = @client.get_rest( "#{ type.pluralize }/#{ id }/settings" )
xml = Nokogiri::XML( doc ) xml = Nokogiri::XML( doc )
@settings = Base.extract_attributes( xml.children ) @settings = Base.extract_attributes( xml.children )
@attributes.merge!( @settings ) @attributes.merge!( @settings )
Expand Down
17 changes: 3 additions & 14 deletions lib/rightstuff/server_array.rb
Expand Up @@ -10,19 +10,8 @@ def initialize( client, item )
super super
end end


def self.collection_xpath def active?
'/server-arrays/server-array' @attributes[ :state ] != 'stopped'
end

def method_missing( name , *args, &block )
result = super
return result unless result.nil?
return nil if @attributes[ :state ] == 'stopped'
return @attributes[ name ]
end

def id
@attributes[ :href ].split( '/' ).last
end end


def inputs def inputs
Expand All @@ -33,7 +22,7 @@ def inputs


def instances def instances
return @instances if @instances return @instances if @instances
body = Nokogiri::XML( @client.get_rest( "server_arrays/#{id}/instances" ) ) body = Nokogiri::XML( @client.get_rest( "#{ type.pluralize }/#{ id }/instances" ) )
@instances = Rightstuff::ArrayInstance.load_collection( @client, body ) @instances = Rightstuff::ArrayInstance.load_collection( @client, body )
end end
end end
Expand Down
1 change: 1 addition & 0 deletions rightstuff.gemspec
Expand Up @@ -28,6 +28,7 @@ Gem::Specification.new do |s|
s.executables += EXECUTABLE_FILENAMES s.executables += EXECUTABLE_FILENAMES
s.require_paths = [ 'lib' ] s.require_paths = [ 'lib' ]
s.add_dependency( 'nokogiri', '>= 1.4.3.1' ) s.add_dependency( 'nokogiri', '>= 1.4.3.1' )
s.add_dependency( 'activesupport', '>= 2.3.2' )


s.has_rdoc = true s.has_rdoc = true
s.rdoc_options += RDOC_OPTS s.rdoc_options += RDOC_OPTS
Expand Down

0 comments on commit 738a271

Please sign in to comment.