Class: Padiddler::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/padiddler/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



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

Parameters:

  • Symbol

    name The name of the output



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

Parameters:

  • Array

    outputs an array of strings or symbols that correspond to instance variable names



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

Parameters:

  • Hash

    outputs Each pair should have the format :new_name => :original_name



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

#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



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