Class: Neewom::Collection

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

Constant Summary collapse

SEQ_KEY =
"neewom|value|"
MOD_KEY =
"|mods:"

Class Method Summary collapse

Class Method Details

.build_for_field(field, bind) ⇒ Object



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

def self.build_for_field(field, bind)
  if field.collection_params.present?
    method_params = field.collection_params.map do |param|
      if param.start_with?(SEQ_KEY)
        deserialize(param)
      else
        eval param.to_s, bind
      end
    end

    field.collection_klass.constantize.public_send(field.collection_method, *method_params)
  else
    field.collection_klass.constantize.public_send(field.collection_method)
  end
end

.deserialize(param) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/neewom/collection.rb', line 22

def self.deserialize(param)
  token = param.gsub(SEQ_KEY, "")

  value, mods = token.split(MOD_KEY)
  value = JSON.parse(value)

  value = mods.split('.').reduce(value) { |acc, mod| acc.public_send(mod) } if mods.present?

  value
end

.serialize(value, mods = "") ⇒ Object



33
34
35
36
37
38
# File 'lib/neewom/collection.rb', line 33

def self.serialize(value, mods="")
  token = "#{SEQ_KEY}#{value.to_json}"
  token += "#{MOD_KEY}#{mods}" unless mods.blank?

  token
end