class Backframe::Response::Fields
Public Class Methods
new(collection, fields)
click to toggle source
# File lib/backframe/response/fields.rb, line 9 def initialize(collection, fields) @collection = collection @fields = fields end
Public Instance Methods
any?()
click to toggle source
# File lib/backframe/response/fields.rb, line 14 def any? !@fields.nil? end
array()
click to toggle source
# 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
Private Instance Methods
keys(hash, prefix = '')
click to toggle source
# File lib/backframe/response/fields.rb, line 45 def keys(hash, prefix = '') keys = [] hash.each do |key, value| fullkey = (!prefix.empty?) ? "#{prefix}.#{key}" : key if value.is_a?(Hash) keys.concat(keys(value, fullkey)) else keys << fullkey.to_s end end keys end