Class: MC2P::ObjectItem

Inherits:
ObjectItemMixin show all
Defined in:
lib/base.rb

Overview

Object item - class used to wrap the data from API that represent an item

Direct Known Subclasses

ReadOnlyObjectItem

Instance Attribute Summary

Attributes inherited from ObjectItemMixin

#_deleted, #json_dict, #resource

Instance Method Summary collapse

Methods inherited from ObjectItemMixin

#id_required_and_not_deleted, #to_s

Constructor Details

#initialize(json_dict, resource) ⇒ ObjectItem

Initializes an object item Params:

json_dict

Data of the object

resource

Resource used to delete, save, create or retrieve the object



42
43
44
45
46
47
# File 'lib/base.rb', line 42

def initialize(json_dict, resource)
  @json_dict = json_dict.nil? ? {} : json_dict
  @resource = resource
  @_deleted = false
  @id_property = 'id'
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(key, *args) ⇒ Object

Allows use the following syntax to get a field of the object:

obj.name

Params:

key

Field to return

Returns: Value of the field from json_dict



54
55
56
# File 'lib/base.rb', line 54

def method_missing(key, *args)
  @json_dict.include?(key.to_s) ? @json_dict[key.to_s] : super
end

Instance Method Details

#respond_to?(key, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/base.rb', line 62

def respond_to?(key, include_private = false)
  @json_dict.include?(key.to_s) || super
end

#respond_to_missing?(key, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/base.rb', line 58

def respond_to_missing?(key, include_private = false)
  @json_dict.include?(key.to_s) || super
end

#set(key, value) ⇒ Object

Allows use the following syntax to set a field of the object:

obj.name = 'example'

Params:

key

Field to change

value

Content to replace the current value



71
72
73
# File 'lib/base.rb', line 71

def set(key, value)
  @json_dict[key] = value
end