Permalink
Browse files

Updating resourcing DSL

  • Loading branch information...
1 parent f50be85 commit a9661ff27d74c774629fb399e3559e1a2141f713 Ari Lerner committed Oct 19, 2008
@@ -8077,7 +8077,7 @@ maybe_utc
write_event
write_events1
write_events
-install_prev
+install_pre
'-wait_nodes/2-fun-0-'
sync_nodes_optional
get_sync_optional_nodes
@@ -8616,7 +8616,7 @@ log_terms
'-table/2-lc$^0/1-0-'
'-table/2-fun-9-'
'-qlc_next/2-fun-0-'
-'-qlc_prev/2-fun-0-'
+'-qlc_pre/2-fun-0-'
'-qlc_select/1-fun-0-'
'-i/0-fun-0-'
'-hform/6-lc$^0/1-0-'
@@ -8650,7 +8650,7 @@ default
n_objects
traverse
qlc_select
-qlc_prev
+qlc_pre
qlc_next
is_unique_objects
is_sorted_key
@@ -8661,7 +8661,7 @@ post_fun
info_fun
lookup_fun
first_next
-last_prev
+last_pre
tabfile_info
cannot_create_table
create_tab
@@ -11470,7 +11470,7 @@ safe_fixtable
rename
insert_new
insert
-prev
+pre
is_key
member
match_object
@@ -15,8 +15,8 @@ def runnable(quiet=true)
def nice_runnable(quiet=true)
self.flatten.reject{|e| (e.nil? || e.empty?) }.join(" \n ").chomp.nice_runnable(quiet)
end
- def to_string(prev="")
- map {|a| a.to_string(prev)}.join("\n")
+ def to_string(pre="")
+ map {|a| a.to_string(pre)}.join("\n")
end
def get_named(str="")
map {|a| a.name == str ? a : nil }.reject {|a| a.nil? }
View
@@ -12,14 +12,28 @@ def extract!(&block)
o.keys.each {|k| self.delete(k) }
o
end
+ def append(other_hash)
+ returning Hash.new do |h|
+ h.merge!(self)
+ other_hash.each do |k,v|
+ h[k] = has_key?(k) ? [self[k], v].flatten : v
+ end
+ end
+ end
+ def append!(other_hash)
+ other_hash.each do |k,v|
+ self[k] = has_key?(k) ? [self[k], v].flatten : v
+ end
+ self
+ end
def safe_merge(other_hash)
merge(other_hash.delete_if {|k,v| has_key?(k) })
end
def safe_merge!(other_hash)
merge!(other_hash.delete_if {|k,v| has_key?(k) && !v.nil? })
end
- def flush_out(prev="", post="")
- map {|k,v| "#{prev}#{k} => #{v.to_option_string}#{post}"}
+ def flush_out(pre="", post="")
+ map {|k,v| "#{pre}#{k} => #{v.to_option_string}#{post}"}
end
def to_os
m={}
@@ -1,6 +1,6 @@
class Symbol
- def to_string(prev="")
- "#{prev}#{self.to_s}"
+ def to_string(pre="")
+ "#{pre}#{self.to_s}"
end
def sanitize
self.to_s.sanitize
@@ -7,10 +7,10 @@ def pool_describe(options={})
print v.pretty_print
end
else
- prev = "\t"
+ pre = "\t"
clouds.each do |name, cl|
- puts cl.pretty_name(prev*2, cl)
- puts cl.pretty_options(prev*2, cl) #cl.pretty_print("#{prev}\t")
+ puts cl.pretty_name(pre*2, cl)
+ puts cl.pretty_options(pre*2, cl) #cl.pretty_print("#{pre}\t")
end
end
@@ -1,38 +1,38 @@
module PoolParty
module PrettyPrinter
- def pretty_print(prev="\t")
+ def pretty_print(pre="\t")
returning Array.new do |out|
- out << pretty_name(prev, self)
- out << pretty_options(prev, self)
+ out << pretty_name(pre, self)
+ out << pretty_options(pre, self)
if self.respond_to?(:clouds)
clouds.each do |name, cl|
- out << pretty_name(prev*2, cl)
- out << pretty_options(prev*2, cl) #cl.pretty_print("#{prev}\t")
+ out << pretty_name(pre*2, cl)
+ out << pretty_options(pre*2, cl) #cl.pretty_print("#{pre}\t")
end
end
if self.respond_to?(:plugins)
- out << "#{prev}\t\tPlugins"
- out << "#{prev}\t\t" + plugins.map {|a| a}.join("\n")
+ out << "#{pre}\t\tPlugins"
+ out << "#{pre}\t\t" + plugins.map {|a| a}.join("\n")
end
end.join("\n")
end
# Gather options on the object
# Do not print if the option is nil or empty.
# Also, don't show the option if the option is empty or the default option on the cloud
- def pretty_options(prev, o)
+ def pretty_options(pre, o)
return "" unless o.respond_to?(:options)
print_options = (o.respond_to?(:parent) && o.parent && o.parent.respond_to?(:options)) ?
(o.options.delete_if {|k,v| o.parent.options.has_key?(k) && o.parent.options[k] == o.options[k] && !o.options[k].nil? } ) :
o.options
print_options = print_options.map {|k,v| [k, o.send(k.to_sym).to_s] }.inject({}) { |r,e| r[e[0]] = e[1] unless o.class.default_options[e[0]] == e[1] || e[1].nil? || e[1].empty?; r }
- print_options.flush_out("#{prev}\t")
+ print_options.flush_out("#{pre}\t")
end
- def pretty_name(prev, o)
- "#{prev}#{o.class.to_s.top_level_class.capitalize}: #{o.name if o.respond_to?(:name)}"
+ def pretty_name(pre, o)
+ "#{pre}#{o.class.to_s.top_level_class.capitalize}: #{o.name if o.respond_to?(:name)}"
end
end
@@ -6,13 +6,13 @@ def require(str="")
options[:require]
end
def requires(str="")
- options.merge!(:require => str)
+ options.append!(:require => str)
end
def ensures(str="running")
if %w(absent running).map {|a| self.send a.to_sym}.include?(str)
str == "absent" ? is_absent : is_present
else
- options.merge!(:ensure => str)
+ options.append!(:ensure => str)
end
str
end
@@ -22,11 +22,11 @@ def call_function(str, opts={}, &block)
class CallFunction < Resource
def initialize(str="", opts={}, parent=self, &block)
@str = str
- super(opts, parent, &block)
+ # super(opts, parent, &block)
end
- def to_string(prev="")
+ def to_string(pre="")
returning Array.new do |arr|
- arr << "#{prev}#{@str}"
+ arr << "#{pre}#{@str}"
end.join("\n")
end
end
@@ -42,10 +42,10 @@ def self.inherited(subclass)
super(subclass)
end
- def to_string(prev="")
+ def to_string(pre="")
returning Array.new do |output|
- output << "#{prev} # Custom Functions\n"
- output << self.class.custom_functions_to_string(prev)
+ output << "#{pre} # Custom Functions\n"
+ output << self.class.custom_functions_to_string(pre)
end.join("\n")
end
end
@@ -17,13 +17,13 @@ def resource(type=:file)
resources[type] ||= []
end
- def add_resource(type, opts={}, parent=self, &block)
- resource = get_resource(type, opts[:name])
+ def add_resource(type, opts={}, parent=self, &block)
+ resource = parent.get_resource(type, opts[:name])
if resource
return resource
else
returning "PoolParty::Resources::#{type.to_s.camelize}".classify.constantize.new(opts, parent, &block) do |o|
- resource(type) << o
+ parent.resource(type) << o
end
end
end
@@ -37,7 +37,7 @@ def reset_resources!
@resources = nil
end
- def resources_string(prev="")
+ def resources_string(pre="")
returning Array.new do |output|
output << resources_string_from_resources(resources)
end.join("\n")
@@ -53,7 +53,7 @@ class Resource
include CloudResourcer
include Configurable
- extend PoolParty::Resources
+ # extend PoolParty::Resources
include PoolParty::Resources
def self.inherited(subclass)
@@ -90,12 +90,19 @@ def self.available_resource_methods
# Finally, it uses the parent's options as the lowest priority
def initialize(opts={}, parent=self, &block)
# Take the options of the parents
- @parent = parent
+ set_resource_parent(parent)
set_vars_from_options(opts) unless opts.empty?
self.instance_eval &block if block
loaded(opts)
end
+ def set_resource_parent(parent=nil)
+ if parent
+ @parent = parent
+ requires parent.to_s if parent.is_a?(PoolParty::Resources::Resource) && parent != self
+ end
+ end
+
# Stub, so you can create virtual resources
# This is called after the resource is initialized
# with the options given to it in the init-block
@@ -123,11 +130,11 @@ def custom_function(str)
self.class.custom_functions << str
end
- def self.custom_functions_to_string(prev="")
+ def self.custom_functions_to_string(pre="")
returning Array.new do |output|
PoolParty::Resources.available_custom_resources.each do |resource|
resource.custom_functions.each do |func|
- output << "#{prev*2}#{func}"
+ output << "#{pre*2}#{func}"
end
end
end.join("\n")
@@ -163,7 +170,7 @@ def get_modified_options
# Generic to_s
# Most Resources won't need to extend this
- def to_string(prev="")
+ def to_string(pre="")
opts = get_modified_options
returning Array.new do |output|
unless cancelled?
@@ -176,10 +183,10 @@ def to_string(prev="")
end
unless virtual_resource?
- output << "#{prev}#{class_type_name} {"
- output << "#{prev}\"#{self.key}\":"
- output << opts.flush_out("#{prev*2}").join(",\n")
- output << "#{prev}}"
+ output << "#{pre}#{class_type_name} {"
+ output << "#{pre}\"#{self.key}\":"
+ output << opts.flush_out("#{pre*2}").join(",\n")
+ output << "#{pre}}"
end
output << @poststring || ""
@@ -43,19 +43,19 @@ def name(*args)
end
- def resources_string_from_resources(resources, prev="\t")
+ def resources_string_from_resources(resources, pre="\t")
@variables = resources.extract! {|name,resource| name == :variable}
returning Array.new do |str|
unless @variables.empty?
str << "\n# Variables \n"
@variables.each do |name, variable|
- str << variable.to_string("#{prev}")
+ str << variable.to_string("#{pre}")
end
end
resources.each do |type, resource|
- str << "\n#{prev*2}# #{type}\n"
- str << resource.to_string("#{prev*2}")
+ str << "\n#{pre*2}# #{type}\n"
+ str << resource.to_string("#{pre*2}")
end
end.join("\n")
end
@@ -25,7 +25,7 @@ def virtual_resource?
true
end
- def to_string(prev="")
+ def to_string(pre="")
returning Array.new do |output|
output << "# #{name.sanitize}"
output << "case #{attribute} {"
@@ -8,7 +8,7 @@ class Variable < Resource
:value => ""
})
- def to_string(prev="\t")
+ def to_string(pre="\t")
"$#{name} = #{value_string}"
end
View
@@ -1363,7 +1363,7 @@ def install_dir_data(rel)
def install_dir_conf(rel)
# FIXME: should not remove current config files
- # (rename previous file to .old/.org)
+ # (rename preious file to .old/.org)
install_files targetfiles(), "#{config('sysconfdir')}/#{rel}", 0644
end
@@ -1542,12 +1542,12 @@ def dive_into(rel)
dir = File.basename(rel)
Dir.mkdir dir unless File.dir?(dir)
- prevdir = Dir.pwd
+ predir = Dir.pwd
Dir.chdir dir
$stderr.puts '---> ' + rel if verbose?
@currdir = rel
yield
- Dir.chdir prevdir
+ Dir.chdir predir
$stderr.puts '<--- ' + rel if verbose?
@currdir = File.dirname(rel)
end
@@ -30,7 +30,7 @@
it "should be able to flush out into a string into an array" do
@a.flush_out.should == ["a => '10'","b => '20'","c => '30'"]
end
- it "should be able to flush out with prev and posts" do
+ it "should be able to flush out with pre and posts" do
@a.flush_out("hi", "ho").should == ["hia => '10'ho","hib => '20'ho","hic => '30'ho"]
end
describe "select" do
@@ -60,4 +60,22 @@
}.should_not raise_error
end
end
+ describe "append" do
+ before(:each) do
+ @hash = {:game => "token", :required => "for_play"}
+ end
+ it "should not destroy an option salready on the hash" do
+ @hash.append(:required => "to_play")[:game].should == "token"
+ end
+ it "should change the entry to an array if it already exists" do
+ @hash.append(:game => "coin")[:game].class.should == Array
+ end
+ it "should append the other hash's key to the end of the array" do
+ @hash.append(:game => "coin")[:game][-1].should == "coin"
+ end
+ it "should change the hash with the append(bang) option" do
+ @hash.append!(:game => "coin")
+ @hash[:game][-1].should == "coin"
+ end
+ end
end
Oops, something went wrong.

0 comments on commit a9661ff

Please sign in to comment.