Sha256: d939fa83a5c5c8bc19cdfc23a8f8904207372db26fa3eda6cdac5584489f3ed9

Contents?: true

Size: 1.94 KB

Versions: 22

Compression:

Stored size: 1.94 KB

Contents

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

module CFoundry::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

22 entries across 22 versions & 1 rubygems

Version Path
cfoundry-4.3.7 lib/cfoundry/v2/model_magic.rb
cfoundry-4.3.6 lib/cfoundry/v2/model_magic.rb
cfoundry-4.3.5 lib/cfoundry/v2/model_magic.rb
cfoundry-4.3.5.rc1 lib/cfoundry/v2/model_magic.rb
cfoundry-4.3.4 lib/cfoundry/v2/model_magic.rb
cfoundry-4.3.4.rc1 lib/cfoundry/v2/model_magic.rb
cfoundry-4.3.3 lib/cfoundry/v2/model_magic.rb
cfoundry-4.3.2.rc1 lib/cfoundry/v2/model_magic.rb
cfoundry-4.3.1 lib/cfoundry/v2/model_magic.rb
cfoundry-4.3.0 lib/cfoundry/v2/model_magic.rb
cfoundry-4.2.0.rc lib/cfoundry/v2/model_magic.rb
cfoundry-4.1.0 lib/cfoundry/v2/model_magic.rb
cfoundry-4.0.4.rc2 lib/cfoundry/v2/model_magic.rb
cfoundry-4.0.4.rc1 lib/cfoundry/v2/model_magic.rb
cfoundry-4.0.3 lib/cfoundry/v2/model_magic.rb
cfoundry-4.0.2.rc5 lib/cfoundry/v2/model_magic.rb
cfoundry-4.0.2.rc4 lib/cfoundry/v2/model_magic.rb
cfoundry-4.0.2.rc3 lib/cfoundry/v2/model_magic.rb
cfoundry-4.0.2.rc2 lib/cfoundry/v2/model_magic.rb
cfoundry-4.0.2.rc1 lib/cfoundry/v2/model_magic.rb