class ResasKit::Resource

Public Class Methods

new(raw_data = {}) click to toggle source
# File lib/resas_kit/resource.rb, line 3
def initialize(raw_data = {})
  @attributes = {}

  raw_data.each do |key, value|
    @attributes[key.to_sym] = process_value(value)
  end

  eigenclass = class << self; self; end
  eigenclass.send(:define_accessors, raw_data.keys)
end

Private Class Methods

define_accessors(attributes) click to toggle source
# File lib/resas_kit/resource.rb, line 15
def define_accessors(attributes)
  attributes.each do |attribute|
    define_reader(attribute)
    define_writer(attribute)
    define_boolean_method(attribute)
  end
end
define_boolean_method(attribute) click to toggle source
# File lib/resas_kit/resource.rb, line 46
def define_boolean_method(attribute)
  method_name = "#{attribute}?"
  alias_method_name = "#{attribute.underscore}?"

  class_eval do
    define_method(method_name) do
      !!@attributes[attribute.to_sym]
    end

    alias_method(alias_method_name, method_name)
  end
end
define_reader(attribute) click to toggle source
# File lib/resas_kit/resource.rb, line 23
def define_reader(attribute)
  class_eval do
    define_method(attribute) do
      @attributes[attribute.to_sym]
    end

    alias_method(attribute.underscore, attribute)
  end
end
define_writer(attribute) click to toggle source
# File lib/resas_kit/resource.rb, line 33
def define_writer(attribute)
  method_name = "#{attribute}="
  alias_method_name = "#{attribute.underscore}="

  class_eval do
    define_method(method_name) do |value|
      @attributes[attribute.to_sym] = value
    end

    alias_method(alias_method_name, method_name)
  end
end

Private Instance Methods

process_value(value) click to toggle source
# File lib/resas_kit/resource.rb, line 69
def process_value(value)
  case value
  when Hash  then self.class.new(value)
  when Array then value.map { |v| process_value(v) }
  else value
  end
end