Class: Decidim::Voca::Rpc::EnumCaster

Inherits:
Object
  • Object
show all
Defined in:
app/rpc/decidim/voca/rpc/enum_casting.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(enum_module, decidim_rpc_mapping) ⇒ EnumCaster

Returns a new instance of EnumCaster.



6
7
8
9
# File 'app/rpc/decidim/voca/rpc/enum_casting.rb', line 6

def initialize(enum_module, decidim_rpc_mapping)
  @decidim_rpc_mapping = decidim_rpc_mapping.deep_symbolize_keys
  @symbolic_mapping =  Hash[enum_module.descriptor.collect { |i| [i, enum_module.resolve(i)] }]
end

Instance Attribute Details

#decidim_rpc_mappingObject (readonly)

Returns the value of attribute decidim_rpc_mapping.



5
6
7
# File 'app/rpc/decidim/voca/rpc/enum_casting.rb', line 5

def decidim_rpc_mapping
  @decidim_rpc_mapping
end

#symbolic_mappingObject (readonly)

Returns the value of attribute symbolic_mapping.



5
6
7
# File 'app/rpc/decidim/voca/rpc/enum_casting.rb', line 5

def symbolic_mapping
  @symbolic_mapping
end

Instance Method Details

#decidim_to_rpc(decidim_key) ⇒ Object



11
12
13
14
# File 'app/rpc/decidim/voca/rpc/enum_casting.rb', line 11

def decidim_to_rpc(decidim_key)
  matches = decidim_rpc_mapping.find { |k, _v| k === decidim_key.to_sym }
  (matches || []).last
end

#rpc_to_decidim(rpc_symbol) ⇒ Object



16
17
18
19
20
# File 'app/rpc/decidim/voca/rpc/enum_casting.rb', line 16

def rpc_to_decidim(rpc_symbol)
  rpc_value = symbolic_mapping[rpc_symbol]
  matches = decidim_rpc_mapping.find { |_k, v| v === rpc_value }
  (matches || []).first
end