Class: MiniSerializer::Serializer

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

Instance Method Summary collapse

Constructor Details

#initialize(object_main, except_params = {}) ⇒ Serializer



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/mini_serializer.rb', line 6

def initialize(object_main,except_params={})
  @wams_has_one=[]
  @wams_has_many=[]
  @wams_association_object=[]
  @wams_association_for_to_json={}
  @wams_main_object=object_main
  @wams_foreign_object={}
  if !except_params.empty?
    @wams_except_params_main_object=except_params[:except]
  else
    @wams_except_params_main_object={}
  end

end

Instance Method Details

#add_foreign_object(objects, custom_name, include_params, except_params = []) ⇒ Object

ezafe kardane object khareji be object asli



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/mini_serializer.rb', line 38

def add_foreign_object(objects,custom_name,include_params,except_params=[]) # ezafe kardane object khareji be object asli

  if include_params.any?
    if include_params.kind_of?(Hash)
      param_for_include=include_params.keys
    elsif (include_params.kind_of?(Array))

      param_for_include=include_params
    end
    objects=objects.includes(param_for_include)
  end

  if except_params.empty?
    wams_foreign_object[custom_name]=objects
  else
    wams_foreign_object[custom_name]=objects.as_json(except:except_params,include:include_params)
  end

end

#add_has_many(object, except_methods = []) ⇒ Object



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

def add_has_many(object,except_methods=[])
  wams_has_many<< [object,except_methods]
end

#add_has_one(object, except_methods = []) ⇒ Object



25
26
27
# File 'lib/mini_serializer.rb', line 25

def add_has_one(object,except_methods=[])
  wams_has_one<<[object,except_methods]
end

#get_object_includedObject



94
95
96
# File 'lib/mini_serializer.rb', line 94

def get_object_included
  return includes_to_serializer
end

#get_params_included_to_json(with_except: false) ⇒ Object



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

def get_params_included_to_json(with_except:false)
  fetch_association_objects
  if with_except
    {include:wams_association_for_to_json,except:wams_except_params_main_object}
  else
    {include:wams_association_for_to_json}
  end
end

#json_serializer(object = nil) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/mini_serializer.rb', line 58

def json_serializer(object=nil)
  if object.nil?
    included_data=includes_to_serializer
  end

  if !wams_except_params_main_object.empty? # have except params for main object

    if !object.nil? # have object for proccess
      response_data= object.as_json({include:wams_association_for_to_json,except:wams_except_params_main_object})
    else # haven't object for proccess
      response_data= included_data.as_json({include:wams_association_for_to_json,except:wams_except_params_main_object})
    end

  else

    if !object.nil?
      response_data= object.as_json({include:wams_association_for_to_json})
    else
      response_data= included_data.as_json({include:wams_association_for_to_json})
    end
  end
  if wams_foreign_object.any? # when use method foreign_object
    if wams_main_object.kind_of?(Class)
      name_main_object=wams_main_object.to_s.downcase.pluralize
    else
      name_main_object=wams_main_object.klass.name.downcase.pluralize
    end
    return {"#{name_main_object}": response_data,included:wams_foreign_object}

  else
    response_data
  end

  # example output : wams_association_for_to_json is {'house_images':{except: [:id,:house_id]} }
end