Sha256: b4e0d6405294772e1d05232bc25528ead4c50648961a6070982485af4bf4c9c9

Contents?: true

Size: 1.85 KB

Versions: 13

Compression:

Stored size: 1.85 KB

Contents

module JSONAPIonify::Api
  module Base::ResourceDefinitions

    def self.extended(klass)
      klass.class_eval do
        extend JSONAPIonify::InheritedAttributes
        inherited_hash_attribute :resource_definitions
      end
    end

    def resource(type)
      raise ArgumentError, 'type required' if type.nil?
      type       = type.to_sym
      const_name = type.to_s.camelcase + 'Resource'
      return const_get(const_name, false) if const_defined?(const_name, false)
      raise Errors::ResourceNotFound, "Resource not defined: #{type}" unless resource_defined?(type)
      klass = Class.new(resource_class, &resource_definitions[type]).set_type(type)
      param(:fields, type)
      const_set const_name, klass
    rescue NameError => e
      raise e unless e.instance_of?(NameError)
      raise Errors::ResourceNotFound, "Resource not defined: #{type}"
    end

    def resource_defined?(name)
      load_resources
      !!resource_definitions[name]
    end

    def resources
      load_resources
      resource_definitions.map do |name, _|
        resource(name)
      end
    end

    def define_resource(name, extend: nil, &block)
      resource_definitions[name.to_sym] =
        if extend
          sup = superclass
          cur = self
          proc do
            extend_def =
              if name.to_sym == extend.to_sym && sup.respond_to?(:resource)
                sup.resource(extend)
                sup.resource_definitions[extend.to_sym]
              else
                cur.resource(extend)
                cur.resource_definitions[extend.to_sym]
              end
            class_eval &extend_def
            class_eval &block
          end
        else
          block
        end
      const_name                        = name.to_s.camelcase + 'Resource'
      remove_const(const_name) if const_defined? const_name, false
      name
    end

  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
jsonapionify-0.11.10 lib/jsonapionify/api/base/resource_definitions.rb
jsonapionify-0.11.9 lib/jsonapionify/api/base/resource_definitions.rb
jsonapionify-0.11.8 lib/jsonapionify/api/base/resource_definitions.rb
jsonapionify-0.11.7 lib/jsonapionify/api/base/resource_definitions.rb
jsonapionify-0.11.6 lib/jsonapionify/api/base/resource_definitions.rb
jsonapionify-0.11.5 lib/jsonapionify/api/base/resource_definitions.rb
jsonapionify-0.11.0 lib/jsonapionify/api/base/resource_definitions.rb
jsonapionify-0.10.2 lib/jsonapionify/api/base/resource_definitions.rb
jsonapionify-0.10.1 lib/jsonapionify/api/base/resource_definitions.rb
jsonapionify-0.10.0 lib/jsonapionify/api/base/resource_definitions.rb
jsonapionify-0.9.3 lib/jsonapionify/api/base/resource_definitions.rb
jsonapionify-0.9.2 lib/jsonapionify/api/base/resource_definitions.rb
jsonapionify-0.9.1 lib/jsonapionify/api/base/resource_definitions.rb