Class: Freyja::Base

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source) ⇒ Base

Returns a new instance of Base.



5
6
7
# File 'lib/freyja/base.rb', line 5

def initialize(source)
  self.source = ActiveSupport::HashWithIndifferentAccess.new(source)
end

Instance Attribute Details

#sourceObject

Returns the value of attribute source.



3
4
5
# File 'lib/freyja/base.rb', line 3

def source
  @source
end

Class Method Details

._attributesObject



29
30
31
# File 'lib/freyja/base.rb', line 29

def self._attributes
  @attributes || []
end

._has_manyObject



37
38
39
# File 'lib/freyja/base.rb', line 37

def self._has_many
  @has_many || []
end

._has_oneObject



33
34
35
# File 'lib/freyja/base.rb', line 33

def self._has_one
  @has_one || []
end

.attributes(*args) ⇒ Object



9
10
11
# File 'lib/freyja/base.rb', line 9

def self.attributes(*args)
  @attributes = args
end

.has_many(attribute, translator = nil) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/freyja/base.rb', line 21

def self.has_many(attribute, translator = nil)
  @has_many ||= []
  @has_many << {
    attribute: attribute,
    class: translator,
  }
end

.has_one(attribute, translator = nil) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/freyja/base.rb', line 13

def self.has_one(attribute, translator = nil)
  @has_one ||= []
  @has_one << {
    attribute: attribute,
    class: translator,
  }
end

Instance Method Details

#as_jsonObject



41
42
43
44
45
46
# File 'lib/freyja/base.rb', line 41

def as_json
  result = translate_attributes
  result.merge!(translate_has_one)
  result.merge!(translate_has_many)
  result
end