class Padiddler::Base

Public Class Methods

add(name) { |block| ... } click to toggle source

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

@param Symbol name The name of the output

# File lib/padiddler/base.rb, line 29
def add(name, &block)
  define_method(name.to_sym) { yield block }
end
keep(*outputs) click to toggle source

defines which attributes should be used in the output without changing their values

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

# File lib/padiddler/base.rb, line 8
def keep(*outputs)
  outputs.each do |name|
    instance_variable_accessor(name, name)
  end
end
new(input = {}) click to toggle source

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

# 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
rename(outputs) click to toggle source

defines outputs whose names do not match the name of their instance variable

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

# 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

Private Class Methods

instance_variable_accessor(name, var_name) click to toggle source

create a name accessor for an instance variable

# File lib/padiddler/base.rb, line 36
def instance_variable_accessor(name, var_name)
  instance_var = instance_variable_name(var_name) 
  define_method(name) { instance_variable_get(instance_var) }
end
instance_variable_name(name) click to toggle source

convenience method for turning a string into an instance variable symbol will take a string or symbol as an argument will add one @ to the front of the input

# File lib/padiddler/base.rb, line 44
def instance_variable_name(name)
  "@#{name.to_s}".to_sym
end

Public Instance Methods

diddle() click to toggle source

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

# 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