class Psapi::API_Object

Public Class Methods

accessors_for(camels) click to toggle source
# File lib/psapi/api_object.rb, line 13
def accessors_for(camels)
  snakes = camels.map(&method(:camel_to_snake))
  snakes.each do |snake|
    self.class_eval {
      attr_accessor snake.to_sym
    }
  end

  self.class_eval {
    define_method(:to_hash) do
      hash = {}
      snakes.each.with_index do |snake, index|
        hash[camels[index]] = instance_variable_get("@#{snake}")
      end
      hash
    end
  }
end
define_all_with(method_name) click to toggle source

APIオブジェクトクラスに all クラスメソッドを追加する

# File lib/psapi/api_object.rb, line 33
def define_all_with(method_name)
  object_klass = self
  self.singleton_class.class_eval do
    define_method(:all) do
      __send__(method_name).map do |item|
        object_klass.new(item)
      end
    end

  end
end
inherited(klass) click to toggle source
# File lib/psapi/api_object.rb, line 9
def inherited(klass)
  klass.singleton_class.__send__(:include, PeercastStation)
end
new(hash) click to toggle source
# File lib/psapi/api_object.rb, line 54
def initialize(hash)
   mass_assign(hash)
end

Public Instance Methods

mass_assign(hash) click to toggle source
# File lib/psapi/api_object.rb, line 47
def mass_assign(hash)
  hash.each do |key,value|
    sym = ('@' + camel_to_snake(key)).to_sym
    instance_variable_set(sym, value)
  end
end