Sha256: f2225f5cf645a297707a1df019bcc284c68d2f50111a89b4dbcbf811497c2be0

Contents?: true

Size: 1.85 KB

Versions: 27

Compression:

Stored size: 1.85 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(":")
        end
      end

      params
    end
  end
end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
cfoundry-3.0.1 lib/cfoundry/v2/model_magic.rb
cfoundry-3.0.0 lib/cfoundry/v2/model_magic.rb
cfoundry-2.4.1.rc1 lib/cfoundry/v2/model_magic.rb
cfoundry-2.4.0 lib/cfoundry/v2/model_magic.rb
cfoundry-2.3.7.rc1 lib/cfoundry/v2/model_magic.rb
cfoundry-2.3.6 lib/cfoundry/v2/model_magic.rb
cfoundry-2.3.6.rc2 lib/cfoundry/v2/model_magic.rb
cfoundry-2.3.6.rc1 lib/cfoundry/v2/model_magic.rb
cfoundry-2.3.5 lib/cfoundry/v2/model_magic.rb
cfoundry-2.3.4 lib/cfoundry/v2/model_magic.rb
cfoundry-2.3.3 lib/cfoundry/v2/model_magic.rb
cfoundry-2.3.2 lib/cfoundry/v2/model_magic.rb
cfoundry-2.3.1 lib/cfoundry/v2/model_magic.rb
cfoundry-2.3.0 lib/cfoundry/v2/model_magic.rb
cfoundry-2.2.0rc3 lib/cfoundry/v2/model_magic.rb
cfoundry-2.2.0.rc2 lib/cfoundry/v2/model_magic.rb
cfoundry-2.2.0.rc1 lib/cfoundry/v2/model_magic.rb
cfoundry-2.1.2.rc1 lib/cfoundry/v2/model_magic.rb
cfoundry-2.1.1 lib/cfoundry/v2/model_magic.rb
cfoundry-2.1.0 lib/cfoundry/v2/model_magic.rb