class VLC360::Resource

Attributes

errors[R]

Public Class Methods

attributes(*attributes) click to toggle source
# File lib/vlc360/resource.rb, line 4
def attributes(*attributes)
  attr_accessor(*attributes)
  (@attributes ||= []).push(*attributes)
end
nested_resource(resource = nil) click to toggle source
# File lib/vlc360/resource.rb, line 14
def nested_resource(resource = nil)
  return @nested_resource unless resource

  attr_accessor(resource)
  @nested_resource = resource
end
nested_resources(*resources) click to toggle source
# File lib/vlc360/resource.rb, line 9
def nested_resources(*resources)
  attr_accessor(*resources)
  (@nested_resources ||= []).push(*resources)
end
new(attributes = {}) click to toggle source
# File lib/vlc360/resource.rb, line 28
def initialize(attributes = {})
  clear_errors
  set_attributes(attributes)
end
new_from_hash(attrs, type) click to toggle source
# File lib/vlc360/resource.rb, line 21
def new_from_hash(attrs, type)
  VLC360.const_get(type.capitalize).new(attrs)
end

Public Instance Methods

set_attributes(attributes) click to toggle source
# File lib/vlc360/resource.rb, line 33
def set_attributes(attributes)
  attributes.each do |key, value|
    if self.class.attributes.include?(key.to_sym)
      send("#{key}=", value)
    elsif self.class.nested_resource == key.to_sym
      send("#{key}=", Resource.new_from_hash(value, key))
    elsif self.class.nested_resources.include?(key.to_sym)
      send("#{key}=", (value || []).map{ |attrs| Resource.new_from_hash(attrs, key) })
    end
  end
end
to_hash() click to toggle source
# File lib/vlc360/resource.rb, line 49
def to_hash
  hashie = Hash[self.class.attributes.map { |attribute| [attribute, send(attribute)] }]
  hashie[self.class.nested_resource] = send(self.class.nested_resource)&.to_hash if self.class.nested_resource

  self.class.nested_resources.map do |resources|
    hashie[resources] = send(resources)&.map(&:to_hash)
  end

  hashie.delete_if { |k, v| k.nil? || v.nil? }
end
valid?() click to toggle source
# File lib/vlc360/resource.rb, line 45
def valid?
  errors.empty?
end

Private Instance Methods

clear_errors() click to toggle source
# File lib/vlc360/resource.rb, line 62
def clear_errors
  @errors = []
end