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



45
46
47
# File 'lib/freyja/base.rb', line 45

def self._attributes
  @attributes || []
end

._has_manyObject



53
54
55
# File 'lib/freyja/base.rb', line 53

def self._has_many
  @has_many || []
end

._has_oneObject



49
50
51
# File 'lib/freyja/base.rb', line 49

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

.default(attribute) ⇒ Object



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

def self.default(attribute)
  make_sure_defaults_is_set

  defaults[attribute]
end

.defaultsObject



13
14
15
# File 'lib/freyja/base.rb', line 13

def self.defaults
  @defaults
end

.has_many(attribute, translator = nil) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/freyja/base.rb', line 37

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

.has_one(attribute, translator = nil) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/freyja/base.rb', line 29

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

.set_default(attribute, value) ⇒ Object



17
18
19
20
21
# File 'lib/freyja/base.rb', line 17

def self.set_default(attribute, value)
  make_sure_defaults_is_set

  @defaults[attribute] = value
end

Instance Method Details

#as_jsonObject



57
58
59
60
61
62
# File 'lib/freyja/base.rb', line 57

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