Class: Jabara::Transformer::Flatten
- Inherits:
-
Object
- Object
- Jabara::Transformer::Flatten
- Defined in:
- lib/jabara/transformer/flatten.rb
Overview
TODO array と set のflat化は別クラスに責務を切り出した方がいいかも
Instance Method Summary collapse
-
#convert(object_repr) ⇒ Object
与えられたオブジェクトを第1正規化する.
-
#initialize(id_key_name: 'id', index_key_name: 'index') ⇒ Flatten
constructor
A new instance of Flatten.
Constructor Details
#initialize(id_key_name: 'id', index_key_name: 'index') ⇒ Flatten
8 9 10 11 |
# File 'lib/jabara/transformer/flatten.rb', line 8 def initialize(id_key_name: 'id', index_key_name: 'index') @id_key_name = id_key_name @index_key_name = index_key_name end |
Instance Method Details
#convert(object_repr) ⇒ Object
与えられたオブジェクトを第1正規化する
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/jabara/transformer/flatten.rb', line 14 def convert(object_repr) new_repr_hash = {} divided_reprs = [] id = ::.id(object_repr) ::.data(object_repr).each do |key, repr| data = ::.data(repr) if ::.primitive?(repr) then new_repr_hash[key] = repr else tag = ::.tag(repr) case tag when :object _object_repr = data ::.data(_object_repr)[@id_key_name] = id divided_reprs.push(_object_repr) when :array # TODO ヘテロな要素が来るケースへの対応 # TODO primitiveな要素が来るケースへの対応 ar = data ar.each_with_index do |_object_repr, index| ::.data(_object_repr)[@id_key_name] = id ::.data(_object_repr)[@index_key_name] = index divided_reprs.push(_object_repr) end when :set # TODO ヘテロな要素が来るケースへの対応 # TODO primitiveな要素が来るケースへの対応 st = data st.each do |_object_repr| ::.data(_object_repr)[@id_key_name] = id divided_reprs.push(_object_repr) end else raise ArgumentError, "Invalid tag was found :%s" % tag end end end ::.set_data(object_repr, new_repr_hash) return object_repr, divided_reprs end |