Class: Rdio::ApiObj

Inherits:
Object show all
Defined in:
lib/rdio/base.rb

Overview


Base class for remote objects. They contain an api instance, and also have a fill method that will set the values to the appropriate values


Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api) ⇒ ApiObj

Returns a new instance of ApiObj.



145
146
147
# File 'lib/rdio/base.rb', line 145

def initialize(api)
  @api = api
end

Instance Attribute Details

#apiObject (readonly)

Returns the value of attribute api.



143
144
145
# File 'lib/rdio/base.rb', line 143

def api
  @api
end

Instance Method Details

#fill(x) ⇒ Object



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
188
189
190
191
192
193
194
195
196
197
# File 'lib/rdio/base.rb', line 149

def fill(x)
  return self if not x
  if Rdio::log_fill
    Rdio::log "#{self.class}.fill #{x.class} : #{x}"
  end
  syms_to_types = Rdio::symbols_to_types || {}
  #
  # https://github.com/spudtrooper/rdiorb/issues/9: Certain rubys
  # don't declare 'each' on String.  In this case and others where
  # the key is a mapped type and the resulting body isn't a Hash,
  # make x a Hash
  #
  x = {x => nil} if not x.is_a? Hash
  x.each do |k,v|
    sym = Rdio::camel2underscores(k).to_sym
    #
    # If we have an actual type for this symbol, then use that
    # type to construct this value.  Otherwise, it's just a
    # primitive type
    #
    type = syms_to_types[sym]
    if Rdio::log_symbols
      Rdio::log "#{self.class}.#{sym} => #{type} v=#{v.class}"
    end
    if type and v.is_a? Enumerable
      #
      # Allow simple types that are used for arrays
      #
      if v.is_a? Array
        o = v.map {|x| type.new(api).fill x}
      else
        o = type.new api
        o.fill v
      end
    else
      o = Rdio::to_o v
    end
    begin
      sym_eq = (Rdio::camel2underscores(k)+'=').to_sym
      self.send sym_eq,o
    rescue Exception => e
      if Rdio::log_couldnt_find_symbols
        Rdio::logger.warn "Couldn't find symbol: " +
          "#{sym} => #{o} for type: #{self.class}"
      end
    end
  end
  self
end