Class: Honeybee::ModelObject

Inherits:
Object
  • Object
show all
Defined in:
lib/honeybee/model_object.rb,
lib/to_openstudio/model_object.rb,
lib/from_openstudio/model_object.rb

Constant Summary collapse

@@encoding_options =
{
  :invalid           => :replace,  # Replace invalid byte sequences
  :undef             => :replace,  # Replace anything not defined in ASCII
  :replace           => '',        # Use a blank for those replacements
  :universal_newline => true       # Always break lines with \n
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ ModelObject

Load ModelObject from symbolized hash



89
90
91
92
93
94
95
96
97
98
# File 'lib/honeybee/model_object.rb', line 89

def initialize(hash)
  # initialize class variable @@extension only once
  @@extension ||= Extension.new
  @@schema ||= @@extension.schema

  @hash = hash
  @type = @hash[:type]
  raise 'Unknown type' if @type.nil?
  raise "Incorrect model object type '#{@type}' for '#{self.class.name}'" unless allowable_types.include?(@type)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/honeybee/model_object.rb', line 61

def method_missing(sym, *args)
  name = sym.to_s
  aname = name.sub('=', '')
  asym = aname.to_sym
  is_getter = args.empty? && @hash.key?(asym)
  is_setter = (name != aname) && (args.size == 1) && @hash.key?(asym)

  if is_getter
    return @hash[asym]
  elsif is_setter
    return @hash[asym] = args[0]
  end

  # do the regular thing
  super
end

Instance Attribute Details

#errorsObject (readonly)

Base class from which all other objects in this module inherit. Attributes and methods of this class should be overwritten in each inheriting object.



51
52
53
# File 'lib/honeybee/model_object.rb', line 51

def errors
  @errors
end

#openstudio_objectObject (readonly)

Returns the value of attribute openstudio_object.



39
40
41
# File 'lib/to_openstudio/model_object.rb', line 39

def openstudio_object
  @openstudio_object
end

#warningsObject (readonly)

Base class from which all other objects in this module inherit. Attributes and methods of this class should be overwritten in each inheriting object.



51
52
53
# File 'lib/honeybee/model_object.rb', line 51

def warnings
  @warnings
end

Class Method Details

.clean_identifier(str) ⇒ Object

remove illegal characters in identifier



120
121
122
123
124
# File 'lib/honeybee/model_object.rb', line 120

def self.clean_identifier(str)
  encode_str = str.encode(Encoding.find('ASCII'), **@@encoding_options)
  encode_str = truncate(encode_str, 97)
  encode_str.gsub(/[^.A-Za-z0-9_-]/, '_').gsub(' ', '_')
end

.clean_name(str) ⇒ Object

remove illegal characters in identifier



114
115
116
117
# File 'lib/honeybee/model_object.rb', line 114

def self.clean_name(str)
  ascii = str.encode(Encoding.find('ASCII'), **@@encoding_options)
  ascii = truncate(ascii, 97)
end

.read_from_disk(file) ⇒ Object

Read ModelObject JSON from disk



79
80
81
82
83
84
85
86
# File 'lib/honeybee/model_object.rb', line 79

def self.read_from_disk(file)
  hash = nil
  File.open(File.join(file), 'r') do |f|
    hash = JSON.parse(File.read(f, :external_encoding => 'UTF-8',
      :internal_encoding => 'UTF-8'), symbolize_names: true, encoding: 'UTF-8')
  end
  new(hash)
end

.truncate(string, max) ⇒ Object



109
110
111
# File 'lib/honeybee/model_object.rb', line 109

def self.truncate(string, max)
  string.length > max ? "#{string[0...max]}..." : string
end

Instance Method Details

#allowable_typesObject



100
101
102
# File 'lib/honeybee/model_object.rb', line 100

def allowable_types
  [self.class.name.split('::').last]
end

#defaultsObject

hash containing the object defaults taken from the open API schema



105
106
107
# File 'lib/honeybee/model_object.rb', line 105

def defaults
  raise 'defaults not implemented for ModelObject, override in your class'
end

#find_existing_openstudio_object(openstudio_model) ⇒ Object

find an equivalent existing object in the openstudio model, return nil if not found



42
43
44
# File 'lib/to_openstudio/model_object.rb', line 42

def find_existing_openstudio_object(openstudio_model)
  raise 'find_existing_openstudio_object is not yet implemented for this ModelObject.'
end

#to_openstudio(openstudio_model) ⇒ Object

create a new object in the openstudio model and return it



47
48
49
# File 'lib/to_openstudio/model_object.rb', line 47

def to_openstudio(openstudio_model)
  raise 'to_openstudio is not yet implemented for this ModelObject.'
end