Module: InstanceOptions
- Defined in:
- lib/instanceoptions.rb
Overview
Instance utils, creates accessor and merge options arguments Examples: class Foo
include InstanceOptions
end f=Foo.new f.attr_accessor(:test) f.test=123 p f.test #=> ‘123’ f.bar=456 #=> raise error ‘undefined method’ Merge options class Bar
DEFAULT_OPTIONS={:name => 'my name', :city => 'my city', :address => 'my address'}
include InstanceOptions
def initialize(={})
@options= #The name @options is mandatory
(DEFAULT_OPTIONS) #call method instance_options with hash
end
end b=Bar.new b.inspect #=> #Bar:0xb79e9730 @city=“my city”, @address=“my address”, @options={}, @name=“my name” b.city #=> ‘my city’ b.city=‘other’ b.city #=> ‘other’
b=Bar.new :name => ‘Shairon’, :city => ‘Gyn’ b.inspect #=> #Bar:0xb79e4ba4 @city=“Gyn”, @address=“my address”, @name=“Shairon”, @options=:city=>“Gyn” b.city #=> ‘Gyn’
b=Bar.new :name => ‘Tom’, :address => ‘can not say!’, :country => ‘Japan’ # NoMethodError: undefined method ‘country=’ for Bar:0xb79de7f4
b=Bar.new b.attr_accessor(:country) b.country=‘Brazil’ b.country #=> ‘Brazil’ b.inspect #=> #Bar:0xb79dbe28 @city=“my city”, @country=“Brazil”, @address=“my address”, @options={}, @name=“my name”
Instance Method Summary collapse
-
#attr_accessor(*args) ⇒ Object
Equivalent to attr_reader and attr_writer.
-
#attr_reader(*args) ⇒ Object
Creates method and instance variable for read.
-
#attr_writer(*args) ⇒ Object
Creates method and instance variable for write.
-
#initialize_default_options(options) ⇒ Object
:nodoc:.
-
#instance_options(default_options) ⇒ Object
Creates through of the hash methods accessors for each entries and set values.
-
#set_instance_options(options) ⇒ Object
:nodoc:.
Instance Method Details
#attr_accessor(*args) ⇒ Object
Equivalent to attr_reader and attr_writer
54 55 56 57 |
# File 'lib/instanceoptions.rb', line 54 def attr_accessor(*args) attr_reader(args) attr_writer(args) end |
#attr_reader(*args) ⇒ Object
Creates method and instance variable for read.
39 40 41 42 43 44 |
# File 'lib/instanceoptions.rb', line 39 def attr_reader(*args) sym_args=args_to_sym(args) sym_args.each do |value| self.instance_eval("def #{value}; @#{value};end;") end end |
#attr_writer(*args) ⇒ Object
Creates method and instance variable for write.
46 47 48 49 50 51 52 |
# File 'lib/instanceoptions.rb', line 46 def attr_writer(*args) sym_args=args_to_sym(args) sym_args.each do |value| self.instance_eval("def #{value}=(arg); @#{value}=arg;end;") end end |
#initialize_default_options(options) ⇒ Object
:nodoc:
65 66 67 68 69 70 71 |
# File 'lib/instanceoptions.rb', line 65 def () # :nodoc: .each do |k,v| attr_accessor(k) self.send(k.to_s+'=',v) end end |
#instance_options(default_options) ⇒ Object
Creates through of the hash methods accessors for each entries and set values
74 75 76 77 |
# File 'lib/instanceoptions.rb', line 74 def () () (@options) end |
#set_instance_options(options) ⇒ Object
:nodoc:
59 60 61 62 63 |
# File 'lib/instanceoptions.rb', line 59 def () # :nodoc: .each do |k,v| self.send(k.to_s+"=",v) end end |