Class: JSON::Pie::ResourceIdentity
- Inherits:
-
Object
- Object
- JSON::Pie::ResourceIdentity
- Defined in:
- lib/json/pie/resource_identity.rb
Instance Attribute Summary collapse
-
#instance ⇒ Object
readonly
Returns the value of attribute instance.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(type:, id: nil, **options) ⇒ ResourceIdentity
constructor
A new instance of ResourceIdentity.
Constructor Details
#initialize(type:, id: nil, **options) ⇒ ResourceIdentity
Returns a new instance of ResourceIdentity.
14 15 16 17 18 19 20 |
# File 'lib/json/pie/resource_identity.rb', line 14 def initialize(type:, id: nil, **) @options = klass = determine_type(type).to_s.classify.constantize @instance = klass.find_or_initialize_by klass.primary_key => id rescue NameError raise JSON::Pie::InvalidType, "#{type}(#{id})" end |
Instance Attribute Details
#instance ⇒ Object (readonly)
Returns the value of attribute instance.
6 7 8 |
# File 'lib/json/pie/resource_identity.rb', line 6 def instance @instance end |
Class Method Details
.find_or_initialize(type:, id: nil, **options) ⇒ Object
8 9 10 11 12 |
# File 'lib/json/pie/resource_identity.rb', line 8 def self.find_or_initialize(type:, id: nil, **) new(type: type, id: id, **).instance rescue ArgumentError raise JSON::Pie::MissingType end |