Module: CFoundry::V2::ModelMagic

Includes:
Attribute, ClientExtensions, HasSummary, QueryValueHelper, QueryableBy, ToMany, ToOne
Included in:
Model
Defined in:
lib/cfoundry/v2/model_magic.rb,
lib/cfoundry/v2/model_magic/to_one.rb,
lib/cfoundry/v2/model_magic/to_many.rb,
lib/cfoundry/v2/model_magic/attribute.rb,
lib/cfoundry/v2/model_magic/has_summary.rb,
lib/cfoundry/v2/model_magic/client_extensions.rb,
lib/cfoundry/v2/model_magic/query_value_helper.rb

Defined Under Namespace

Modules: Attribute, ClientExtensions, HasSummary, QueryValueHelper, QueryableBy, ToMany, ToOne

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from QueryableBy

#queryable_by

Methods included from ToMany

#to_many

Methods included from ToOne

#to_one

Methods included from Attribute

#attribute

Methods included from HasSummary

#has_summary

Methods included from ClientExtensions

#add_client_methods

Instance Attribute Details

#scoped_organizationObject (readonly)

Returns the value of attribute scoped_organization.



33
34
35
# File 'lib/cfoundry/v2/model_magic.rb', line 33

def scoped_organization
  @scoped_organization
end

#scoped_spaceObject (readonly)

Returns the value of attribute scoped_space.



33
34
35
# File 'lib/cfoundry/v2/model_magic.rb', line 33

def scoped_space
  @scoped_space
end

Class Method Details

.params_from(args) ⇒ Object

To query a single attribute using equality, you can use :query => [“attribute”, “value”]

To query multiple attributes, you can specify a hash of attributes to values, where the values can be:

A single value with equality
  :query => {attr1: 'value1', attr2: 'value2'}
Multiple values for an attribute
  :query => {attr1: ['value1', 'value2']}
Complex comparisons i.e ('<', '>', '<=', '>=')
  :query => {attr1: QueryValue.new(comparator: '>', value: 'VALUE')}

QueryValue can be found in CFoundry::V2::ModelMagic::QueryValueHelper You can include this module in your class to access QueryValue directly priting is handled by #to_s



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/cfoundry/v2/model_magic.rb', line 91

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
      if v.is_a? Array
        params[:q] = v.join(":")
      else
        params[:q] = query_from_hash(v)
      end
    when :user_provided
      params[:"return_user_provided_service_instances"] = v
    end
  end

  params
end

.query_from_hash(query_params) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/cfoundry/v2/model_magic.rb', line 115

def self.query_from_hash(query_params)
  query_params.collect do |key, value|
    case value
      when Array
        qv = QueryValue.new(:comp => 'IN', :value => value)
        "#{key}#{qv}"
      when QueryValue
        "#{key}#{value}"
      else
        "#{key}:#{value}"
    end
  end.join(";")
end

Instance Method Details

#attributesObject



50
51
52
# File 'lib/cfoundry/v2/model_magic.rb', line 50

def attributes
  @attributes ||= {}
end

#defaultsObject



46
47
48
# File 'lib/cfoundry/v2/model_magic.rb', line 46

def defaults
  @defaults ||= {}
end

#inherited(klass) ⇒ Object



62
63
64
65
# File 'lib/cfoundry/v2/model_magic.rb', line 62

def inherited(klass)
  add_client_methods(klass)
  has_summary
end

#object_nameObject



35
36
37
38
39
40
# File 'lib/cfoundry/v2/model_magic.rb', line 35

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

#plural_object_nameObject



42
43
44
# File 'lib/cfoundry/v2/model_magic.rb', line 42

def plural_object_name
  :"#{object_name}s"
end

#scoped_to_organization(relation = :organization) ⇒ Object



67
68
69
# File 'lib/cfoundry/v2/model_magic.rb', line 67

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

#scoped_to_space(relation = :space) ⇒ Object



71
72
73
# File 'lib/cfoundry/v2/model_magic.rb', line 71

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

#to_many_relationsObject



58
59
60
# File 'lib/cfoundry/v2/model_magic.rb', line 58

def to_many_relations
  @to_many_relations ||= {}
end

#to_one_relationsObject



54
55
56
# File 'lib/cfoundry/v2/model_magic.rb', line 54

def to_one_relations
  @to_one_relations ||= {}
end