Module: CollectionJson::Serializer::Support

Included in:
Builder, Objects::Item, Validator, Validator::ItemsValidator
Defined in:
lib/collection_json_serializer/support.rb

Instance Method Summary collapse

Instance Method Details

#extract_value_from(resource, method) ⇒ Object



4
5
6
7
8
9
10
11
12
# File 'lib/collection_json_serializer/support.rb', line 4

def extract_value_from(resource, method)
  begin
    value = resource.send(method)
  rescue NoMethodError
    # ignore unknown attributes
  end

  value if value
end

#has_placeholder?(string) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/collection_json_serializer/support.rb', line 31

def has_placeholder?(string)
  string.chars.first.eql?("{") && string.chars.last.eql?("}")
end

#parse_url(url, object) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/collection_json_serializer/support.rb', line 14

def parse_url(url, object)
  segments = url.split("/")

  segments.each_with_index do |segment, index|
    if has_placeholder?(segment)
      action = segment.gsub(/[{}]/, "")
      segments[index] = object.send(action)
    end
  end if segments_with_placeholder?(segments)

  segments.join("/")
end

#segments_with_placeholder?(segments) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/collection_json_serializer/support.rb', line 27

def segments_with_placeholder?(segments)
  segments.any? { |s| has_placeholder?(s) }
end

#set_rel(params) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/collection_json_serializer/support.rb', line 35

def set_rel(params)
  if params[:properties].key?(:rel)
    params[:properties][:rel].to_s
  else
    params[:name].to_s
  end
end