module SimplifyApi

SimplifyApi

Constants

VERSION

Public Class Methods

included(base) click to toggle source
# File lib/simplify_api/simplify_api.rb, line 5
def self.included(base)
  base.singleton_class.send(:attr_accessor, :attributes)
  base.attributes = {}
  base.extend(ClassMethods)
end
new(opts = {}) click to toggle source
# File lib/simplify_api/simplify_api.rb, line 33
def initialize(opts = {})
  opts.symbolize_keys!

  opts.each_pair do |key, value|
    expanded_value = process_value(key, value)
    create_and_set_instance_variable(key.to_s, expanded_value)
  end
end

Public Instance Methods

method_missing(method_name, *args, &block) click to toggle source

method_missing

Called in case an unexisting method is called.

To an assignment call it will create the instance variable.
To an getter of a specified attribute, return the default value.
Every other call will be passed to super.
Calls superclass method
# File lib/simplify_api/simplify_api.rb, line 73
def method_missing(method_name, *args, &block)
  if method_name.match(/(.*)\=$/)
    create_and_set_instance_variable($1.to_s, args[0])
  else
    return klass_attr.dig(method_name.to_sym, :params, :default) if klass_attr.key?(method_name.to_sym)

    super(method_name, args, block)
  end
end
respond_to_missing?(method_name, *args) click to toggle source

respond_to_mssing?

Called to check if an instance respond to a message.

It should respond to any defined attribute (getter & setter).
Every other type should be passed to super.
Calls superclass method
# File lib/simplify_api/simplify_api.rb, line 89
def respond_to_missing?(method_name, *args)
  return true if method_name.match(/(.*)\=$/)

  return true if klass_attr.key?(method_name.to_sym)

  super(method_name, args)
end
to_h() click to toggle source
# File lib/simplify_api/simplify_api.rb, line 42
def to_h
  h = {}
  instance_variables.each do |i|
    k = /\@(.*)/.match(i.to_s)[1].to_sym
    v = instance_variable_get(i)
    if v.class == Array then
      r = []
      v.each { |a| r << (a.respond_to?(:to_h) ? a.to_h : a) }
      if klass_attr[k][:params][:invisible] then
        h = r
      else
        h[k] = r
      end
    else
      h[k] = v.respond_to?(:to_h) ? v.to_h : v
    end
  end
  return h
end
to_json() click to toggle source
# File lib/simplify_api/simplify_api.rb, line 62
def to_json
  self.to_h.to_json
end
valid?() click to toggle source
# File lib/simplify_api/simplify_api.rb, line 97
def valid?
  klass_attr.each_pair do |key, value|
    puts "Key: #{key}"
    return false if value.dig(:params, :mandatory) & !instance_variable_defined?("@#{key.to_s}".to_sym)
  end
  true
end

Private Instance Methods

create_and_set_instance_variable(name, value) click to toggle source
# File lib/simplify_api/simplify_api.rb, line 107
def create_and_set_instance_variable(name, value)
  klass_attr[name.to_sym] = { name: name, type: value.class, params: { default: nil, mandatory: false }} unless klass_attr[name.to_sym]

  define_singleton_method("#{name}=") do |arg|
    raise ArgumentError, "Invalid value for #{name} => #{arg}" unless valid_value?(name, arg)

    instance_variable_set("@#{name}", arg)
  end

  define_singleton_method(name.to_s) do
    instance_variable_get("@#{name}")
  end

  send("#{name}=", value)
end
klass_attr() click to toggle source
# File lib/simplify_api/simplify_api.rb, line 149
def klass_attr
  self.class.attributes
end
process_value(key, value) click to toggle source
# File lib/simplify_api/simplify_api.rb, line 133
def process_value(key, value)
  case value
  when Hash
    value = klass_attr[key][:type].new(value)
  when Array
    value.collect! do |item|
      if klass_attr[key][:params].key?(:array_type)
        klass_attr[key][:params][:array_type].new(item)
      else
        item
      end
    end
  end
  value
end
valid_value?(name, value) click to toggle source
# File lib/simplify_api/simplify_api.rb, line 123
def valid_value?(name, value)
  return true if value.nil?
  return true unless klass_attr[name.to_sym][:params].key?(:values)

  valid_values = klass_attr[name.to_sym][:params][:values]
  return true if valid_values.include?(value)

  false
end