Class: Array

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

Instance Method Summary collapse

Instance Method Details

#to_hObject



25
26
27
28
# File 'lib/exts/array.rb', line 25

def to_h
  return {} unless self.all? {|a| a.is_a?(Symbol) || a.is_a?(String)}
  self.inject({}) {|h,a| h[a] = a;h }  
end

#to_sorted_nodesObject



30
31
32
# File 'lib/exts/array.rb', line 30

def to_sorted_nodes
  self.group_by(&:parent_id).each {|k,v| v.sort_by! &:order_num}.to_a.flatten.select{|c| !c.is_a?Integer}.compact
end

#to_ztree_node(*args) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/exts/array.rb', line 3

def to_ztree_node(*args)
  opts = args.extract_options!
  checked_ids = opts[:checked_ids] ? opts[:checked_ids].split(',') : []
  opts.delete(:checked_ids)
  self.inject([]) do |nodes, h|
     ha = {
       id: h.id,
       name: h.name,
       pId: h.parent_id.blank? ? 0 : h.parent_id, 
       open: false
     }
     [opts, args].each do |opt|
       opt.to_h.each do |symb, metd|
         val =  h.respond_to?(metd) ? h.send(metd) : metd
         ha = ha.merge(symb => val)
         ha = ha.merge(checked: true) if checked_ids.include?(h.id.to_s)
       end
     end
     nodes << ha
  end
end