Module: EPlat::Concerns::Aliases

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#type_coercerObject

Returns the value of attribute type_coercer.



7
8
9
# File 'lib/e_plat/resource/concerns/aliases.rb', line 7

def type_coercer
  @type_coercer
end

Instance Method Details

#add_aliases!(aliases, type_schema) ⇒ Object



9
10
11
12
13
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
# File 'lib/e_plat/resource/concerns/aliases.rb', line 9

def add_aliases!(aliases, type_schema)
	@type_coercer = EPlat::TypeCoercer.new(type_schema)
	processed = { getter: [], setter: [] }

	aliases.each do |action|
		action_name, args 	   = action.keys.first, action.values.first
		e_plat_key, native_key = args[:e_plat_key], args[:native_key]
		@is_virtual = !!args[:virtual_collection]

		case action_name
		when :alias_attribute
			add_to_instance! alias_mapped_getter(e_plat_key, native_key, proc: args[:custom_e_plat_getter])
			add_to_instance! alias_mapped_setter(e_plat_key, native_key, proc: args[:custom_native_setter])
			processed[:getter] << e_plat_key
			processed[:setter] << e_plat_key
		when :existing_entry
			add_to_instance! mapped_getter(e_plat_key, proc: args[:custom_e_plat_getter])
			add_to_instance! native_setter(native_key, proc: args[:custom_native_setter])
			processed[:getter] << e_plat_key
			processed[:setter] << native_key
		end
	end
	
	type_schema.each do |e_plat_key, type|
		next if processed[:getter].include?(e_plat_key)
		add_to_instance! mapped_getter(e_plat_key), @is_virtual
	end

	native_keys.each do |native_key|
		add_to_instance!(mapped_getter(native_key), @is_virtual) unless processed[:getter].include?(native_key)
		add_to_instance!(native_setter(native_key), @is_virtual) unless processed[:setter].include?(native_key)
	end
end