Simple class to allow basic serialization for the server and more importantly, for CompatibleOne.
Denotes an int index
Denotes a string index
Override of ::attr_accessor so we can track attr_accessors
+*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
Override of ::attr_reader so we can track attr_readers
+*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
Get attributes of this class and of it’s parent classes
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 for this class dependant on role
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
Add a symbol with the specified index
symbol - Attribute we will be indexing in
type - One of STRING_INDEX or INT_INDEX
# File lib/onapp_market/api/serialize.rb, line 80 def self.index(symbol, type) @indexes ||= {} @indexes[symbol] = type end
Get all indexes (incl. parent classes)
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
Define a series of int indexes
* +*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
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
Get the ‘table’, ‘namespace’, or ‘bucket’ for this type Override in derived classes to allow all children to share the same ‘namespace’
# File lib/onapp_market/api/serialize.rb, line 37 def self.namespace self::TYPE end
Create new instance of a serializable class
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
Define a series of string indexes
* +*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
# File lib/onapp_market/api/serialize.rb, line 41 def accessors @accessors end
Get all attributes associated with this class and of it’s parent class
Array of attribute names
# File lib/onapp_market/api/serialize.rb, line 147 def attributes self.class.attributes end
Get the accessors for this class dependant on role (those which have values)
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
Get all indexes (incl. parent classes)
Hash [attribute => index type]
# File lib/onapp_market/api/serialize.rb, line 115 def indexes self.class.indexes end
Basic ‘required’ validation helper
+*args+ - :all - all attributes, :exclude => [list of exclusions], :name - include attribute with given ‘name’
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
Allow a class to restrict which attributes are serialized dependant on the role of the current user
symbol - Symbol or symbols to
be restricted
roles - Roles or roles this symbol will be supported
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