Class: Bio::BaseSpace::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/basespace/model.rb

Overview

Base class for all BaseSpace Ruby SDK model classes. Implements a basic key/value store and provides convenience methods for accessing the key/value store using ‘method_missing` magic.

Keys in this model are referred to as “attribute names”, whereas values are called “attributes”.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeModel

Create a new (empty) model.



27
28
29
30
# File 'lib/basespace/model.rb', line 27

def initialize
  @swagger_types = {}
  @attributes = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

If a method was called on the object for which no implementations is provided, then execute this method and try to return the attribute value whose attribute key matches the method call’s name.

method

Method call for which no implementation could be found.

args

Arguments that were provided to the method call.

block

If not nil, code block that follows the method call.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/basespace/model.rb', line 39

def method_missing(method, *args, &block)
  attr_name = method.to_s.downcase.gsub('_', '')
  attr_value = false
  self.attributes.each do |key, value|
    if key.downcase == attr_name
      attr_value = value  # can be an object or nil
    end
  end
  if attr_value == false
    super
  else
    return attr_value
  end
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



24
25
26
# File 'lib/basespace/model.rb', line 24

def attributes
  @attributes
end

#swagger_typesObject (readonly)

Returns the value of attribute swagger_types.



24
25
26
# File 'lib/basespace/model.rb', line 24

def swagger_types
  @swagger_types
end

Instance Method Details

#get_attr(key) ⇒ Object

Returns the value, if any, of the given attribute name.

key

Attribute name whose value should be returned.



67
68
69
# File 'lib/basespace/model.rb', line 67

def get_attr(key)
  return @attributes[key]
end

#set_attr(key, value) ⇒ Object

Sets the value of a named attribute. Overrides the value of a previous assignment.

key

Attribute name whose value should be set.

value

Value that should be assigned.



59
60
61
62
# File 'lib/basespace/model.rb', line 59

def set_attr(key, value)
  @attributes[key] = value
  return @attributes
end

#to_sObject

Returns a string representation of the model.



72
73
74
# File 'lib/basespace/model.rb', line 72

def to_s
  return self.inspect
end