Class: Kerbi::Cli::BaseSerializer

Inherits:
Object
  • Object
show all
Defined in:
lib/cli/base_serializer.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, context = {}) ⇒ BaseSerializer

Returns a new instance of BaseSerializer.



9
10
11
12
13
# File 'lib/cli/base_serializer.rb', line 9

def initialize(object, context={})
  @object = object
  @parent_object = context.delete(:parent_object)
  @context = context
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



7
8
9
# File 'lib/cli/base_serializer.rb', line 7

def context
  @context
end

#objectObject (readonly)

Returns the value of attribute object.



5
6
7
# File 'lib/cli/base_serializer.rb', line 5

def object
  @object
end

#parent_objectObject (readonly)

Returns the value of attribute parent_object.



6
7
8
# File 'lib/cli/base_serializer.rb', line 6

def parent_object
  @parent_object
end

Class Method Details

.associationsObject



93
94
95
# File 'lib/cli/base_serializer.rb', line 93

def self.associations
  @associations ||= []
end

.attributesObject



97
98
99
# File 'lib/cli/base_serializer.rb', line 97

def self.attributes
  @attributes ||= []
end

.has_attributes(*attrs) ⇒ Object



101
102
103
# File 'lib/cli/base_serializer.rb', line 101

def self.has_attributes(*attrs)
  @attributes = attrs.map(&:to_sym)
end

.has_many(name, serializer, attr = nil) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/cli/base_serializer.rb', line 71

def self.has_many(name, serializer, attr=nil)
  @associations ||= []
  @associations << {
    name: name.to_sym,
    serializer: serializer,
    attr: attr
  }
end

.has_one(name, serializer, attr = nil) ⇒ Object



80
81
82
83
84
85
86
87
# File 'lib/cli/base_serializer.rb', line 80

def self.has_one(name, serializer, attr=nil)
  @possessions ||= []
  @possessions << {
    name: name.to_sym,
    serializer: serializer,
    attr: attr
  }
end

.header_titlesObject



105
106
107
# File 'lib/cli/base_serializer.rb', line 105

def self.header_titles
  attributes.map(&:to_s).map(&:upcase)
end

.possessionsObject



89
90
91
# File 'lib/cli/base_serializer.rb', line 89

def self.possessions
  @possessions ||= []
end

.serialize(object, context = {}) ⇒ Object



109
110
111
112
# File 'lib/cli/base_serializer.rb', line 109

def self.serialize(object, context={})
  instance = new(object, context)
  instance.serialize
end

.serialize_many(objects, context = {}) ⇒ Object



114
115
116
117
# File 'lib/cli/base_serializer.rb', line 114

def self.serialize_many(objects, context={})
  out = objects.map {|object| serialize(object, context)}
  { data: out }
end

Instance Method Details

#attribute_value(name) ⇒ Object



57
58
59
60
# File 'lib/cli/base_serializer.rb', line 57

def attribute_value(name)
  receiver = self.respond_to?(name) ? self : object
  receiver.send(name)
end

#keyObject



67
68
69
# File 'lib/cli/base_serializer.rb', line 67

def key
  object.id
end

#limit_has_many(association_name, query_result) ⇒ Object



62
63
64
65
# File 'lib/cli/base_serializer.rb', line 62

def limit_has_many(association_name, query_result)
  limit_quantity = context["#{association_name}_limit".to_sym]
  limit_quantity && query_result.limit(limit_quantity) || query_result
end

#serializeObject



15
16
17
18
19
20
# File 'lib/cli/base_serializer.rb', line 15

def serialize
  flat_attrs = serialize_flat_attrs
  assoc_attrs = serialize_associations
  poss_attrs = serialize_possessions
  flat_attrs.merge(assoc_attrs).merge(poss_attrs)
end

#serialize_associationsObject



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/cli/base_serializer.rb', line 39

def serialize_associations
  Hash[self.class.associations.map do |association|
    attr = association[:attr] || association[:name]
    values = attribute_value(attr)
    values = limit_has_many(attr, values)
    serialized_array = values.map do |value|
      serialized_relation_value(association, value)
    end
    [attr, serialized_array]
  end]
end

#serialize_flat_attrsObject



22
23
24
25
26
# File 'lib/cli/base_serializer.rb', line 22

def serialize_flat_attrs
  Hash[self.class.attributes.map do |attribute_name|
    [attribute_name, attribute_value(attribute_name)]
  end]
end

#serialize_possessionsObject



28
29
30
31
32
33
34
35
36
37
# File 'lib/cli/base_serializer.rb', line 28

def serialize_possessions
  Hash[self.class.possessions.map do |possession|
    attr = possession[:attr] || possession[:name]
    value = attribute_value(attr)
    serialized_value = value && begin
                                  serialized_relation_value(possession, value)
                                end
    [attr, serialized_value]
  end]
end

#serialized_relation_value(relation, value) ⇒ Object



51
52
53
54
55
# File 'lib/cli/base_serializer.rb', line 51

def serialized_relation_value(relation, value)
  serializer = relation[:serializer]
  new_context = context.merge(parent_object: object)
  serializer.serialize(value, new_context)
end