Module: CFoundry::V2::ModelMagic::ClientExtensions

Included in:
CFoundry::V2::ModelMagic
Defined in:
lib/cfoundry/v2/model_magic/client_extensions.rb

Instance Method Summary collapse

Instance Method Details

#add_client_methods(klass) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/cfoundry/v2/model_magic/client_extensions.rb', line 5

def add_client_methods(klass)
  singular = klass.object_name
  plural = klass.plural_object_name

  define_base_client_methods do
    #
    # def client.MODEL
    #
    define_method(singular) do |guid, *args|
      get("v2", plural, guid,
        :accept => :json,
        :params => ModelMagic.params_from(args)
      )
    end

    #
    # def client.MODELs
    #
    define_method(plural) do |*args|
      all_pages(
        get("v2", plural,
          :accept => :json,
          :params => ModelMagic.params_from(args)
        )
      )
    end

    #
    # def client.MODELs_first_page
    #
    define_method(:"#{plural}_first_page") do |*args|
      get("v2", plural,
        :accept => :json,
        :params => ModelMagic.params_from(args)
      )
    end

    #
    # def client.MODELs_for_each
    #
    define_method(:"#{plural}_for_each") do |*args, &block|
      for_each(get("v2", plural,
          :accept => :json,
          :params => ModelMagic.params_from(args)
        ),
        &block
      )
    end
  end


  define_client_methods do
    #
    # def client.MODEL
    #
    define_method(singular) do |*args|
      guid, partial, _ = args

      x = klass.new(guid, self, nil, partial)

      # when creating an object, automatically set the org/space
      unless guid
        if klass.scoped_organization && current_organization
          x.send(:"#{klass.scoped_organization}=", current_organization)
        end

        if klass.scoped_space && current_space
          x.send(:"#{klass.scoped_space}=", current_space)
        end
      end

      x
    end

    #
    # def client.MODELs
    #
    define_method(plural) do |*args|
      # use current org/space
      if klass.scoped_space && current_space
        current_space.send(plural, *args)
      elsif klass.scoped_organization && current_organization
        current_organization.send(plural, *args)
      else
        @base.send(plural, *args).collect do |json|
          send(:"make_#{singular}", json)
        end
      end
    end

    #
    # def client.MODELs_first_page
    #
    define_method(:"#{plural}_first_page") do |*args|
      response = @base.send(:"#{plural}_first_page", *args)
      results = response[:resources].collect do |json|
        send(:"make_#{singular}", json)
      end
      {
          :next_page => !!response[:next_url],
          :results => results
      }
    end

    #
    # def client.MODELs_for_each
    #
    define_method(:"#{plural}_for_each") do |*args, &block|
      @base.send(:"#{plural}_for_each", *args) do |json|
        result = send(:"make_#{singular}", json)
        block.call(result)
      end
    end

    #
    # def client.MODEL_from
    #
    define_method(:"#{singular}_from") do |path, *args|
      send(
        :"make_#{singular}",
        @base.get(
          path,
          :accept => :json,
          :params => ModelMagic.params_from(args)))
    end

    #
    # def client.MODELs_from
    #
    define_method(:"#{plural}_from") do |path, *args|
      objs = @base.all_pages(
        @base.get(
          path,
          :accept => :json,
          :params => ModelMagic.params_from(args)))

      objs.collect do |json|
        send(:"make_#{singular}", json)
      end
    end

    #
    # def client.make_MODEL
    #
    define_method(:"make_#{singular}") do |json|
      klass.new(
        json[:metadata][:guid],
        self,
        json)
    end
  end
end