module TelegramBot::AutoFromMethods::ClassMethods

Public Instance Methods

extra_types() click to toggle source
# File lib/telegram_bot/auto_from_methods.rb, line 19
def extra_types
  {}
end
from(id) click to toggle source
# File lib/telegram_bot/auto_from_methods.rb, line 4
def from(id)
  case id
  when self, Struct
    id
  when nil
    nil
  when Hash
    parse(id)
  when Integer
    new(id)
  else
    warn "unknown stuff passed in [#{id}]"
  end
end
hash_key_aliases() click to toggle source
# File lib/telegram_bot/auto_from_methods.rb, line 23
def hash_key_aliases
  {}
end
parse(hsh) click to toggle source
# File lib/telegram_bot/auto_from_methods.rb, line 27
def parse(hsh)
  obj = new(*parse_attrs(hsh))
  parse_extra_types(obj)
  obj
end

Private Instance Methods

parse_attrs(hsh) click to toggle source
# File lib/telegram_bot/auto_from_methods.rb, line 35
def parse_attrs(hsh)
  aliases = hash_key_aliases
  members.map do |attr|
    if aliases.include? attr
      hash_attr = aliases[attr]
    else
      hash_attr = attr
    end
    hsh[attr] || hsh[hash_attr.to_s]
  end
end
parse_extra_types(obj) click to toggle source
# File lib/telegram_bot/auto_from_methods.rb, line 47
def parse_extra_types(obj)
  extra_types.each do |attr, typ|
    case typ
    when Class
      obj[attr] = typ.from(obj[attr])
    when Array
      unless obj[attr].nil?
        obj[attr] = obj[attr].map { |x| typ[0].from(x) }
      end
    else
      warn 'unknown type #{type}'
    end
  end
end