Sha256: 26f971d5af6b5ca7fd19603cae5fd4acc410d14f9a40d73ea05608d5768fa3f0

Contents?: true

Size: 1.29 KB

Versions: 42

Compression:

Stored size: 1.29 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))
        @inner_object.send(nested_resource_type).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

42 entries across 42 versions & 1 rubygems

Version Path
restful_resource-2.2.4 lib/restful_resource/associations.rb
restful_resource-2.2.3 lib/restful_resource/associations.rb
restful_resource-2.2.2 lib/restful_resource/associations.rb
restful_resource-2.2.1 lib/restful_resource/associations.rb
restful_resource-2.2.0 lib/restful_resource/associations.rb
restful_resource-2.0.2 lib/restful_resource/associations.rb
restful_resource-2.0.1 lib/restful_resource/associations.rb
restful_resource-1.6.0 lib/restful_resource/associations.rb
restful_resource-1.5.0 lib/restful_resource/associations.rb
restful_resource-1.4.3 lib/restful_resource/associations.rb
restful_resource-1.4.2 lib/restful_resource/associations.rb
restful_resource-1.4.1 lib/restful_resource/associations.rb
restful_resource-1.4.0 lib/restful_resource/associations.rb
restful_resource-1.3.0 lib/restful_resource/associations.rb
restful_resource-1.2.2 lib/restful_resource/associations.rb
restful_resource-1.2.1 lib/restful_resource/associations.rb
restful_resource-1.2.0 lib/restful_resource/associations.rb
restful_resource-1.1.0 lib/restful_resource/associations.rb
restful_resource-1.0.0 lib/restful_resource/associations.rb
restful_resource-0.10.2 lib/restful_resource/associations.rb