freyja 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/freyja/base.rb +107 -0
- data/lib/freyja/version.rb +1 -1
- data/lib/freyja.rb +2 -0
- metadata +1 -1
data/lib/freyja/base.rb
CHANGED
@@ -1,3 +1,110 @@
|
|
1
1
|
class Freyja::Base
|
2
2
|
|
3
|
+
attr_accessor :source
|
4
|
+
|
5
|
+
def initialize(source)
|
6
|
+
self.source = ActiveSupport::HashWithIndifferentAccess.new(source)
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.attributes(*args)
|
10
|
+
@attributes = args
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.has_one(attribute, translator = nil)
|
14
|
+
@has_one ||= []
|
15
|
+
@has_one << {
|
16
|
+
attribute: attribute,
|
17
|
+
class: translator,
|
18
|
+
}
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.has_many(attribute, translator = nil)
|
22
|
+
@has_many ||= []
|
23
|
+
@has_many << {
|
24
|
+
attribute: attribute,
|
25
|
+
class: translator,
|
26
|
+
}
|
27
|
+
end
|
28
|
+
|
29
|
+
def self._attributes
|
30
|
+
@attributes || []
|
31
|
+
end
|
32
|
+
|
33
|
+
def self._has_one
|
34
|
+
@has_one || []
|
35
|
+
end
|
36
|
+
|
37
|
+
def self._has_many
|
38
|
+
@has_many || []
|
39
|
+
end
|
40
|
+
|
41
|
+
def as_json
|
42
|
+
result = translate_attributes
|
43
|
+
result.merge!(translate_has_one)
|
44
|
+
result.merge!(translate_has_many)
|
45
|
+
result
|
46
|
+
end
|
47
|
+
|
48
|
+
protected
|
49
|
+
|
50
|
+
def translate_attributes
|
51
|
+
self.class._attributes.each.with_object({}) do |attr, result|
|
52
|
+
if include?(attr)
|
53
|
+
if self.respond_to?(attr)
|
54
|
+
result[attr] = send(attr)
|
55
|
+
else
|
56
|
+
result[attr] = source[attr]
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def translate_has_one
|
63
|
+
self.class._has_one.each.with_object({}) do |association, result|
|
64
|
+
attr = association[:attribute]
|
65
|
+
if include?(attr)
|
66
|
+
if self.respond_to?(attr)
|
67
|
+
value = send(attr)
|
68
|
+
else
|
69
|
+
value = source[attr]
|
70
|
+
end
|
71
|
+
if association[:class]
|
72
|
+
translator = association[:class].new(value)
|
73
|
+
result[attr] = translator.as_json
|
74
|
+
else
|
75
|
+
result[attr] = value
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
def translate_has_many
|
82
|
+
self.class._has_many.each.with_object({}) do |association, result|
|
83
|
+
attr = association[:attribute]
|
84
|
+
if include?(attr)
|
85
|
+
if self.respond_to?(attr)
|
86
|
+
value = send(attr)
|
87
|
+
else
|
88
|
+
value = source[attr]
|
89
|
+
end
|
90
|
+
if association[:class]
|
91
|
+
result[attr] = value.map do |v|
|
92
|
+
translator = association[:class].new(v)
|
93
|
+
translator.as_json
|
94
|
+
end
|
95
|
+
else
|
96
|
+
result[attr] = value
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
def include?(attribute)
|
103
|
+
if respond_to?("include_#{attribute}?")
|
104
|
+
send("include_#{attribute}?")
|
105
|
+
else
|
106
|
+
true
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
3
110
|
end
|
data/lib/freyja/version.rb
CHANGED
data/lib/freyja.rb
CHANGED