Class: RemoteModule::RemoteModel

Inherits:
Object
  • Object
show all
Defined in:
lib/remote_model/record.rb,
lib/remote_model/requests.rb,
lib/remote_model/remote_model.rb

Overview

ActiveRecord-esque methods

Constant Summary collapse

HTTP_METHODS =
[:get, :post, :put, :delete]

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ RemoteModel

Returns a new instance of RemoteModel.



97
98
99
# File 'lib/remote_model/remote_model.rb', line 97

def initialize(params = {})
  update_attributes(params)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/remote_model/remote_model.rb', line 132

def method_missing(method, *args, &block)
  # Check for custom URLs
  if self.class.custom_urls.has_key? method
    return self.class.custom_urls[method].format(args && args[0], self)
  end

  # has_one relationships
  if self.class.has_one.has_key?(method) || self.class.belongs_to.has_key?(method)
    return instance_variable_get("@" + method.to_s)
  elsif (setter_vals = setter_klass(self.class.has_one, method) || setter_vals = setter_klass(self.class.belongs_to, method))
    klass, hash_symbol = setter_vals
    obj = args[0]
    if obj.class != klass
      obj = klass.new(obj)
    end
    return instance_variable_set("@" + hash_symbol.to_s, obj)
  end

  # has_many relationships
  if self.class.has_many.has_key?(method)
    ivar = "@" + method.to_s
    if !instance_variable_defined? ivar
      instance_variable_set(ivar, [])
    end
    return instance_variable_get ivar
  elsif (setter_vals = setter_klass(self.class.has_many, method))
    klass, hash_symbol = setter_vals
    ivar = "@" + hash_symbol.to_s

    tmp = []
    args[0].each do |arg|
      rep = nil
      if arg.class == Hash
        rep = klass.new(arg)
      elsif arg.class == klass
        rep = arg
      end

      if rep.class.belongs_to.values.member? self.class
        rep.send((rep.class.belongs_to.invert[self.class].to_s + "=").to_sym, self)
      end

      tmp << rep
    end

    instance_variable_set(ivar, tmp)
    return instance_variable_get(ivar)
  end

  # HTTP methods
  if RemoteModule::RemoteModel::HTTP_METHODS.member? method
    return self.class.send(method, *args, &block)
  end

  super
end

Class Attribute Details

.default_url_optionsObject

Returns the value of attribute default_url_options.



4
5
6
# File 'lib/remote_model/requests.rb', line 4

def default_url_options
  @default_url_options
end

.extensionObject



7
8
9
# File 'lib/remote_model/requests.rb', line 7

def extension
  @extension || (self == RemoteModel ? false : RemoteModel.extension) || ".json"
end

.root_urlObject

Returns the value of attribute root_url.



4
5
6
# File 'lib/remote_model/requests.rb', line 4

def root_url
  @root_url
end

Class Method Details

.belongs_to(params = []) ⇒ Object

EX self.belongs_to :question, :answer, :camel_case

> => Question, :answer => Answer, :camel_case => CamelCase



31
32
33
# File 'lib/remote_model/remote_model.rb', line 31

def belongs_to(params = [])
  make_fn_lookup "belongs_to", params, singular_klass_str_lambda
end

.collection_url(url_format = -1)) ⇒ Object

URLs for the resource Can be called by <class>.<url>



14
15
16
17
18
# File 'lib/remote_model/requests.rb', line 14

def collection_url(url_format = -1)
  return @collection_url || nil if url_format == -1

  @collection_url = RemoteModule::FormatableString.new(url_format)
end

.custom_urls(params = {}) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/remote_model/requests.rb', line 26

def custom_urls(params = {})
  @custom_urls ||= {}
  params.each do |fn, url_format|
    @custom_urls[fn] = RemoteModule::FormatableString.new(url_format)
  end
  @custom_urls
end

.delete(url, params = {}, &block) ⇒ Object



52
53
54
# File 'lib/remote_model/requests.rb', line 52

def delete(url, params = {}, &block)
  http_call(:delete, url, params, &block)
end

.find(id, params = {}, &block) ⇒ Object



6
7
8
9
10
11
12
13
14
15
# File 'lib/remote_model/record.rb', line 6

def find(id, params = {}, &block)
  get(member_url.format(params.merge(id: id))) do |response, json|
    if response.ok?
      obj = self.new(json)
      request_block_call(block, obj, response)
    else
      request_block_call(block, nil, response)
    end
  end
end

.find_all(params = {}, &block) ⇒ Object



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
# File 'lib/remote_model/record.rb', line 17

def find_all(params = {}, &block)
  get(collection_url.format(params)) do |response, json|
    if response.ok?
      objs = []
      arr_rep = nil
      if json.class == Array
        arr_rep = json
      elsif json.class == Hash
        plural_sym = self.pluralize.to_sym
        if json.has_key? plural_sym
          arr_rep = json[plural_sym]
        end
      else
        # the returned data was something else
        # ie a string, number
        request_block_call(block, nil, response)
        return
      end
      arr_rep.each { |one_obj_hash|
        objs << self.new(one_obj_hash)
      }
      request_block_call(block, objs, response)
    else
      request_block_call(block, nil, response)
    end
  end
end

.get(url, params = {}, &block) ⇒ Object

URL helpers (via BubbleWrap) EX Question.get(a_question.custom_url) do |response, json|

p json

end



40
41
42
# File 'lib/remote_model/requests.rb', line 40

def get(url, params = {}, &block)
  http_call(:get, url, params, &block)
end

.has_many(params = []) ⇒ Object

EX self.has_many :questions, :answers, :camel_cases

> => Question, :answers => Answer, :camel_cases => CamelCase



24
25
26
# File 'lib/remote_model/remote_model.rb', line 24

def has_many(params = [])
  make_fn_lookup "has_many", params, lambda { |sym| sym.to_s.singularize.split("_").collect {|s| s.capitalize}.join }
end

.has_one(params = []) ⇒ Object

EX self.has_one :question, :answer, :camel_case

> => Question, :answer => Answer, :camel_case => CamelCase



17
18
19
# File 'lib/remote_model/remote_model.rb', line 17

def has_one(params = [])
  make_fn_lookup "has_one", params, singular_klass_str_lambda
end

.member_url(url_format = -1)) ⇒ Object



20
21
22
23
24
# File 'lib/remote_model/requests.rb', line 20

def member_url(url_format = -1)
  return @member_url if url_format == -1

  @member_url = RemoteModule::FormatableString.new(url_format)
end

.method_missing(method, *args, &block) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/remote_model/remote_model.rb', line 39

def method_missing(method, *args, &block)
  if self.custom_urls.has_key? method
    return self.custom_urls[method].format(args && args[0], self)
  end

  super
end

.pluralizeObject



35
36
37
# File 'lib/remote_model/remote_model.rb', line 35

def pluralize
  self.to_s.downcase + "s"
end

.post(url, params = {}, &block) ⇒ Object



44
45
46
# File 'lib/remote_model/requests.rb', line 44

def post(url, params = {}, &block)
  http_call(:post, url, params, &block)
end

.put(url, params = {}, &block) ⇒ Object



48
49
50
# File 'lib/remote_model/requests.rb', line 48

def put(url, params = {}, &block)
  http_call(:put, url, params, &block)
end

Instance Method Details

#collection_url(params = {}) ⇒ Object



87
88
89
# File 'lib/remote_model/requests.rb', line 87

def collection_url(params = {})
  self.class.collection_url.format(params, self)
end

#destroy(&block) ⇒ Object

EX a_model.destroy do |response, json|

if json[:success]
  p "success!"
end

end



68
69
70
71
72
73
74
# File 'lib/remote_model/record.rb', line 68

def destroy(&block)
  delete(member_url) do |response, json|
    if block
      block.call response, json
    end
  end
end

#member_url(params = {}) ⇒ Object



91
92
93
# File 'lib/remote_model/requests.rb', line 91

def member_url(params = {})
  self.class.member_url.format(params, self)
end

#methodsObject



120
121
122
# File 'lib/remote_model/remote_model.rb', line 120

def methods
  super + remote_model_methods
end

#remote_model_methodsObject



110
111
112
113
114
115
116
117
118
# File 'lib/remote_model/remote_model.rb', line 110

def remote_model_methods
  methods = []
  [self.class.has_one, self.class.has_many, self.class.belongs_to].each {|fn_hash|
    methods += fn_hash.collect {|sym, klass|
      [sym, (sym.to_s + "=:").to_sym, ("set" + sym.to_s.capitalize).to_sym]
    }.flatten
  }
  methods + RemoteModule::RemoteModel::HTTP_METHODS
end

#respond_to?(symbol, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


124
125
126
127
128
129
130
# File 'lib/remote_model/remote_model.rb', line 124

def respond_to?(symbol, include_private = false)
  if remote_model_methods.include? symbol
    return true
  end

  super
end

#update_attributes(params = {}) ⇒ Object



101
102
103
104
105
106
107
108
# File 'lib/remote_model/remote_model.rb', line 101

def update_attributes(params = {})
  attributes = self.methods - Object.methods
  params.each do |key, value|
    if attributes.member?((key.to_s + "=:").to_sym)
      self.send((key.to_s + "=:").to_sym, value)
    end
  end
end