class PrintfulAPI::APIResource

Attributes

api_attribute_list[RW]
raw_data[RW]

Public Class Methods

api_attributes( *args ) click to toggle source
# File lib/printful_api/api_resource.rb, line 9
def self.api_attributes( *args )
        self.api_attribute_list = (self.api_attribute_list || []).concat(args)

        attr_accessor *args
end
belongs_to( attribute_name, args = {} ) click to toggle source
# File lib/printful_api/api_resource.rb, line 31
def self.belongs_to( attribute_name, args = {} )
        args[:class] = args[:class] if args[:class].is_a? String
        args[:class] ||= "PrintfulAPI::#{APIResource.camelize( attribute_name.to_s )}"
        args[:foreign_key] ||= "#{attribute_name}_id"

        self.api_attributes *[attribute_name.to_sym]

        define_method("#{attribute_name}") do
                attribute_value = self.instance_variable_get( "@#{attribute_name}" )
                attribute_value ||= args[:class].new.get( args[:foreign_key] ) if self.respond_to?( args[:foreign_key] )
                self.instance_variable_set( "@#{attribute_name}", attribute_value )
        end

        define_method("#{attribute_name}=") do |data|
                if data.is_a?(PrintfulAPI::APIResource)
                        self.instance_variable_set("@#{attribute_name}", data)
                else
                        model = args[:class].constantize.new.load_data(data) if data.present?
                        self.instance_variable_set( "@#{attribute_name}", model )
                end
        end
end
camelize( str ) click to toggle source
# File lib/printful_api/api_resource.rb, line 94
def self.camelize( str )
        str.split('_').map {|w| w.capitalize}.join
end
has_many( attribute_name, args = {} ) click to toggle source
# File lib/printful_api/api_resource.rb, line 15
def self.has_many( attribute_name, args = {} )
        args[:class] = args[:class] if args[:class].is_a? String
        args[:class] ||= "PrintfulAPI::#{APIResource.singularize( APIResource.camelize(attribute_name.to_s ) )}"

        self.api_attributes *[attribute_name.to_sym]

define_method("#{attribute_name}=") do |array|
                if array.present?
                        array = array.collect do |data|
                                args[:class].constantize.new.load_data(data)
                        end
                end
                self.instance_variable_set("@#{attribute_name}", array)
        end
end
singularize( str ) click to toggle source
# File lib/printful_api/api_resource.rb, line 98
def self.singularize( str )
        str = str[0..-2] if str[-1] == 's'
        str
end

Public Instance Methods

load_data( data ) click to toggle source
# File lib/printful_api/api_resource.rb, line 54
def load_data( data )
        self.raw_data = data
        data.each do |key,value|

                if self.respond_to? "#{key}="
                        self.send("#{key}=", value)
                else
                        puts "Accessor doesn't exist #{self.class.name}\##{"#{key}="}"
                end

        end

        self

end
to_h() click to toggle source
# File lib/printful_api/api_resource.rb, line 70
def to_h()
        hash = {}
        self.class.api_attribute_list.each do |attribute_name|
                value = self.send(attribute_name)
                if value.is_a? Array

                        hash[attribute_name] = value.collect do |array_item|
                                if array_item.is_a? APIResource
                                        hash[attribute_name] = array_item.to_h
                                else
                                        hash[attribute_name] = array_item
                                end
                        end

                elsif value.is_a? APIResource
                        hash[attribute_name] = value.to_h
                else
                        hash[attribute_name] = value
                end
        end

        hash
end