module Moneybird::Resource

Attributes

client[RW]
errors[RW]

Public Class Methods

new(attributes) click to toggle source
# File lib/moneybird/resource.rb, line 5
def initialize(attributes)
  self.attributes = attributes
end

Public Instance Methods

attributes() click to toggle source
# File lib/moneybird/resource.rb, line 29
def attributes
  self.class.attributes.inject({}) do |attributes, attribute|
    value = send(attribute)
    if !value.nil?
      if value.is_a?(Array) && value.first.respond_to?(:attributes)
        attributes["#{attribute}_attributes"] = value.map { |item| item.attributes }
      elsif value.respond_to?(:attributes)
        attributes["#{attribute}_attributes"] = value.attributes
      else
        unless self.class.nillable_attributes && self.class.nillable_attributes.include?(attribute)
          attributes[attribute] = value
        end
      end
    end; attributes
  end
end
attributes=(attributes) click to toggle source
# File lib/moneybird/resource.rb, line 17
def attributes=(attributes)
  @attributes = attributes
  self.client ||= attributes[:client]
  attributes.each do |attribute, value|
    if respond_to?(writer = attribute.to_s + '=')
      send(writer, value)
    else
      self.class.logger.warn "#{self.class} does not have an `#{attribute}' attribute"
    end
  end
end
path() click to toggle source
# File lib/moneybird/resource.rb, line 13
def path
  persisted? ? "/#{id}" : ""
end
persisted?() click to toggle source
# File lib/moneybird/resource.rb, line 9
def persisted?
  !!id
end
to_json() click to toggle source
# File lib/moneybird/resource.rb, line 46
def to_json
  JSON.generate({self.class.resource => attributes.as_json})
end