Module: HOALife::Arrayable

Included in:
Resource
Defined in:
lib/hoalife/concerns/arrayable.rb

Overview

Pass a string, have it split with the given separator

Constant Summary collapse

BLANK_RE =
/\A[[:space:]]*\z/

Instance Method Summary collapse

Instance Method Details

#as_array(method_name, separator: /,|\||;|\s&\s|\sand\s|\s\s+/, joiner: ",") ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/hoalife/concerns/arrayable.rb', line 5

def as_array(method_name, separator: /,|\||;|\s&\s|\sand\s|\s\s+/, joiner: ",")
  define_method("#{method_name}=") do |value|
    case value
    when String
      super(value.split(separator).collect(&:strip).uniq)
    when Array
      super(
        value.reject do |sub_val|
          sub_val.nil? || sub_val.empty? || sub_val.to_s.match?(BLANK_RE)
        end.collect do |sub_val|
          case sub_val
          when String
            sub_val.split(separator).collect(&:strip)
          else
            sub_val
          end
        end.flatten.uniq
      )
    when nil
      super([])
    else
      super(value)
    end
  end
end