module Zakuro::Tools::Stringifier
Stringifier
文字列処理
Public Class Methods
to_h(obj:, class_prefix:, formatted: true)
click to toggle source
対象インスタンスをハッシュ化する
@param [Object] obj 対象インスタンス @param [String] class_prefix インスタンス内でハッシュ変換するクラスのプレフィックス
@return [Hash<String, Objcet>] ハッシュ
# File lib/zakuro/tools/stringifier.rb, line 23 def self.to_h(obj:, class_prefix:, formatted: true) hash = {} obj.instance_variables.each do |var| key = var.to_s.delete('@') hash[key] = value_to_hash( obj: obj.instance_variable_get(var), class_prefix: class_prefix, formatted: formatted ) end hash end
Private Class Methods
value_to_hash(obj:, class_prefix:, formatted:)
click to toggle source
対象インスタンスをハッシュ化する(再帰処理)
@param [Object] obj 対象インスタンス @param [String] class_prefix インスタンス内でハッシュ変換するクラスのプレフィックス
@return [Hash<String, Objcet>] ハッシュ
# File lib/zakuro/tools/stringifier.rb, line 44 def self.value_to_hash(obj:, class_prefix:, formatted:) return obj unless obj # 日付をフォーマットする return obj.format if formatted && Tools::Typeof.time?(obj: obj) # 同じモジュール内のオブジェクトは再帰する if obj.class.name.start_with?(class_prefix) return to_h(obj: obj, class_prefix: class_prefix, formatted: formatted) end # 配列は要素一つずつで再帰する if obj.is_a?(Array) arr = [] obj.each do |item| arr.push(to_h(obj: item, class_prefix: class_prefix, formatted: formatted)) end return arr end obj end