Class: Eeml::Environment

Inherits:
Object show all
Defined in:
lib/eeml/environment.rb

Overview

This class represents the main point of entry to this library. In general we will be creating instances of this class, either by manually specifying the parameters, or probably more commonly, by passing in either an EEML or JSON formatted string which will be parsed to initialize the object.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Environment

Create a new Environment object by passing parameters in a hash (just like creating an ActiveRecord object)



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/eeml/environment.rb', line 112

def initialize(options = {})
  @datastreams = []
  @identifier = options[:identifier]
  @creator = options[:creator]
  @title = options[:title]
  @status = options[:status]
  @feed_url = options[:feed_url]
  @description = options[:description]
  @website = options[:website]
  @email = options[:email]
  @icon = options[:icon]
  @private = options[:private]
  @csv_version = options[:csv_version]
  @publisher = options[:publisher]
  @tags = []
  @has_tag_element = false
end

Instance Attribute Details

#creatorObject

Returns the value of attribute creator.



102
103
104
# File 'lib/eeml/environment.rb', line 102

def creator
  @creator
end

#csv_versionObject

Returns the value of attribute csv_version.



105
106
107
# File 'lib/eeml/environment.rb', line 105

def csv_version
  @csv_version
end

#datastreamsObject

Returns the value of attribute datastreams.



106
107
108
# File 'lib/eeml/environment.rb', line 106

def datastreams
  @datastreams
end

#descriptionObject

Returns the value of attribute description.



103
104
105
# File 'lib/eeml/environment.rb', line 103

def description
  @description
end

#emailObject

Returns the value of attribute email.



103
104
105
# File 'lib/eeml/environment.rb', line 103

def email
  @email
end

#feed_urlObject

Returns the value of attribute feed_url.



103
104
105
# File 'lib/eeml/environment.rb', line 103

def feed_url
  @feed_url
end

#has_tag_elementObject

Returns the value of attribute has_tag_element.



107
108
109
# File 'lib/eeml/environment.rb', line 107

def has_tag_element
  @has_tag_element
end

#iconObject

Returns the value of attribute icon.



103
104
105
# File 'lib/eeml/environment.rb', line 103

def icon
  @icon
end

#identifierObject

Returns the value of attribute identifier.



102
103
104
# File 'lib/eeml/environment.rb', line 102

def identifier
  @identifier
end

#locationObject

Returns the value of attribute location.



104
105
106
# File 'lib/eeml/environment.rb', line 104

def location
  @location
end

#privateObject

Returns the value of attribute private.



108
109
110
# File 'lib/eeml/environment.rb', line 108

def private
  @private
end

#publisherObject

Returns the value of attribute publisher.



102
103
104
# File 'lib/eeml/environment.rb', line 102

def publisher
  @publisher
end

#statusObject

Returns the value of attribute status.



103
104
105
# File 'lib/eeml/environment.rb', line 103

def status
  @status
end

#tagsObject

Returns the value of attribute tags.



107
108
109
# File 'lib/eeml/environment.rb', line 107

def tags
  @tags
end

#titleObject

Returns the value of attribute title.



103
104
105
# File 'lib/eeml/environment.rb', line 103

def title
  @title
end

#updatedObject

Returns the value of attribute updated.



102
103
104
# File 'lib/eeml/environment.rb', line 102

def updated
  @updated
end

#websiteObject

Returns the value of attribute website.



103
104
105
# File 'lib/eeml/environment.rb', line 103

def website
  @website
end

Class Method Details

.new_from_csv(csv_str, version = nil) ⇒ Object

Create a new Environment object from a CSV string



162
163
164
# File 'lib/eeml/environment.rb', line 162

def self.new_from_csv(csv_str, version = nil)
  EnvironmentBuilder.build_from_csv(csv_str, detect_csv_version(version, csv_str))
end

.new_from_eeml(xml_str) ⇒ Object

Create a new Environment object from an EEML string.



152
153
154
# File 'lib/eeml/environment.rb', line 152

def self.new_from_eeml(xml_str)
  EnvironmentBuilder.build_from_xml(xml_str)
end

.new_from_json(json_str) ⇒ Object

Create a new Environment object from a JSON string.



157
158
159
# File 'lib/eeml/environment.rb', line 157

def self.new_from_json(json_str)
  EnvironmentBuilder.build_from_json(json_str)
end

.new_list_from_eeml(xml_str) ⇒ Object

Create multiple Environment objects from an EEML string containing multiple ‘environment` stanzas.



147
148
149
# File 'lib/eeml/environment.rb', line 147

def self.new_list_from_eeml(xml_str)
  EnvironmentBuilder.build_list_from_xml(xml_str)
end

Instance Method Details

#add_datastream(datastream) ⇒ Object



130
131
132
133
134
135
136
# File 'lib/eeml/environment.rb', line 130

def add_datastream(datastream)
  #TODO: consider checking for unique identifier
  datastreams << datastream
  if dups = datastreams.map(&:identifier).compact.uniq!
    raise Eeml::Exceptions::DuplicateDataStreams, "Duplicate Datastream IDs: #{dups.join(', ')}"
  end
end

#add_datastreams(array) ⇒ Object



138
139
140
# File 'lib/eeml/environment.rb', line 138

def add_datastreams(array)
  array.each { |d| add_datastream d }
end

#remove_last_datastreamObject



142
143
144
# File 'lib/eeml/environment.rb', line 142

def remove_last_datastream
  datastreams.pop
end

#update_datastreams_from_csv_values!(csv_values) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/eeml/environment.rb', line 166

def update_datastreams_from_csv_values!(csv_values)

  csv_values.each_with_index do |new_val, index|
    datastream = datastreams[index]
    #if we don't have an existing datastream at same index, create new one
    if datastream.nil?
      datastream = DataStream.new(:identifier => index, :ordering => index)
      datastream.add_value(Value.new(:value => new_val))
      self.datastreams << datastream
    else
      #we had a match, so update the existing datastream
      datastream.values.last.value = new_val
    end
  end

  # if the csv specifies FEWER feeds than the env currently has.
  if csv_values.size < self.datastreams.size
    # remove the excess from the end of the env's list.
    num_extra = datastreams.size - csv_values.size
    1.upto(num_extra) { remove_last_datastream }
  end
end