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,
lib/cfoundry/v2/model_magic/query_multi_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.



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

def scoped_organization
  @scoped_organization
end

#scoped_spaceObject (readonly)

Returns the value of attribute scoped_space.



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

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



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

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
    else
      params[k] = v
    end
  end

  params
end

.query_from_hash(query_params) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/cfoundry/v2/model_magic.rb', line 118

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}"
      when QueryMultiValue
        value.collect_values(key)
      else
        "#{key}:#{value}"
    end
  end.join(";")
end

Instance Method Details

#attributesObject



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

def attributes
  @attributes ||= {}
end

#defaultsObject



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

def defaults
  @defaults ||= {}
end

#inherited(klass) ⇒ Object



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

def inherited(klass)
  add_client_methods(klass)
  has_summary
end

#object_nameObject



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

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

#plural_object_nameObject



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

def plural_object_name
  :"#{object_name}s"
end

#scoped_to_organization(relation = :organization) ⇒ Object



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

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

#scoped_to_space(relation = :space) ⇒ Object



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

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

#to_many_relationsObject



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

def to_many_relations
  @to_many_relations ||= {}
end

#to_one_relationsObject



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

def to_one_relations
  @to_one_relations ||= {}
end