Sha256: 0d3ecee1675f84b4de81d01eb13776747b475eccae3c4dd68df099faf60fc31c

Contents?: true

Size: 1.38 KB

Versions: 5

Compression:

Stored size: 1.38 KB

Contents

module RestfulResource
  module Associations
    def has_many(nested_resource_type)
      namespace = self.to_s.deconstantize
      klass_name = "#{nested_resource_type.to_s.singularize.camelize.to_s}"
      klass_name = "#{namespace}::#{klass_name}" if namespace.present?

      self.send(:define_method, nested_resource_type) do
        klass = begin
          klass_name.safe_constantize
        rescue NameError
          nil
        end
        klass = RestfulResource::OpenObject if (klass.nil? || !(klass < RestfulResource::OpenObject))
        nested_resource = @inner_object.send(nested_resource_type)
        return nil if nested_resource.nil?
        nested_resource.map { |obj| klass.new(obj) }
      end
    end

    def has_one(nested_resource_type)
      namespace = self.to_s.deconstantize
      klass_name = "#{nested_resource_type.to_s.camelize.to_s}"
      klass_name = "#{namespace}::#{klass_name}" if namespace.present?

      self.send(:define_method, nested_resource_type) do
        klass = begin
          klass_name.safe_constantize
        rescue NameError
          nil
        end
        klass = RestfulResource::OpenObject if (klass.nil? || !(klass < RestfulResource::OpenObject))
        nested_resource = @inner_object.send(nested_resource_type)
        return nil if nested_resource.nil?
        klass.new(@inner_object.send(nested_resource_type))
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
restful_resource-2.3.0 lib/restful_resource/associations.rb
restful_resource-2.2.8 lib/restful_resource/associations.rb
restful_resource-2.2.7 lib/restful_resource/associations.rb
restful_resource-2.2.6 lib/restful_resource/associations.rb
restful_resource-2.2.5 lib/restful_resource/associations.rb