module Nis::Mixin::Struct

Public Instance Methods

[](attr) click to toggle source

@param [Symbol, String] attr Attribute name @return [Any] Attribute value

# File lib/nis/mixin/struct.rb, line 6
def [](attr)
  send(attr)
end
to_hash() click to toggle source

@return [Hash] Attribute and value pairs

# File lib/nis/mixin/struct.rb, line 11
def to_hash
  hashed_properties = instance_variables.each_with_object({}) do |var, hash|
    hash[var.to_s.delete('@').to_sym] = instance_variable_get(var)
  end
  hashnize(hashed_properties)
end
to_json(state = nil) click to toggle source

@return [String] JSON formatted structure

# File lib/nis/mixin/struct.rb, line 19
def to_json(state = nil)
  to_hash.to_json(state)
end

Private Instance Methods

hashnize(obj) click to toggle source
# File lib/nis/mixin/struct.rb, line 25
def hashnize(obj)
  case true
  when obj.class.to_s.start_with?('Nis::Fee')
    obj.to_i
  when obj.class.to_s.start_with?('Nis::Unit')
    obj.to_s
  when obj.class.to_s.start_with?('Nis::Transaction')
    obj.to_hash
  when obj.class.to_s.start_with?('Nis::Struct')
    obj.to_hash
  when obj.class.to_s.start_with?('Nis::Keypair')
    obj.to_hash
  when obj.is_a?(Array)
    obj.map { |el| hashnize(el) }
  when obj.is_a?(Hash)
    obj.inject({}) do |hash, (k, v)|
      hash[k] = hashnize(v)
      hash
    end
  else
    obj
  end
end