Class: TheDiddler::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/the_diddler/base.rb

Class Method Summary collapse

Instance Method Summary collapse

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



19
20
21
22
23
# File 'lib/the_diddler/base.rb', line 19

def initialize(input = {})
  input.each_pair do |key, value|
    instance_variable_set(instance_variable_name(key), value)
  end
end

Class Method Details

.attr_outputs(*outputs) ⇒ Object Also known as: attr_output

defines which attributes should be used to generated the diddled output note that attr_reader or attr_accessor can be used for the same purpose maybe I should have just used those



8
9
10
11
12
13
# File 'lib/the_diddler/base.rb', line 8

def attr_outputs(*outputs)
  outputs.each do |name|
    name = name.to_sym
    define_method(name) { instance_variable_get(instance_variable_name(name)) }
  end
end

Instance Method Details

#diddleObject

gets a list of public methods, assigns their name and value to a hash, and returns ignores private methods and methods that require arguments



27
28
29
30
31
32
33
34
# File 'lib/the_diddler/base.rb', line 27

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