Sha256: 923f377f316c18d15a729a1c60b63ff00ee7a4fa87a8927663b558fec18f23c1

Contents?: true

Size: 1.05 KB

Versions: 1

Compression:

Stored size: 1.05 KB

Contents

module Yao::Resources::Dumper
  class Base

    def self.fields=(v)
      @fields = v
    end

    def self.dump(obj)
      [obj].flatten.map do |o|
        arr = @fields.map{|field|
          result = o.send(field)

          result = if result.instance_of?(Array)
                    result.map{|r| resource_dump(r)}
                   else
                    resource_dump(result)
                   end

          [field, result]
        }.flatten(1)

        Hash[*arr]
      end
    end

    private
    def self.resource_dump(obj)
      result = obj
      if m = obj.class.to_s.match(/^Yao::Resources::(.+)$/)
        if obj.id.nil?
          # とりあえずnameを返しておく(Yao::Resources::SecurityGroup)
          result = {"name": obj.name}
        else
          # Yao::ResourcesにマッピングされているがIDのみなので
          # getして情報を取得する
          resource = obj.class.get(obj.id)
          result = Object.const_get("Yao::Resources::Dumper::#{m[1]}").dump(resource)
        end
      end
      result
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
yao-cli-0.1.0 lib/yao/resources/dumper/base.rb