Class: ActiveModel::Serializer::IncludeTree

Inherits:
Object
  • Object
show all
Defined in:
lib/active_model/serializer/include_tree.rb

Overview

TODO: description of this class, and overview of how it's used

Defined Under Namespace

Modules: Parsing

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (IncludeTree) initialize(hash = {})

Returns a new instance of IncludeTree

Parameters:

  • hash (Hash) (defaults to: {})


88
89
90
# File 'lib/active_model/serializer/include_tree.rb', line 88

def initialize(hash = {})
  @hash = hash
end

Class Method Details

+ (IncludeTree) from_include_args(included)

Translates the arguments passed to the include option into an IncludeTree. The format can be either a String (see #from_string), an Array of Symbols and Hashes, or a mix of both.

Examples:

`posts: [:author, comments: [:author, :upvotes]]`

Parameters:

  • included (Symbol, Hash, Array, String)

Returns:



81
82
83
84
85
# File 'lib/active_model/serializer/include_tree.rb', line 81

def self.from_include_args(included)
  return included if included.is_a?(IncludeTree)

  new(Parsing.include_args_to_hash(included))
end

+ (IncludeTree) from_string(included)

Builds an IncludeTree from a comma separated list of dot separated paths (JSON API format).

Examples:

`'posts.author, posts.comments.upvotes, posts.comments.author'`

Parameters:

  • included (String)

Returns:



70
71
72
# File 'lib/active_model/serializer/include_tree.rb', line 70

def self.from_string(included)
  new(Parsing.include_string_to_hash(included))
end

Instance Method Details

- (Object) [](key)



96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/active_model/serializer/include_tree.rb', line 96

def [](key)
  # TODO(beauby): Adopt a lazy caching strategy for generating subtrees.
  case
  when @hash.key?(key)
    self.class.new(@hash[key])
  when @hash.key?(:*)
    self.class.new(@hash[:*])
  when @hash.key?(:**)
    self.class.new(:** => {})
  else
    nil
  end
end

- (Boolean) key?(key)

Returns:

  • (Boolean)


92
93
94
# File 'lib/active_model/serializer/include_tree.rb', line 92

def key?(key)
  @hash.key?(key) || @hash.key?(:*) || @hash.key?(:**)
end