Class: Nzbn::Models::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/nzbn/models/base.rb

Overview

Base model class with common functionality

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Base

Returns a new instance of Base.



7
8
9
10
11
12
# File 'lib/nzbn/models/base.rb', line 7

def initialize(attributes = {})
  attributes.each do |key, value|
    setter = "#{underscore(key.to_s)}="
    send(setter, value) if respond_to?(setter)
  end
end

Instance Method Details

#to_hObject



14
15
16
17
18
19
20
# File 'lib/nzbn/models/base.rb', line 14

def to_h
  instance_variables.each_with_object({}) do |var, hash|
    key = var.to_s.delete('@')
    value = instance_variable_get(var)
    hash[key] = serialize_value(value)
  end
end

#to_json(*args) ⇒ Object



22
23
24
# File 'lib/nzbn/models/base.rb', line 22

def to_json(*args)
  to_h.to_json(*args)
end