Class: AttrArgsShorthand
- Inherits:
-
Object
- Object
- AttrArgsShorthand
- Defined in:
- lib/attr_args_shorthand.rb
Class Method Summary collapse
- .set(object, arguments, args = {}) ⇒ Object
- .set_args(object, args) ⇒ Object
- .set_attrs(clazz, args) ⇒ Object
Class Method Details
.set(object, arguments, args = {}) ⇒ Object
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/attr_args_shorthand.rb', line 2 def self.set(object, arguments, args = {}) arguments.each do |key, value| object.instance_variable_set("@#{key}", value) unless object.respond_to?(key) object.define_singleton_method(key) do instance_variable_get("@#{key}") end end unless object.respond_to?("#{key}=") object.define_singleton_method("#{key}=") do |value| instance_variable_set("@#{key}", value) end end end end |
.set_args(object, args) ⇒ Object
32 33 34 35 36 |
# File 'lib/attr_args_shorthand.rb', line 32 def self.set_args(object, args) args.each do |key, value| object.instance_variable_set("@#{key}", value) end end |
.set_attrs(clazz, args) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/attr_args_shorthand.rb', line 20 def self.set_attrs(clazz, args) args.each do |arg| clazz.__send__(:define_method, arg) do instance_variable_get("@#{arg}") end clazz.__send__(:define_method, "#{arg}=") do |value| instance_variable_set("@#{arg}", value) end end end |