Module: EPlat::Concerns::OverwriteInstanceMethods

Extended by:
ActiveSupport::Concern
Included in:
Base
Defined in:
lib/e_plat/resource/concerns/overwrite_instance_methods.rb

Instance Method Summary collapse

Instance Method Details

#as_eplat_jsonObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/e_plat/resource/concerns/overwrite_instance_methods.rb', line 63

def as_eplat_json
	schema_keys = self.class.schema.keys
	schema_keys.each_with_object({}) do |key, hash|
		value = send(key)
		
		hash[key] = 
			if value.is_a?(Array)
				value.map { |item| item.respond_to?(:as_eplat_json) ? item.as_eplat_json : item }
			elsif value.respond_to?(:as_eplat_json)
				value.as_eplat_json
			else
				value
			end
	end
end

#as_json(options = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/e_plat/resource/concerns/overwrite_instance_methods.rb', line 14

def as_json(options = {})
	full_json = super(options)
	return {} if read_only?

	if mapping.class.virtual_collections.present?
		virtual_keys = mapping.class.virtual_collections.map{|c| c[:name]}.map(&:to_s)
		
		full_json.reject!{|key, value| virtual_keys.include?(key) } # not needed in JSON, as it's just a proxy for setting the real attributes
	end

	return full_json if new_record? # new records request with all attributes
	
	request_json = 
		if include_root_in_json or options[:root].present?
			{ element_name => select_changed_attributes_and_collection_keys(full_json[element_name]) }
		else
			select_changed_attributes_and_collection_keys(full_json)
		end

	if top_level_resource? # then check that any present collections have all entries represented with atleast an ID, so they're not removed
		hash_without_root(request_json).each do |key, value|
			next unless arrayOfPresentHashes?(value)

			hash_without_root(request_json)[key].filter!(&:present?)
			hash_without_root(request_json)[key] += unrepresented_collection_ids(key, value).map{ |id| { "id" => id } }
		end
	end

		add_root_if_needed(request_json, options[:root]).reject do |k,v| 
		if v.is_a?(Array) 
			v.reject(&:empty?).empty?
		else 
			v.nil? || v.blank?
		end
	end
end

#createObject



90
91
92
93
# File 'lib/e_plat/resource/concerns/overwrite_instance_methods.rb', line 90

def create
	self.attributes = mapping.via_native_attributes_where_possible(attributes)
	super
end

#create_resource_for(resource_name) ⇒ Object

Create and return a class definition for a resource inside the current resource



81
82
83
84
85
86
87
88
# File 'lib/e_plat/resource/concerns/overwrite_instance_methods.rb', line 81

def create_resource_for(resource_name)
	resource = Class.new(EPlat::Base) # <- this line changed
	resource.prefix = self.class.prefix
	resource.site = self.class.site
	self.class.const_set(resource_name, resource)
		
	resource
end

#to_eplat_json(options = {}) ⇒ Object



51
52
53
54
55
56
57
58
59
60
# File 'lib/e_plat/resource/concerns/overwrite_instance_methods.rb', line 51

def to_eplat_json(options = {})
	root_at_top_of_json = self.mapping.include_root_in_request_body?(self)
	options[:root] 		= self.element_name if root_at_top_of_json 

	result = '{'
	result << as_eplat_json.map do |key, value|
		"#{ActiveSupport::JSON.encode(key.to_s)}:#{ActiveSupport::JSON.encode(value, options)}"
	end * ','
	result << '}'
end

#to_json(options = {}) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/e_plat/resource/concerns/overwrite_instance_methods.rb', line 6

def to_json(options = {})
	#add this option here so root is added at the top of the JSON, as_json to every nested resource
	root_at_top_of_json = self.mapping.include_root_in_request_body?(self)
	options[:root] 		= self.element_name if root_at_top_of_json 

	super(options)
end

#updateObject



95
96
97
98
# File 'lib/e_plat/resource/concerns/overwrite_instance_methods.rb', line 95

def update
	self.attributes = mapping.via_native_attributes_where_possible(attributes)
	super
end