class RestPki::RestPkiObject

Constants

RESOURCES

Attributes

attributes[R]

Public Class Methods

new(response = {}, object) click to toggle source
# File lib/rest_pki/object.rb, line 9
def initialize(response = {}, object)
  @attributes         = Hash.new
  @unsaved_attributes = Set.new
  update(response, object)
end

Protected Class Methods

capitalize_name(name) click to toggle source
# File lib/rest_pki/object.rb, line 120
def capitalize_name(name)
  name.gsub(/(\A\w|\_\w)/){ |str| str.gsub('_', '').upcase }
end
convert(response, object) click to toggle source
# File lib/rest_pki/object.rb, line 98
def convert(response, object)
  case response
  when Array
    response.map{ |i| convert(i, object) }
  when Hash
    resource_class_for(object).new(response, object)     #response['object']).new(response)
  else
    response
  end
end
resource_class_for(resource_name) click to toggle source
# File lib/rest_pki/object.rb, line 110
def resource_class_for(resource_name)
  return RestPki::RestPkiObject if resource_name.nil?

  if RESOURCES.include? resource_name.to_sym
    Object.const_get "RestPki::#{capitalize_name resource_name}"
  else
    RestPki::RestPkiObject
  end
end

Public Instance Methods

==(other) click to toggle source
# File lib/rest_pki/object.rb, line 30
def ==(other)
  self.class == other.class && id == other.id
end
[]=(key,value) click to toggle source
# File lib/rest_pki/object.rb, line 15
def []=(key,value)
  @attributes[key] = value
  @unsaved_attributes.add key
end
empty?() click to toggle source
# File lib/rest_pki/object.rb, line 26
def empty?
  @attributes.empty?
end
respond_to?(name, include_all = false) click to toggle source
Calls superclass method
# File lib/rest_pki/object.rb, line 40
def respond_to?(name, include_all = false)
  return true if name.to_s.end_with? '='

  @attributes.has_key?(name.to_s) || super
end
to_hash() click to toggle source
# File lib/rest_pki/object.rb, line 34
def to_hash
  Hash[@attributes.map do |key, value|
    [ key, to_hash_value(value, :to_hash) ]
  end]
end
unsaved_attributes() click to toggle source
# File lib/rest_pki/object.rb, line 20
def unsaved_attributes
  Hash[@unsaved_attributes.map do |key|
    [ key, to_hash_value(self[key], :unsaved_attributes) ]
  end]
end

Protected Instance Methods

method_missing(name, *args, &block) click to toggle source
Calls superclass method
# File lib/rest_pki/object.rb, line 75
def method_missing(name, *args, &block)
  name = name.to_s

  unless block_given?
    if name.end_with?('=') && args.size == 1
      attribute_name = name[0...-1]
      return self[attribute_name] = args[0]
    end

    if args.size == 0
      return self[name]
    end
  end

  if attributes.respond_to? name
    return attributes.public_send name, *args, &block
  end

  super name, *args, &block
end
to_hash_value(value, type) click to toggle source
# File lib/rest_pki/object.rb, line 62
def to_hash_value(value, type)
  case value
  when RestPkiObject
    value.send type
  when Array
    value.map do |v|
      to_hash_value v, type
    end
  else
    value
  end
end
update(attributes, object) click to toggle source
# File lib/rest_pki/object.rb, line 47
def update(attributes, object)
  removed_attributes = @attributes.keys - attributes.keys

  removed_attributes.each do |key|
    @attributes.delete key
  end

  attributes.each do |key, value|
    key = key.to_s

    @attributes[key] = RestPkiObject.convert(value, object)
    @unsaved_attributes.delete key
  end
end