Sha256: ced5123fd1b2d60945579d9da3584c453d2c719aa42b21259386599e5dd00510

Contents?: true

Size: 1.45 KB

Versions: 1

Compression:

Stored size: 1.45 KB

Contents

module Yao::Resources::Dumper
  class Base

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

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

    def self.dump(obj)
      [obj].flatten.map do |o|
        arr = @fields.map{|field|
          # bugfixされるまで特定のメソッドは `_id` をつける
          if [@bug_fields].flatten.include?(field)
            name = field + "_id"
            [name, o.send("[]", name)]
          else
            result = o.send(field)

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

            [field, result]
          end
        }.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
      if result.instance_of?(Array)
        result.size == 1 ? result.first : result
      else
        result
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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