Sha256: bab0bc31f3cb2dae970839feb40d2fd1814ebb7e724756bb373746142d51b5fc

Contents?: true

Size: 1.83 KB

Versions: 8

Compression:

Stored size: 1.83 KB

Contents

module Evvnt
  # Internal: Adds behaviour to {Evvnt::Base} for handling nested resources
  #
  # Examples
  #
  #   class Thing < Evvnt::Base
  #     belongs_to :user
  #   end
  #   # Will find a Thing at "/users/456/things/123.json"
  #   @thing = Thing.find("123", user_id: "456")
  #
  module NestedResources
    # frozen_string_literal: true

    private

      # Tell a class that it's resources may be nested within another named resource
      #
      # parent_resource - A Symbol with the name of the parent resource.
      #
      def belongs_to(parent_resource)
        parent_resource = parent_resource.to_sym
        parent_resources << parent_resource unless parent_resource.in?(parent_resources)
      end

      # A list of the parent resources for this class.
      #
      # Return Array
      def parent_resources
        @parent_resources ||= []
      end

      # A list of the param names that represent parent resources
      #
      # Returns Array
      def parent_resource_id_params
        parent_resources.map { |pr| :"#{pr}_id" }
      end

      # Does the params hash contain a parent resource ID?
      #
      # params - A Hash of params to send to the API.
      #
      # Returns Boolean
      def params_include_parent_resource_id?(params)
        parent_resource_param(params).present?
      end

      # The param that represents the parent record (e.g. +:user_id+)
      #
      # params - A Hash of params to send to the API.
      #
      # Returns Symbol
      def parent_resource_param(params)
        (params.symbolize_keys.keys & parent_resource_id_params).first
      end

      # The name of the parent record defined in the parms
      #
      # params - A Hash of params to send to the API.
      #
      def parent_resource_name(params)
        parent_resource_param(params).to_s.gsub(/\_id/, '').pluralize
      end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
evvnt-0.2.6 lib/evvnt/nested_resources.rb
evvnt-0.2.5 lib/evvnt/nested_resources.rb
evvnt-0.2.4 lib/evvnt/nested_resources.rb
evvnt-0.2.3 lib/evvnt/nested_resources.rb
evvnt-0.2.2 lib/evvnt/nested_resources.rb
evvnt-0.2.1 lib/evvnt/nested_resources.rb
evvnt-0.2.0 lib/evvnt/nested_resources.rb
evvnt-0.1.0 lib/evvnt/nested_resources.rb