Module: Mongrep::Model

Includes:
VirtusExtensions
Included in:
MongoModel
Defined in:
lib/mongrep/model.rb

Overview

A mixin providing Virtus.model functionality and a recursive to_h method

Defined Under Namespace

Modules: Shell, VirtusExtensions

Class Method Summary collapse

Methods included from VirtusExtensions

#==, #to_h

Class Method Details

.partial(*field_names) ⇒ Model .partial(*field_names, nested_fields) ⇒ Model

Returns a new model that includes just the provided subset of fields from the original model

Examples:

MyModel.partial(:foo, :bar)

With nested fields

MyModel.partial(:foo, :bar, nested: [:foo, { deep: [:bar] }])

Overloads:

  • .partial(*field_names) ⇒ Model

    Parameters:

    • field_names (Array(Symbol))

      a list of fields to include in the resulting model

  • .partial(*field_names, nested_fields) ⇒ Model

    Parameters:

    • field_names (Array(Symbol))

      a list of fields to include in the resulting model

    • nested_fields (Hash(Symbol => Array(Symbol, Hash)))

      nested fields to include in the resulting model

Returns:

  • (Model)

    the generated model class



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/mongrep/model.rb', line 59

def partial(*fields)
  class_name = "#{name}::Partial[#{fields.map(&:inspect).join(', ')}]"
  attributes = partial_attributes(*fields)
  Class.new do
    include Model
    include Shell
    extend Shell::ClassMethods

    define_singleton_method(:name) { class_name }
    attributes.each { |attribute| attribute_set << attribute }
  end
end