module Nis::Util::Assignable
Public Class Methods
new(attributes = {}) { |self| ... }
click to toggle source
# File lib/nis/util/assignable.rb, line 3 def initialize(attributes = {}) attributes.each do |k, v| send("#{k.to_s}=", v) if respond_to?("#{k.to_s}=") end if attributes yield self if block_given? end
Public Instance Methods
[](attr)
click to toggle source
@param [Symbol, String] attr Attribute name @return [Any] Attribute value
# File lib/nis/util/assignable.rb, line 12 def [](attr) send(attr) end
to_hash()
click to toggle source
@return [Hash] Attribute and value pairs
# File lib/nis/util/assignable.rb, line 17 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/util/assignable.rb, line 25 def to_json(state = nil) to_hash.to_json(state) end
Private Instance Methods
hashnize(obj)
click to toggle source
# File lib/nis/util/assignable.rb, line 31 def hashnize(obj) case true when obj.class.to_s.start_with?('Nis::Unit') obj.to_s when obj.class.to_s.start_with?('Nis::Struct') 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