class Zoop::ZoopObject

Constants

RESOURCES

Attributes

attributes[R]

Public Class Methods

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

Protected Class Methods

capitalize_name(name) click to toggle source
# File lib/zoop/object.rb, line 131
def capitalize_name(name)
  name.gsub(/(\A\w|\_\w)/){ |str| str.gsub('_', '').upcase }
end
convert(response) click to toggle source
# File lib/zoop/object.rb, line 108
def convert(response)
  case response
  when Array
    response.map{ |i| convert i }
  when Hash
    resource_class_for(response['resource']).new(response)
  else
    response
  end
end
resource_class_for(resource_name) click to toggle source
# File lib/zoop/object.rb, line 121
def resource_class_for(resource_name)
  return Zoop::ZoopObject if resource_name.nil?

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

Public Instance Methods

==(other) click to toggle source
# File lib/zoop/object.rb, line 24
def ==(other)
  self.class == other.class && id == other.id
end
[]=(key,value) click to toggle source
# File lib/zoop/object.rb, line 15
def []=(key,value)
  @attributes[key] = value
  @unsaved_attributes.add key
end
empty?() click to toggle source
# File lib/zoop/object.rb, line 20
def empty?
  @attributes.empty?
end
inspect()
Alias for: to_s
respond_to?(name, include_all = false) click to toggle source
Calls superclass method
# File lib/zoop/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/zoop/object.rb, line 34
def to_hash
  Hash[@attributes.map do |key, value|
    [ key, to_hash_value(value, :to_hash) ]
  end]
end
to_s() click to toggle source
# File lib/zoop/object.rb, line 46
def to_s
  attributes_str = ''
  (attributes.keys - ['id', 'object']).sort.each do |key|
    attributes_str += " \033[1;33m#{key}:\033[0m#{self[key].inspect}" unless self[key].nil?
  end
  "\033[1;31m#<#{self.class.name}:\033[0;32m#{id}#{attributes_str}\033[0m\033[0m\033[1;31m>\033[0m"
end
Also aliased as: inspect
unsaved_attributes() click to toggle source
# File lib/zoop/object.rb, line 28
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/zoop/object.rb, line 85
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] || self[name.to_sym]
    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/zoop/object.rb, line 72
def to_hash_value(value, type)
  case value
  when ZoopObject
    value.send type
  when Array
    value.map do |v|
      to_hash_value v, type
    end
  else
    value
  end
end
update_model(attributes) click to toggle source
# File lib/zoop/object.rb, line 57
def update_model(attributes)
  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] = ZoopObject.convert(value)
    @unsaved_attributes.delete key
  end
end