rules0.rb
config = Configuration.new
config.add_item(Item.new(:secure_air_vent))
config.add_item(Item.new(:acid_bath))
config[:acid_bath].add_usage(Electricity.new(12))
acid = Acid.new
config[:acid_bath].add_usage(acid)
acid.type = :hcl
acid.grade = 5
config.add_item(Item.new(:camera))
config[:camera].add_usage(Electricity.new(1))
config.add_item(Item.new(:small_power_plant))
config[:small_power_plant].add_provision(Electricity.new(11))
config[:small_power_plant].add_dependency(config[:secure_air_vent])
return config
rules1.rb
#using a 'reasonable' mix of method chaining and parameters
item :secure_air_vent
item(:acid_bath).
uses(acid.
type(:hcl).
grade(5)).
uses(electricity(12))
item(:camera).uses(electricity(1))
item(:small_power_plant).
provides(electricity(11)).
depends_on(:secure_air_vent)
rules2.rb
#using method chaining as much as reasonable
ConfigurationBuilder.
item(:secure_air_vent).
item(:acid_bath).
uses.acid.
type(:hcl).
grade(5).
uses.electricity(12).
item(:camera).uses.electricity(1).
item(:small_power_plant).
provides.electricity(11).
depends_on(:secure_air_vent)
rules3.rb
# Using explicit receivers
ConfigurationBuilder.start do |config|
config.item :secure_air_vent
config.item(:acid_bath) do |item|
item.uses(Resources.acid) do |acid|
acid.type = :hcl
acid.grade = 5
end
item.uses(Resources.electricity(12))
end
config.item(:camera) do |item|
item.uses(Resources.electricity(1))
end
config.item(:small_power_plant) do |item|
item.provides(Resources.electricity(11))
item.depends_on(:secure_air_vent)
end
end
rules4.rb
#Using hashes to encode most data but not going all the way
item :secure_air_vent
item :acid_bath,
:uses => [acid(:type => :hcl,
:grade => 5) ,
electricity(12)]
item :camera,
:uses => electricity(1)
item :small_power_plant,
:provides => electricity(11),
:depends_on => :secure_air_vent
rules5.rb
#Using hash based literals to encode everything
item :secure_air_vent
item :acid_bath,
:uses => [
[:acid, {:type => :hcl, :grade => 5}],
[:electricity, 12]
]
item :camera,
:uses => [:electricity, 5]
item :small_power_plant,
:provides => [:electricity, 11],
:depends_on => :secure_air_vent
rules6.rb
# using lists only. Greenspun would love this ;-)
[
[:item, :secure_air_vent],
[:item, :acid_bath,
[:uses,
[:acid,
[:type, :hcl],
[:grade, 5]],
[:electricity, 12]]],
[:item, :camera,
[:uses, [:electricity, 5]]],
[:item, :small_power_plant,
[:provides, [:electricity, 11]],
[:depends_on, :secure_air_vent]]]
# interesting note here to rely on tail rather than more nested lists
# eg use [:depends_on, :secure_air_vent, :something_else] rather than
# [:depends_on, [:secure_air_vent, :something_else]]
rules7.rb
# using global functions with no identifiers
item(:secure_air_vent)
item(:acid_bath)
uses(:acid_bath, acid(:acid_bath_acid))
acid_type(:acid_bath_acid, :hcl)
acid_grade(:acid_bath_acid, 5)
uses(:acid_bath, electricity(12))
item(:camera)
uses(:camera, electricity(1))
item(:small_power_plant)
provides(:small_power_plant, electricity(11))
depends(:small_power_plant, :secure_air_vent)
rules8.rb
#global functions again, but using parse state
item(:secure_air_vent)
item(:acid_bath)
uses(acid)
acid_type(:hcl)
acid_grade(5)
uses(electricity(12))
item(:camera)
uses(electricity(1))
item(:small_power_plant)
provides(electricity(11))
depends(:secure_air_vent)
rules9.rb
#global functions again eliminating all parens
item :secure_air_vent
item :acid_bath
uses acid
acid_type :hcl
acid_grade 5
uses electricity
electricity_power 12
item :camera
uses electricity
electricity_power 1
item :small_power_plant
provides electricity
electricity_power 11
depends :secure_air_vent
rules10.rb
# using a single method to introduce objects with method chaining
load_configuration.
item(:secure_air_vent).
item(:acid_bath).
uses.acid.
type(:hcl).
grade(5).
uses.electricity(12).
item(:camera).uses.electricity(1).
item(:small_power_plant).
provides.electricity(11).
depends_on(:secure_air_vent)
rules11.rb
# Using class factories and method chaining
Configuration.item(:secure_air_vent)
Configuration.item(:acid_bath).
uses(Resources.acid.
set_type(:hcl).
set_grade(5)).
uses(Resources.electricity(12))
Configuration.item(:camera).uses(Resources.electricity(1))
Configuration.item(:small_power_plant).
provides(Resources.electricity(11)).
depends_on(:secure_air_vent)
#maybe try multi-arg Configuration.load(Item.new.... Item.new...)
rules12.rb
# Using the same rules as case 11 but to be processed using expression
# builder
Configuration.item(:secure_air_vent)
Configuration.item(:acid_bath).
uses(Resources.acid.
set_type(:hcl).
set_grade(5)).
uses(Resources.electricity(12))
Configuration.item(:camera).uses(Resources.electricity(1))
Configuration.item(:small_power_plant).
provides(Resources.electricity(11)).
depends_on(:secure_air_vent)
#maybe try multi-arg Configuration.load(Item.new.... Item.new...)
rules13.rb
# using a single builder object with class methods and chaining
ConfigurationBuilder.
item(:secure_air_vent).
item(:acid_bath).
uses(Resources.acid.
type(:hcl).
grade(5)).
uses(Resources.electricity(12)).
item(:camera).uses(Resources.electricity(1)).
item(:small_power_plant).
provides(Resources.electricity(11)).
depends_on(:secure_air_vent)
rules14.rb
# using an expression builder model with the same config code as 13
ConfigurationBuilder.
item(:secure_air_vent).
item(:acid_bath).
uses(Resources.acid.
type(:hcl).
grade(5)).
uses(Resources.electricity(12)).
item(:camera).uses(Resources.electricity(1)).
item(:small_power_plant).
provides(Resources.electricity(11)).
depends_on(:secure_air_vent)
rules15.rb
# using method chaining the same as case 2 but with a model expression
# builder rather than a single object
ConfigurationBuilder.
item(:secure_air_vent).
item(:acid_bath).
uses.acid.
type(:hcl).
grade(5).
uses.electricity(12).
item(:camera).uses.electricity(1).
item(:small_power_plant).
provides.electricity(11).
depends_on(:secure_air_vent)
rules16.rb
# taking the example from case 3 and doing it with temps instead of
# closures
config = ConfigurationBuilder.new
config.item :secure_air_vent
item = config.item(:acid_bath)
acid = Resources.acid
item.uses(acid)
acid.type = :hcl
acid.grade = 5
item.uses(Resources.electricity(12))
item = config.item(:camera)
item.uses(Resources.electricity(1))
item = config.item(:small_power_plant)
item.provides(Resources.electricity(11))
item.depends_on(:secure_air_vent)
rules17.rb
# Using subclass scoping and method chaining
class PrimaryConfigurationRules < ConfigurationBuilder
def run
item(:secure_air_vent)
item(:acid_bath).
uses(acid.
type(:hcl).
grade(5)).
uses(electricity(12))
item(:camera).uses(electricity(1))
item(:small_power_plant).
provides(electricity(11)).
depends_on(:secure_air_vent)
end
end
rules18.rb
# Using closures with instance_eval
item :secure_air_vent
item(:acid_bath) do
uses(acid) do
type :hcl
grade 5
end
uses(electricity(12))
end
item(:camera) do
uses(electricity(1))
end
item(:small_power_plant) do
provides(electricity(11))
depends_on(:secure_air_vent)
end
rules19.rb
#using literal map for acid on a chaining base
item :secure_air_vent
item(:acid_bath).
uses(acid(:type => :hcl, :grade => 5)).
uses(electricity(12))
item(:camera).uses(electricity(1))
item(:small_power_plant).
provides(electricity(11)).
depends_on(:secure_air_vent)
rules20.rb
# Using map for acid on an instance_eval closure base
item :secure_air_vent
item(:acid_bath) do
uses(acid(:type => :hcl, :grade => 5))
uses(electricity(12))
end
item(:camera) do
uses(electricity(1))
end
item(:small_power_plant) do
provides(electricity(11))
depends_on(:secure_air_vent)
end
rules21.rb
#Using hashes to encode most data but not going all the way
item :secure_air_vent
item :acid_bath,
[:uses,
acid(:type => :hcl, :grade => 5),
electricity(12)]
item :camera,
[:uses, electricity(1)]
item :small_power_plant,
[:provides, electricity(11)],
[:depends_on, :secure_air_vent]
rules22.rb
#Using hash based literals to encode everything
{:items => [
{:id => :secure_air_vent},
{:id => :acid_bath,
:uses => [
[:acid, {:type => :hcl, :grade => 5}],
[:electricity, 12]]},
{:id => :camera,
:uses => [:electricity, 5]},
{:id => :small_power_plant,
:provides => [:electricity, 11],
:depends_on => :secure_air_vent}
]}