Module: Cda::Utility
Instance Method Summary collapse
- #annotations(context, attribute) ⇒ Object
- #attribute_class(context, attribute) ⇒ Object
- #collection?(context, attribute) ⇒ Boolean
- #hash_to_object(hash) ⇒ Object
- #inference(path, value, context) ⇒ Object
- #merge_json(x, y) ⇒ Object
- #mk_class(attrs) ⇒ Object
- #object_to_hash(object) ⇒ Object
Instance Method Details
#annotations(context, attribute) ⇒ Object
42 43 44 |
# File 'lib/cda/utility.rb', line 42 def annotations(context, attribute) context.attribute_set[attribute.to_sym].annotations end |
#attribute_class(context, attribute) ⇒ Object
31 32 33 34 35 36 |
# File 'lib/cda/utility.rb', line 31 def attribute_class(context, attribute) attribute_decl = context.attribute_set[attribute.to_sym] type = attribute_decl.respond_to?(:member_type) ? attribute_decl.member_type : attribute_decl.type type.primitive end |
#collection?(context, attribute) ⇒ Boolean
38 39 40 |
# File 'lib/cda/utility.rb', line 38 def collection?(context, attribute) annotations(context, attribute)[:multiple] end |
#hash_to_object(hash) ⇒ Object
50 51 52 53 54 55 |
# File 'lib/cda/utility.rb', line 50 def hash_to_object(hash) return hash unless Hash === hash is_array = !hash.empty? && hash.keys.all? { |x| x.is_a?(Integer) } acc = is_array ? [] : {} hash.reduce(acc) { |acc, (k, v)| acc[k] = hash_to_object(v); acc } end |
#inference(path, value, context) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/cda/utility.rb', line 5 def inference(path, value, context) # puts if self == context # puts "inference(#{path}, #{value}, #{context})" return value if path.empty? name = path.shift if path.empty? && value.is_a?(Hash) && value.key?(:_type) klass = value[:_type].constantize is_collection = false else klass = attribute_class(context, name) is_collection = collection?(context, name) end value = inference(path, value, klass) if is_collection {name => (value.is_a?(Array) ? value : [value])} elsif value.is_a?(Array) value.map { |val| {name => val} } else {name => value} end end |
#merge_json(x, y) ⇒ Object
46 47 48 |
# File 'lib/cda/utility.rb', line 46 def merge_json(x, y) hash_to_object object_to_hash(x).deep_merge(object_to_hash(y)) end |
#mk_class(attrs) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/cda/utility.rb', line 79 def mk_class(attrs) return attrs unless attrs.is_a?(Hash) res = attrs.reduce({}) do |acc, (k, v)| acc[k.to_sym] = case v when Hash mk_class(v) when Array v.map { |vv| mk_class(vv) } else v end acc end if res.key?(:_type) res.delete(:_type).constantize.new res else res end end |
#object_to_hash(object) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/cda/utility.rb', line 57 def object_to_hash(object) case object when Array if object.size == 1 value = object_to_hash(object[0]) Hash.new { |h, k| k.is_a?(Integer) ? h[k] = value : nil }.merge(0 => value) else object.each_with_index.reduce({}) do |acc, (elem, index)| acc[index] = object_to_hash(elem) acc end end when Hash object.reduce({}) do |acc, (key, value)| acc[key] = object_to_hash(value) acc end else object end end |