SpiceInit

Sometimes I want a simple way of initializing an object or setting class/module methods from a hash.

Instances

With SpiceInit to initialize an instance of some class you can use inspice instead of new

For example:

class Person;end

person = Person.inspice(:name => “Lucky”, :last_name => “Luke”, :is_cool? => true) p person # => #<Person:0xb93c @name=“Lucky”, @last_name=“Luke”, @is_cool=true> p person.name # => “Lucky” p person.last_name # => “Luke” p person.is_cool? # => true

If you want to track the spices added without going through methods you can pass true to the :track_spices option. person = Person.inspice(:name => “Lucky”, :last_name => “Luke”, :is_cool? => true, :track_spices => true) p person.spices_added # => [“is_cool?”, “is_cool=”, “last_name=”, “name”, “name=”, “last_name”]

You can also have some attributes defined already so if a block is given the new object will be in scope. class Person attr_accessor :smokes end

person = Person.inspice(:name => “Lucky”, :last_name => “Luke”, :is_cool? => true) do |ll| ll.smokes = 'cigarettes' end

p person # => #<Person:0x9eac @is_cool=true, @smokes=“cigarettes”, @name=“Lucky”, @last_name=“Luke”> p person.smokes # => “cigarettes” person.smokes = 'cigars' p person.smokes # => “cigars” You can also have an initialize method with required arguments. In that case, you should provide them before the hash. class Person attr_accessor :smokes, :kind def initialize(kind) @kind = kind # this will not run if you passed a block to :inspice if block_given? yield(self) end end end

person = Person.inspice('comic',:name => “Lucky”, :last_name => “Luke”, :is_cool? => true) do |ll| ll.smokes = 'cigarettes' end

p person # => <Person:0x9128 @kind=“comic”, @smokes=“cigarettes”, @name=“Lucky”, @last_name=“Luke”, @is_cool=true> Note: initialize is called before the block (if given to inspice) so block variable assignments will override any other assignment you are doing at initialize. class Person attr_accessor :smokes, :kind def initialize(kind=nil) @kind = kind @smokes = 'cigars' # this will not run if you passed a block to :inspice if block_given? yield(self) end end end

person = Person.inspice('drama',:name => “Lucky”, :last_name => “Luke”, :is_cool? => true) do |ll| ll.smokes = 'cigarettes' ll.kind = 'comic' end

p person # => #<Person:0xb4dc @last_name=“Luke”, @name=“Lucky”, @is_cool=true, @smokes=“cigarettes”, @kind=“comic”> person = Person.inspice('comic',:name => “Lucky”, :last_name => “Luke”, :is_cool? => true)

p person # => #<Person:0xad5c @last_name=“Luke”, @name=“Lucky”, @is_cool=true, @smokes=“cigars”, @kind=“comic”>

Sure, you can still instantiate with new person = Person.new('friendly') do |f| f.smokes = false end

p person.kind # => “friendly” p person.smokes # => false p person # => #<Person:0x88e0 @smokes=false, @kind=“friendly”>

When you pass a proc as a value you must call the key method as a proc also. person = Person.inspice('comic',:name => “Lucky”, :last_name => “Luke”, :says => Proc.new {|obj,str| “#{obj.name} says #str”}) p person.says[person,'hi there'] # or person.says.call(person,'hi there') # => “Lucky says hi there”

Another way of using inspice is with factory methods. class Person def self.new(name=nil) inspice :name => name end end

person = Person.new p person # => #<Person:0x1287034 @name=nil> p person.name # => nil person.name = 'Lucky' p person.name # => “Lucky” p person # => #<Person:0x1287034 @name=“Lucky”> person = Person.new('Tintin') p person # => #<Person:0x1286e54 @name=“Tintin”> p person.name # => “Tintin” person.name = 'Lucky' p person # => #<Person:0x1286e54 @name=“Lucky”> p person.name # => “Lucky”

Modules and Classes

For Classes and Modules you can use claspice or clasinit and modspice (they are method aliases for modinit) and the usage is similar to inspice but keys are defined as class methods and values as class variables. module LCD modspice :core_image => “Hardware Accelerated”, :main_display => true, :mirror => “off”, :online => true

class Display claspice :resolution => “1440x960”, :colors => “Millions”, :brightness => 50, :auto_adjust => true, :show_in_menu => true, :refresh_rate => “n/a” end end

p LCD.core_image # => “Hardware Accelerated” p LCD.main_display # => true p LCD.mirror # => “off” p LCD.online # => true p LCD.class_variables # => [“@@mirror”, “@@online”, “@@main_display”, “@@core_image”] p LCD::Display.resolution # => “1440x960” p LCD::Display.colors # => “Millions” p LCD::Display.brightness # => 50 p LCD::Display.auto_adjust # => true p LCD::Display.show_in_menu # => true p LCD::Display.refresh_rate # => “n/a” p LCD::Display.class_variables # => [“@@brightness”, “@@refresh_rate”, “@@show_in_menu”, “@@colors”, “@@auto_adjust”, “@@resolution”]

module LCD modspice :core_image => “Hardware Accelerated”, :main_display => true, :mirror => “off”, :online => true, :track_spices => true

class Display claspice :resolution => “1440x960”, :colors => “Millions”, :brightness => 50, :auto_adjust => true, :show_in_menu => true, :refresh_rate => “n/a”, :track_spices => true end end

p LCD.spices_added # => [“online=”, “online”, “core_image=”, “core_image”, “main_display=”, “main_display”, “mirror=”, “mirror”] p LCD::Display.spices_added # => [“brightness=”, “brightness”, “auto_adjust=”, “auto_adjust”, “show_in_menu=”, “show_in_menu”, # “refresh_rate=”, “refresh_rate”, “resolution=”, “resolution”, “colors=”, “colors”]

# can also be inline M = Module.new M.modspice :core_image => “Hardware Accelerated”, :main_display => true, :mirror => “off”, :online => true, :track_spices => true

p M.spices_added # => [“online=”, “online”, “core_image=”, “core_image”, “main_display=”, “main_display”, “mirror=”, “mirror”] p M.class_variables # => [“@@main_display”, “@@spices_added”, “@@core_image”, “@@mirror”, “@@online”] With classes and modules key methods with proc values are defined with optional arguments making the method call more natural. module Tiempo FORMATS = => “%m/%d/%y”, :long => “%A, %B %d, %Y” modspice :fecha => Proc.new {|time,key| time.strftime(FORMATS)}, :ahora => Proc.new Time.now end

p Tiempo.fecha(Time.now,:short) # => “11/14/10” p Tiempo.fecha(Time.now,:long) # => “Sunday, November 14, 2010” p Tiempo.ahora # => Sun Nov 14 02:56:19 -0200 2010

SpiceInited

If you need some keys to be readers and other accessors you can use inspiced and modspiced. For example: person = Person.inspiced(:readers => => “Lucky”, :last_name => “Luke”, :accessors => => true, :track_spices => true) p person # => #<Person:0x8f6ec @is_cool=true, @name=“Lucky”, @last_name=“Luke”> p [person.name,person.last_name,person.is_cool?] # => [“Lucky”, “Luke”, true] p person.spices_added # => [“name”, “last_name”, “is_cool=”, “is_cool?”] module LCD1 modspiced :readers => => “Hardware Accelerated”, :accessors => => true, :mirror => “off”, :online => true, :resolution => “1440x960”, :colors => “Millions”, :brightness => 50, :auto_adjust => true, :show_in_menu => true, :refresh_rate => “n/a”, :track_spices => true end p LCD.class_variables # [“@@auto_adjust”, “@@online”, “@@resolution”, “@@brightness”, “@@mirror”, “@@refresh_rate”, # “@@colors”, “@@main_display”, “@@show_in_menu”, “@@core_image”, “@@_spices_added”] p LCD.spices_added.sort # [“auto_adjust”, “auto_adjust=”, “brightness”, “brightness=”, “colors”, “colors=”, “core_image”, # “main_display”, “main_display=”, “mirror”, “mirror=”, “online”, “online=”, “refresh_rate”, # “refresh_rate=”, “resolution”, “resolution=”, “show_in_menu”, “show_in_menu=”]

License

Read LICENSE