Sha256: eb256d62476c32286e558bc39fe8ed0a3d828865272aafce976f03bfc6e5293a

Contents?: true

Size: 1.94 KB

Versions: 8

Compression:

Stored size: 1.94 KB

Contents

require "jfoundry/validator"
require "jfoundry/v2/model_magic/client_extensions"
require "jfoundry/v2/model_magic/has_summary"
require "jfoundry/v2/model_magic/attribute"
require "jfoundry/v2/model_magic/to_one"
require "jfoundry/v2/model_magic/to_many"
require "jfoundry/v2/model_magic/queryable_by"

module JFoundry::V2
  # object name -> module containing query methods
  #
  # e.g. app -> { app_by_name, app_by_space_guid, ... }
  QUERIES = Hash.new do |h, k|
    h[k] = Module.new
  end

  module BaseClientMethods
  end

  module ClientMethods
  end

  module ModelMagic
    include ModelMagic::ClientExtensions
    include ModelMagic::HasSummary
    include ModelMagic::Attribute
    include ModelMagic::ToOne
    include ModelMagic::ToMany
    include ModelMagic::QueryableBy

    attr_reader :scoped_organization, :scoped_space

    def object_name
      @object_name ||=
        name.split("::").last.gsub(
          /([a-z])([A-Z])/,
          '\1_\2').downcase.to_sym
    end

    def plural_object_name
      :"#{object_name}s"
    end

    def defaults
      @defaults ||= {}
    end

    def attributes
      @attributes ||= {}
    end

    def to_one_relations
      @to_one_relations ||= {}
    end

    def to_many_relations
      @to_many_relations ||= {}
    end

    def inherited(klass)
      add_client_methods(klass)
      has_summary
    end

    def scoped_to_organization(relation = :organization)
      @scoped_organization = relation
    end

    def scoped_to_space(relation = :space)
      @scoped_space = relation
    end

    def self.params_from(args)
      options, _ = args
      options ||= {}
      options[:depth] ||= 1

      params = {}
      options.each do |k, v|
        case k
        when :depth
          params[:"inline-relations-depth"] = v
        when :query
          params[:q] = v.join(":")
        when :user_provided
          params[:"return_user_provided_service_instances"] = v
        end
      end

      params
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
jfoundry-0.1.7 lib/jfoundry/v2/model_magic.rb
jfoundry-0.1.6 lib/jfoundry/v2/model_magic.rb
jfoundry-0.1.4 lib/jfoundry/v2/model_magic.rb
jfoundry-0.1.3 lib/jfoundry/v2/model_magic.rb
jfoundry-0.1.2 lib/jfoundry/v2/model_magic.rb
jfoundry-0.1.1 lib/jfoundry/v2/model_magic.rb
jfoundry-0.1.0.pre lib/jfoundry/v2/model_magic.rb
jfoundry-0.1.0 lib/jfoundry/v2/model_magic.rb