Class: Blueprinter::View

Inherits:
Object
  • Object
show all
Defined in:
lib/blueprinter/view.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, fields: {}, included_view_names: [], excluded_view_names: [], transformers: []) ⇒ View

Returns a new instance of View.



9
10
11
12
13
14
15
16
17
# File 'lib/blueprinter/view.rb', line 9

def initialize(name, fields: {}, included_view_names: [], excluded_view_names: [], transformers: [])
  @name = name
  @fields = fields
  @included_view_names = included_view_names
  @excluded_field_names = excluded_view_names
  @view_transformers = transformers
  @definition_order = []
  @sort_by_definition = Blueprinter.configuration.sort_fields_by.eql?(:definition)
end

Instance Attribute Details

#definition_orderObject (readonly)

Returns the value of attribute definition_order.



7
8
9
# File 'lib/blueprinter/view.rb', line 7

def definition_order
  @definition_order
end

#excluded_field_namesObject (readonly)

Returns the value of attribute excluded_field_names.



7
8
9
# File 'lib/blueprinter/view.rb', line 7

def excluded_field_names
  @excluded_field_names
end

#fieldsObject (readonly)

Returns the value of attribute fields.



7
8
9
# File 'lib/blueprinter/view.rb', line 7

def fields
  @fields
end

#included_view_namesObject (readonly)

Returns the value of attribute included_view_names.



7
8
9
# File 'lib/blueprinter/view.rb', line 7

def included_view_names
  @included_view_names
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/blueprinter/view.rb', line 7

def name
  @name
end

#view_transformersObject (readonly)

Returns the value of attribute view_transformers.



7
8
9
# File 'lib/blueprinter/view.rb', line 7

def view_transformers
  @view_transformers
end

Instance Method Details

#<<(field) ⇒ Object



69
70
71
72
# File 'lib/blueprinter/view.rb', line 69

def <<(field)
  track_definition_order(field.name, viewable: false)
  fields[field.name] = field
end

#add_transformer(custom_transformer) ⇒ Object



65
66
67
# File 'lib/blueprinter/view.rb', line 65

def add_transformer(custom_transformer)
  view_transformers << custom_transformer
end

#exclude_field(field_name) ⇒ Object



55
56
57
# File 'lib/blueprinter/view.rb', line 55

def exclude_field(field_name)
  excluded_field_names << field_name
end

#exclude_fields(field_names) ⇒ Object



59
60
61
62
63
# File 'lib/blueprinter/view.rb', line 59

def exclude_fields(field_names)
  field_names.each do |field_name|
    excluded_field_names << field_name
  end
end

#include_view(view_name) ⇒ Object



43
44
45
46
# File 'lib/blueprinter/view.rb', line 43

def include_view(view_name)
  track_definition_order(view_name)
  included_view_names << view_name
end

#include_views(view_names) ⇒ Object



48
49
50
51
52
53
# File 'lib/blueprinter/view.rb', line 48

def include_views(view_names)
  view_names.each do |view_name|
    track_definition_order(view_name)
    included_view_names << view_name
  end
end

#inherit(view) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/blueprinter/view.rb', line 25

def inherit(view)
  view.fields.each_value do |field|
    self << field
  end

  view.included_view_names.each do |view_name|
    include_view(view_name)
  end

  view.excluded_field_names.each do |field_name|
    exclude_field(field_name)
  end

  view.view_transformers.each do |transformer|
    add_transformer(transformer)
  end
end

#track_definition_order(method, viewable: true) ⇒ Object



19
20
21
22
23
# File 'lib/blueprinter/view.rb', line 19

def track_definition_order(method, viewable: true)
  return unless @sort_by_definition

  @definition_order << DefinitionPlaceholder.new(method, viewable)
end