Sha256: e3117f4c25ba69d1d54b02eb78d604541d84540a047919df99868304f31e314d
Contents?: true
Size: 1.61 KB
Versions: 3
Compression:
Stored size: 1.61 KB
Contents
module JSONAPI class IncludeDirectives # Construct an IncludeDirectives Hash from an array of dot separated include strings. # For example ['posts.comments.tags'] # will transform into => # { # :posts=>{ # :include=>true, # :include_related=>{ # :comments=>{ # :include=>true, # :include_related=>{ # :tags=>{ # :include=>true # } # } # } # } # } # } def initialize(includes_array) @include_directives_hash = {include_related: {}} includes_array.each do |include| parse_include(include) end end def include_directives @include_directives_hash end def model_includes get_includes(@include_directives_hash) end private def get_related(current_path) current = @include_directives_hash current_path.split('.').each do |fragment| fragment = fragment.to_sym current[:include_related][fragment] ||= {include: false, include_related: {}} current = current[:include_related][fragment] end current end def get_includes(directive) directive[:include_related].map do |name, directive| sub = get_includes(directive) sub.any? ? { name => sub } : name end end def parse_include(include) parts = include.split('.') local_path = '' parts.each do |name| local_path += local_path.length > 0 ? ".#{name}" : name related = get_related(local_path) related[:include] = true end end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
jsonapi-resources-0.4.2 | lib/jsonapi/include_directives.rb |
jsonapi-resources-0.4.1 | lib/jsonapi/include_directives.rb |
jsonapi-resources-0.4.0 | lib/jsonapi/include_directives.rb |