Class: Bright::Model

Inherits:
Object show all
Defined in:
lib/bright/model.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Model

Returns a new instance of Model.


5
6
7
8
9
# File 'lib/bright/model.rb', line 5

def initialize(attributes = {})
  assign_attributes(attributes) if attributes

  super()
end

Class Method Details

.attribute_namesObject


21
22
23
# File 'lib/bright/model.rb', line 21

def self.attribute_names
  @attribute_names
end

Instance Method Details

#assign_attributes(new_attributes) ⇒ Object


11
12
13
14
15
16
17
18
19
# File 'lib/bright/model.rb', line 11

def assign_attributes(new_attributes)
  if !new_attributes.is_a?(Hash)
    raise ArgumentError, "When assigning attributes, you must pass a hash as an argument."
  end
  return if new_attributes.empty?

  attributes = new_attributes.collect { |k, v| [k.to_sym, v] }.to_h
  _assign_attributes(attributes)
end

#to_jsonObject


25
26
27
28
29
# File 'lib/bright/model.rb', line 25

def to_json
  self.class.attribute_names.collect do |n|
    [n, send(n)]
  end.to_h.to_json
end