Module: Darthjee::CoreExt::Class
- Included in:
- Class
- Defined in:
- lib/darthjee/core_ext/class.rb
Instance Method Summary collapse
-
#default_reader(name, value) ⇒ ::Symbol
Creates a method that will act as reader with default value.
-
#default_readers(*names, value) ⇒ ::Array<::Symbol>
Creates methods that will act as reader with default value.
-
#default_value(name, value) ⇒ ::Symbol
Adds a method that will return a default value.
-
#default_values(*names, value) ⇒ ::Array<::Symbol>
Adds methods that will return a default value.
Instance Method Details
#default_reader(name, value) ⇒ ::Symbol
Creates a method that will act as reader with default value
The method will be a reader, but when no value was defined for the instance variable, it will return a default
132 133 134 135 136 137 |
# File 'lib/darthjee/core_ext/class.rb', line 132 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) ⇒ ::Array<::Symbol>
Creates methods that will act as reader with default value
The methods will be readers, but when no value was defined for the instance variable, it will return a default
187 188 189 190 191 |
# File 'lib/darthjee/core_ext/class.rb', line 187 def default_readers(*names, value) names.map do |name| default_reader(name, value) end end |
#default_value(name, value) ⇒ ::Symbol
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
37 38 39 |
# File 'lib/darthjee/core_ext/class.rb', line 37 def default_value(name, value) define_method(name) { |*_| value } end |
#default_values(*names, value) ⇒ ::Array<::Symbol>
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
84 85 86 87 88 |
# File 'lib/darthjee/core_ext/class.rb', line 84 def default_values(*names, value) names.map do |name| default_value(name, value) end end |