class OnappMarket::API::Serializable

Simple class to allow basic serialization for the server and more importantly, for CompatibleOne.

Constants

INT_INDEX

Denotes an int index

STRING_INDEX

Denotes a string index

Public Class Methods

attr_accessor(*vars) click to toggle source

Override of ::attr_accessor so we can track attr_accessors

Attributes

  • +*vars+ - attribtues to add

# File lib/onapp_market/api/serialize.rb, line 123
def self.attr_accessor(*vars)
  @attributes ||= []
  @attributes.concat vars
  super(*vars)
end
attr_reader(*vars) click to toggle source

Override of ::attr_reader so we can track attr_readers

Attributes

  • +*vars+ - attribtues to add

# File lib/onapp_market/api/serialize.rb, line 155
def self.attr_reader(*vars)
  @attributes ||= []
  @attributes.concat vars
  super(*vars)
end
attributes() click to toggle source

Get attributes of this class and of it’s parent classes

Returns

  • Array of attribute names

# File lib/onapp_market/api/serialize.rb, line 133
def self.attributes
  @attributes ||= []
  # Our local attributes
  attrs = @attributes
  # Merge in our parent attributes
  attrs.concat(superclass.attributes) if superclass.ancestors.include? Serializable and superclass.attributes != nil
  # Remove duplicates
  attrs.uniq if attrs != nil
end
get_all_accessors(role = :all) click to toggle source

Get all accessors for this class dependant on role

Attribtues

  • role e.g. :admin or :supplier

# File lib/onapp_market/api/serialize.rb, line 194
def self.get_all_accessors(role = :all)
  begin
    obj = self.new
    # Does rails exists
    is_market = true
    begin
      OnappMarketService
    rescue
      is_market = false
    end
    result  = self.attributes.reject do |attr|
      true if ([:accessors, :api].include? attr) or (is_market and obj.accessors.include?(attr) and !obj.accessors[attr].include?(role) and role != :all)
    end
    return result
  #rescue
  #  raise "Could not get accessors for type #{self}"
  end
  return []
end
index(symbol, type) click to toggle source

Add a symbol with the specified index

Attributes

# File lib/onapp_market/api/serialize.rb, line 80
def self.index(symbol, type)
  @indexes ||= {}
  @indexes[symbol] = type
end
indexes() click to toggle source

Get all indexes (incl. parent classes)

Returns

Hash [attribute => index type]

# File lib/onapp_market/api/serialize.rb, line 102
def self.indexes
  # Our local attributes
  indexes = @indexes
  indexes ||= {}
  # Merge in our parent attributes
  indexes = indexes.merge(superclass.indexes) if superclass.ancestors.include? Serializable and superclass.indexes != nil
  indexes
end
int_indexes(*vars) click to toggle source

Define a series of int indexes

Attributes

* +*vars+ - attributes
# File lib/onapp_market/api/serialize.rb, line 71
def self.int_indexes(*vars)
  vars.each { |v| self.index(v, INT_INDEX)}
end
method_missing(sym_method, *args, &block) click to toggle source

Handle adding ‘loosely’ related classes e.g. in OnappMarket::OnApp::HypervisorZone pricing OnappMarket::OnApp::HypervisorZonePricing and later

OnappMarket::OnApp::HypervisorZone::pricing gives OnappMarket::OnApp::HypervisorZonePricing
# File lib/onapp_market/api/serialize.rb, line 91
def self.method_missing(sym_method, *args, &block)
  @related ||= {}
  return @related[sym_method] if args.count == 0
  return @related[sym_method] = args[0] if args.count == 1
  raise "OnappMarket::API::Serializable#method_missing - Incorrect number of arguments #{args.count} [#{args.inspect}]"
end
namespace() click to toggle source

Get the ‘table’, ‘namespace’, or ‘bucket’ for this type Override in derived classes to allow all children to share the same ‘namespace’

Returns

String

# File lib/onapp_market/api/serialize.rb, line 37
def self.namespace
  self::TYPE
end
new(hash = {}, type = nil) { |self| ... } click to toggle source

Create new instance of a serializable class

Attributes

  • hash - Hash of attributes to apply

  • type - type for serialization help

# File lib/onapp_market/api/serialize.rb, line 17
def initialize(hash = {}, type = nil)
  raise "No type defined for Serializable class" if type == nil
  # Set the type
  @type = type
  # Restricted accessors
  @accessors = {}
  # Write all values from the hash
  hash.map do |(k, v)| 
    writer_m = "#{k}="
    send(writer_m, v) if respond_to?(writer_m) 
  end
  # Process block if provided
  yield self if block_given?
end
string_indexes(*vars) click to toggle source

Define a series of string indexes

Attributes

* +*vars+ - attributes
# File lib/onapp_market/api/serialize.rb, line 63
def self.string_indexes(*vars)
  vars.each { |v| self.index(v, STRING_INDEX)}
end

Public Instance Methods

accessors() click to toggle source
# File lib/onapp_market/api/serialize.rb, line 41
def accessors
  @accessors
end
attributes() click to toggle source

Get all attributes associated with this class and of it’s parent class

Returns

  • Array of attribute names

# File lib/onapp_market/api/serialize.rb, line 147
def attributes
  self.class.attributes
end
get_accessors(role) click to toggle source

Get the accessors for this class dependant on role (those which have values)

Attributes

  • role - e.g. :admin or :supplier

# File lib/onapp_market/api/serialize.rb, line 218
def get_accessors(role)
  # Does rails exists
  is_market = true
  begin
    OnappMarketService
  rescue
    is_market = false
  end
  # Build array map on all instance variables
  array = Array(self.instance_variables.map { |name| name = name.to_s[1..-1].to_sym }).reject do |accessor|
    # Filter out :accessors & :api and also any accessors restricted for which the role is not present
    # We only do this is Rails is present - i.e. server side. It is meaningless client side.
    true if ([:accessors, :api].include? accessor) or (is_market and @accessors.include?(accessor) and !@accessors[accessor].include?(role) and role != :all)
  end
end
indexes() click to toggle source

Get all indexes (incl. parent classes)

Returns

Hash [attribute => index type]

# File lib/onapp_market/api/serialize.rb, line 115
def indexes
  self.class.indexes
end
require(*args) click to toggle source

Basic ‘required’ validation helper

Attributes

  • +*args+ - :all - all attributes, :exclude => [list of exclusions], :name - include attribute with given ‘name’

Returns

  • Array of missing attributes (empty if none missing)

# File lib/onapp_market/api/serialize.rb, line 166
def require(*args)
  required = []
  # Always exclude these
  exclude = [:accessors, :api, :db_id]
  args.each do |arg|
    arg, value = arg.shift if arg.is_a? Hash
    case arg
      # Add all attributes
    when :all
      required = attributes
      # Add attributes to the list of exclusions
    when :exclude
      exclude = (value.is_a?(Array) ? exclude.concat(value) : exclude.push(value)) unless value == nil
    else
      # Push the attribute to the required list
      required.push(arg)
    end
  end
  not_present = []
  # Create the not present list (removing anything on the exclude list)
  required.each { |v| not_present.push(v) if !self.instance_variable_defined?("@#{v.to_s}") and !exclude.include? v }
  not_present
end
restrict(symbol, roles) click to toggle source

Allow a class to restrict which attributes are serialized dependant on the role of the current user

Attributes

  • symbol - Symbol or symbols to be restricted

  • roles - Roles or roles this symbol will be supported

Notes

This is only applicable for server side use of the library

# File lib/onapp_market/api/serialize.rb, line 53
def restrict(symbol, roles)
  symbol = [symbol] unless symbol.is_a? Array
  roles = [roles] unless roles.is_a? Array
  symbol.each {|x| @accessors[x] = roles}
end