Class: StrongJSON::Type::Object
- Inherits:
-
Object
- Object
- StrongJSON::Type::Object
- Defined in:
- lib/strong_json/type.rb
Instance Method Summary collapse
- #coerce(object, path: []) ⇒ Object
-
#initialize(fields) ⇒ Object
constructor
A new instance of Object.
- #merge(fields) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(fields) ⇒ Object
Returns a new instance of Object.
76 77 78 |
# File 'lib/strong_json/type.rb', line 76 def initialize(fields) @fields = fields end |
Instance Method Details
#coerce(object, path: []) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/strong_json/type.rb', line 80 def coerce(object, path: []) unless object.is_a?(Hash) raise Error.new(path: path, type: self, value: object) end result = {} @fields.each do |name, ty| value = ty.coerce(object[name], path: path + [name]) result[name] = value if object.has_key?(name) end result end |
#merge(fields) ⇒ Object
95 96 97 98 99 100 101 |
# File 'lib/strong_json/type.rb', line 95 def merge(fields) if fields.is_a?(Object) fields = Object.instance_variable_get("@fields") end Object.new(@fields.merge(fields)) end |
#to_s ⇒ Object
103 104 105 106 107 108 109 110 111 |
# File 'lib/strong_json/type.rb', line 103 def to_s fields = [] @fields.each do |name, type| fields << "#{name}: #{type}" end "object(#{fields.join(', ')})" end |