Class: Backframe::Response::Fields

Inherits:
Object
  • Object
show all
Defined in:
lib/backframe/response/fields.rb

Instance Method Summary collapse

Constructor Details

#initialize(collection, fields) ⇒ Fields

Returns a new instance of Fields.



9
10
11
12
# File 'lib/backframe/response/fields.rb', line 9

def initialize(collection, fields)
  @collection = collection
  @fields = fields
end

Instance Method Details

#any?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/backframe/response/fields.rb', line 14

def any?
  !@fields.nil?
end

#arrayObject



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/backframe/response/fields.rb', line 18

def array
  return @array if defined?(@array)
  hash = ActiveModelSerializers::SerializableResource.new(@collection.first).serializable_hash
  keys = keys(hash)
  @array = []
  if @fields.nil?
    keys.each do |key|
      @array << { label: key, key: key }
    end
  else
    @fields.split(",").each do |token|
      field = nil
      if token =~ /([\w\s]*):([\w\s\.]*)/
        field = { label: $1.strip, key: $2.strip }
      elsif token =~ /([\w\s\.]*)/
        field = { label: $1.strip, key: $1.strip }
      end
      if keys.include?(field[:key])
        @array << field
      end
    end
  end
  @array
end