Module: Darthjee::CoreExt::Class
- Included in:
- Class
- Defined in:
- lib/darthjee/core_ext/class.rb
Instance Method Summary collapse
-
#default_reader(name, value) ⇒ Object
Creates a method that will act as reader, but will return a default value when the instance variable was never set.
-
#default_readers(*names, value) ⇒ Object
Creates methods that will act as readers, but will return a default value when the instance variables ware never set.
-
#default_value(name, value) ⇒ Object
Adds a method that will return a default value.
-
#default_values(*names, value) ⇒ Object
Adds methods that will return a default value.
Instance Method Details
#default_reader(name, value) ⇒ Object
Creates a method that will act as reader, but will return a default value when the instance variable was never set
122 123 124 125 126 127 |
# File 'lib/darthjee/core_ext/class.rb', line 122 def default_reader(name, value) define_method(name) do return value unless instance_variable_defined?("@#{name}") instance_variable_get("@#{name}") end end |
#default_readers(*names, value) ⇒ Object
Creates methods that will act as readers, but will return a default value when the instance variables ware never set
169 170 171 172 173 |
# File 'lib/darthjee/core_ext/class.rb', line 169 def default_readers(*names, value) names.each do |name| default_reader(name, value) end end |
#default_value(name, value) ⇒ Object
Adds a method that will return a default value
the value is evaluated on class definition, meaning that everytime it is called it will be the same instance
36 37 38 |
# File 'lib/darthjee/core_ext/class.rb', line 36 def default_value(name, value) define_method(name) { |*_| value } end |
#default_values(*names, value) ⇒ Object
Adds methods that will return a default value
the value is evaluated on class definition, meaning that everytime any of them are called they will return the same instance of value
81 82 83 84 85 |
# File 'lib/darthjee/core_ext/class.rb', line 81 def default_values(*names, value) names.each do |name| default_value(name, value) end end |