Class: SimpleOpencBot::BaseLicenceRecord
- Inherits:
-
Object
- Object
- SimpleOpencBot::BaseLicenceRecord
- Defined in:
- lib/simple_openc_bot.rb
Direct Known Subclasses
Class Method Summary collapse
Instance Method Summary collapse
-
#errors ⇒ Object
return a structure including errors if invalid; otherwise return nil.
-
#initialize(attrs = {}) ⇒ BaseLicenceRecord
constructor
A new instance of BaseLicenceRecord.
- #to_hash ⇒ Object
- #validate_instance! ⇒ Object
Constructor Details
#initialize(attrs = {}) ⇒ BaseLicenceRecord
Returns a new instance of BaseLicenceRecord.
230 231 232 233 234 235 236 237 |
# File 'lib/simple_openc_bot.rb', line 230 def initialize(attrs={}) validate_instance! attrs = attrs.with_indifferent_access self._type = self.class.name self._store_fields.each do |k| send("#{k}=", attrs[k]) end end |
Class Method Details
.schema(schema) ⇒ Object
225 226 227 228 |
# File 'lib/simple_openc_bot.rb', line 225 def self.schema(schema) hyphenated_name = schema.to_s.gsub("_", "-") self._schema = File.("../../schemas/schemas/#{hyphenated_name}-schema.json", __FILE__) end |
.store_fields(*fields) ⇒ Object
210 211 212 213 214 215 216 217 218 |
# File 'lib/simple_openc_bot.rb', line 210 def self.store_fields(*fields) self._store_fields ||= [] self._store_fields.concat(fields) fields << :_last_exported_at unless _store_fields.include?(:_last_exported_at) fields << :_last_updated_at unless _store_fields.include?(:_last_updated_at) fields.each do |field| attr_accessor field end end |
.unique_fields(*fields) ⇒ Object
220 221 222 223 |
# File 'lib/simple_openc_bot.rb', line 220 def self.unique_fields(*fields) self._unique_fields = fields unless fields.empty? self._unique_fields end |
Instance Method Details
#errors ⇒ Object
return a structure including errors if invalid; otherwise return nil
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
# File 'lib/simple_openc_bot.rb', line 272 def errors data = self.to_pipeline if data if !self._schema # backwards compatibility self._schema = File.("../../schemas/schemas/licence-schema.json", __FILE__) end errors = JSON::Validator.fully_validate( self._schema, data.to_json, {:errors_as_objects => true}) if !errors.empty? data[:errors] = errors data end end end |
#to_hash ⇒ Object
264 265 266 267 268 269 |
# File 'lib/simple_openc_bot.rb', line 264 def to_hash hsh = Hash[_store_fields.map{|field| [field, send(field)]}] hsh[:_type] = self.class.name hsh[:_last_updated_at] = last_updated_at hsh end |
#validate_instance! ⇒ Object
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
# File 'lib/simple_openc_bot.rb', line 239 def validate_instance! all_errors = [] required_functions = [:last_updated_at, :to_pipeline] func_errors = [] required_functions.each do |func| if !respond_to?(func) func_errors << func end end if !func_errors.empty? all_errors << "You must define the following functions in your record class: #{func_errors.join(', ')}" end field_errors = [] required_fields = [:_store_fields, :_unique_fields, :_schema] required_fields.each do |f| if !send(f) field_errors << f.to_s[1..-1] end end if !field_errors.empty? all_errors << "You must define the following fields on your record class: #{field_errors.join(', ')}" end raise all_errors.join('\n') unless all_errors.empty? end |