Class: OpenStudio::Metadata::Haystack

Inherits:
Object
  • Object
show all
Defined in:
lib/openstudio/metadata/serializer.rb

Overview

Class to serialize entities into a Haystack JSON

Examples:

Use Haystack to make JSON from list of entities

entities = creator.entities
haystack = Haystack.new
haystack_json = haystack.create_haystack_from_entities(entities)

Instance Method Summary collapse

Instance Method Details

#create_from_entities(entities) ⇒ String

Creates Haystack JSON from list of entities

Parameters:

Returns:

  • (String)

    Haystack JSON representation of entities



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/openstudio/metadata/serializer.rb', line 108

def create_from_entities(entities)
  cols = []
  rows = []
  entities.each do |entity|
    entity.keys.each do |k|
      if k == 'add_tags'
        (tags = entity[k]) && tags.each { |tag| entity.store(tag, ':m') && entity.delete(k) }
      elsif k == 'type'
        (t_tags = entity[k].split('-')) && t_tags.each { |t_tag| entity.store(t_tag, ':m') } && entity.delete(k)
      elsif k == 'relationships'
        relationships = entity[k]
        relationships.each do |relationship, reference|
          entity.store(relationship, reference)
        end
        entity.delete(k)
      end
    end
    rows.append(entity)
  end
  rows.each do |row|
    row.keys.each do |k|
      unless cols.include?('name' => k)
        cols.append('name' => k)
      end
    end
  end
  data = { 'meta' => { 'ver' => '3.0' },
           'cols' => cols,
           'rows' => rows }
  return JSON.pretty_generate(data)
end