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.



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

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
  @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.



5
6
7
# File 'lib/blueprinter/view.rb', line 5

def definition_order
  @definition_order
end

#excluded_field_namesObject (readonly)

Returns the value of attribute excluded_field_names.



5
6
7
# File 'lib/blueprinter/view.rb', line 5

def excluded_field_names
  @excluded_field_names
end

#fieldsObject (readonly)

Returns the value of attribute fields.



5
6
7
# File 'lib/blueprinter/view.rb', line 5

def fields
  @fields
end

#included_view_namesObject (readonly)

Returns the value of attribute included_view_names.



5
6
7
# File 'lib/blueprinter/view.rb', line 5

def included_view_names
  @included_view_names
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/blueprinter/view.rb', line 5

def name
  @name
end

#transformersObject (readonly)

Returns the value of attribute transformers.



5
6
7
# File 'lib/blueprinter/view.rb', line 5

def transformers
  @transformers
end

Instance Method Details

#<<(field) ⇒ Object



67
68
69
70
# File 'lib/blueprinter/view.rb', line 67

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

#add_transformer(custom_transformer) ⇒ Object



63
64
65
# File 'lib/blueprinter/view.rb', line 63

def add_transformer(custom_transformer)
  transformers << custom_transformer
end

#exclude_field(field_name) ⇒ Object



53
54
55
# File 'lib/blueprinter/view.rb', line 53

def exclude_field(field_name)
  excluded_field_names << field_name
end

#exclude_fields(field_names) ⇒ Object



57
58
59
60
61
# File 'lib/blueprinter/view.rb', line 57

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

#include_view(view_name) ⇒ Object



41
42
43
44
# File 'lib/blueprinter/view.rb', line 41

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

#include_views(view_names) ⇒ Object



46
47
48
49
50
51
# File 'lib/blueprinter/view.rb', line 46

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



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

def inherit(view)
  view.fields.values.each 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.transformers.each do |transformer|
    self.add_transformer(transformer)
  end
end

#track_definition_order(method, is_view = true) ⇒ Object



17
18
19
20
21
# File 'lib/blueprinter/view.rb', line 17

def track_definition_order(method, is_view = true)
  if @sort_by_definition
    @definition_order << DefinitionPlaceholder.new(method, is_view)
  end
end