Class: Id::Model::HasOne

Inherits:
Association show all
Defined in:
lib/id/model/has_one.rb

Instance Attribute Summary

Attributes inherited from Field

#model, #name, #options

Instance Method Summary collapse

Methods inherited from Association

#hierarchy, #inferred_class, #inferred_class_name, #type

Methods inherited from Field

#cast, #default, #default?, #default_value, #define, #hook_define, #initialize, #key, #optional?, #presence_of

Constructor Details

This class inherits a constructor from Id::Model::Field

Instance Method Details

#value_of(data) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/id/model/has_one.rb', line 5

def value_of(data)
  if optional?
    child = data.fetch(key, nil)
    child.nil? ? None : Some[type.new(child)]
  else
    child = data.fetch(key) { raise MissingAttributeError, key }
    type.new(child) unless child.nil?
  end
end