Class: Praxis::View

Inherits:
Object
  • Object
show all
Defined in:
lib/praxis-blueprints/view.rb

Direct Known Subclasses

CollectionView

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, schema, **options, &block) ⇒ View

Returns a new instance of View.



9
10
11
12
13
14
15
16
# File 'lib/praxis-blueprints/view.rb', line 9

def initialize(name, schema, **options, &block)
  @name = name
  @schema = schema
  @contents = ::Hash.new
  @block = block

  @options = options
end

Instance Attribute Details

#contentsObject (readonly)

Returns the value of attribute contents.



5
6
7
# File 'lib/praxis-blueprints/view.rb', line 5

def contents
  @contents
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/praxis-blueprints/view.rb', line 6

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/praxis-blueprints/view.rb', line 7

def options
  @options
end

#schemaObject (readonly)

Returns the value of attribute schema.



4
5
6
# File 'lib/praxis-blueprints/view.rb', line 4

def schema
  @schema
end

Instance Method Details

#attribute(name, **opts, &block) ⇒ Object

Raises:

  • (AttributorException)


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/praxis-blueprints/view.rb', line 38

def attribute(name, **opts, &block)
  raise AttributorException, "Attribute names must be symbols, got: #{name.inspect}" unless name.is_a? ::Symbol

  attribute = schema.attributes.fetch(name) do
    raise "Displaying :#{name} is not allowed in view :#{self.name} of #{schema}. This attribute does not exist in the mediatype"
  end

  if block_given?
    type = attribute.type
    @contents[name] = if type < Attributor::Collection
                        CollectionView.new(name, type.member_attribute.type, &block)
                      else
                        View.new(name, attribute, &block)
                      end
  else
    type = attribute.type
    if type < Attributor::Collection
      is_collection = true
      type = type.member_attribute.type
    end

    if type < Praxis::Blueprint
      view_name = opts[:view] || :default
      view = type.views.fetch(view_name) do
        raise "view with name '#{view_name.inspect}' is not defined in #{type}"
      end
      @contents[name] = if is_collection
                          Praxis::CollectionView.new(view_name, type, view)
                        else
                          view
                        end
    else
      @contents[name] = attribute # , opts]
    end
  end
end

#describeObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/praxis-blueprints/view.rb', line 82

def describe
  # TODO: for now we are just return the first level keys
  view_attributes = {}

  contents.each do |k, dumpable|
    inner_desc = {}
    if dumpable.is_a?(Praxis::View)
      inner_desc[:view] = dumpable.name if dumpable.name
    end
    view_attributes[k] = inner_desc
  end

  { attributes: view_attributes, type: :standard }
end

#example(context = Attributor::DEFAULT_ROOT_CONTEXT) ⇒ Object



75
76
77
78
79
80
# File 'lib/praxis-blueprints/view.rb', line 75

def example(context = Attributor::DEFAULT_ROOT_CONTEXT)
  object = schema.example(context)
  opts = {}
  opts[:context] = context if context
  render(object, **opts)
end

#expanded_fieldsObject



27
28
29
30
31
32
# File 'lib/praxis-blueprints/view.rb', line 27

def expanded_fields
  @expanded_fields ||= begin
    contents # force evaluation of the contents
    FieldExpander.expand(self)
  end
end

#render(object, context: Attributor::DEFAULT_ROOT_CONTEXT, renderer: Renderer.new) ⇒ Object



34
35
36
# File 'lib/praxis-blueprints/view.rb', line 34

def render(object, context: Attributor::DEFAULT_ROOT_CONTEXT, renderer: Renderer.new)
  renderer.render(object, expanded_fields, context: context)
end