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.



116
117
118
# File 'lib/rdio/base.rb', line 116

def initialize(api)
  @api = api
end

Instance Attribute Details

#apiObject (readonly)

Returns the value of attribute api.



114
115
116
# File 'lib/rdio/base.rb', line 114

def api
  @api
end

Instance Method Details

#fill(x) ⇒ Object



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
# File 'lib/rdio/base.rb', line 120

def fill(x)
  syms_to_types = Rdio::symbols_to_types || {}
  x.each do |k,v|
    sym = 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}"
    end
    if type
      #
      # 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 = to_o v
    end
    begin
      sym_eq = (camel2underscores(k)+'=').to_sym
      self.send sym_eq,o
    rescue Exception => e
      Rdio::logger.warn "Couldn't find symbol: " +
        "#{sym} => #{o} for type: #{self.class}"
    end
  end
  self
end