Class: PuppetForgeServer::Models::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet_forge_server/models/builder.rb

Direct Known Subclasses

Dependency, Metadata, Module

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Builder

Returns a new instance of Builder.



19
20
21
22
23
# File 'lib/puppet_forge_server/models/builder.rb', line 19

def initialize(attributes={})
  attributes.each do |name, value|
    send("#{name}=", value) unless value.to_s.empty?
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



25
26
27
# File 'lib/puppet_forge_server/models/builder.rb', line 25

def method_missing (method_name, *args, &block)
  PuppetForgeServer::Logger.get.error "Method #{method_name} with args #{args} not found in #{self.class.to_s}" unless method_name == :to_ary
end

Instance Method Details

#to_hash(obj = self) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/puppet_forge_server/models/builder.rb', line 29

def to_hash(obj = self)
  # Quickly return if it's not one of ours
  unless obj.kind_of? Builder
    return obj
  end
  # Otherwise start building hash
  hash = {}
  obj.instance_variables.each do |var|
    var_value = obj.instance_variable_get(var)
    hash[var.to_s.delete('@')] = if var_value.kind_of?(Array)
      var_value.map {|v| to_hash(v)}
    elsif var_value.respond_to?(:to_hash)
     var_value.to_hash
    else
      var_value
    end
  end
  hash
end