Class: Padiddler::Base
- Inherits:
-
Object
- Object
- Padiddler::Base
- Defined in:
- lib/padiddler/base.rb
Class Method Summary collapse
-
.add(name, &block) ⇒ Object
adds a new output that is not directly related to an instance variable here to keep things consistent.
-
.keep(*outputs) ⇒ Object
defines which attributes should be used in the output without changing their values.
-
.rename(outputs) ⇒ Object
defines outputs whose names do not match the name of their instance variable.
Instance Method Summary collapse
-
#diddle ⇒ Object
gets a list of public methods, assigns their name and value to a hash, and returns ignores private methods and methods that require arguments.
-
#initialize(input = {}) ⇒ Base
constructor
Turns a hash into instance variables based on top level keys if the top level key is pointing to a hash, the value is not changed.
Constructor Details
#initialize(input = {}) ⇒ Base
Turns a hash into instance variables based on top level keys if the top level key is pointing to a hash, the value is not changed
51 52 53 54 55 |
# File 'lib/padiddler/base.rb', line 51 def initialize(input = {}) input.each_pair do |key, value| instance_variable_set(self.class.send(:instance_variable_name, key), value) end end |
Class Method Details
.add(name, &block) ⇒ Object
adds a new output that is not directly related to an instance variable here to keep things consistent
note that this functionality can be duplicated by just defining methods
29 30 31 |
# File 'lib/padiddler/base.rb', line 29 def add(name, &block) define_method(name.to_sym) { yield block } end |
.keep(*outputs) ⇒ Object
defines which attributes should be used in the output without changing their values
8 9 10 11 12 |
# File 'lib/padiddler/base.rb', line 8 def keep(*outputs) outputs.each do |name| instance_variable_accessor(name, name) end end |
.rename(outputs) ⇒ Object
defines outputs whose names do not match the name of their instance variable
17 18 19 20 21 |
# File 'lib/padiddler/base.rb', line 17 def rename(outputs) outputs.each_pair do |public_name, instance_var_name| instance_variable_accessor(instance_var_name, public_name) end end |
Instance Method Details
#diddle ⇒ Object
gets a list of public methods, assigns their name and value to a hash, and returns ignores private methods and methods that require arguments
59 60 61 62 63 64 65 66 |
# File 'lib/padiddler/base.rb', line 59 def diddle output = {} output_methods = public_methods(false).select { |m| method(m).arity == 0 && m != :diddle } output_methods.each do |name| output[name] = send(name) end output end |