Class: Praxis::View
- Inherits:
-
Object
- Object
- Praxis::View
- Defined in:
- lib/praxis-blueprints/view.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#contents ⇒ Object
readonly
Returns the value of attribute contents.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#schema ⇒ Object
readonly
Returns the value of attribute schema.
Instance Method Summary collapse
- #attribute(name, **opts, &block) ⇒ Object
- #describe ⇒ Object
- #example(context = Attributor::DEFAULT_ROOT_CONTEXT) ⇒ Object
- #expanded_fields ⇒ Object
-
#initialize(name, schema, **options, &block) ⇒ View
constructor
A new instance of View.
- #render(object, context: Attributor::DEFAULT_ROOT_CONTEXT, renderer: Renderer.new) ⇒ Object
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, **, &block) @name = name @schema = schema @contents = ::Hash.new @block = block @options = end |
Instance Attribute Details
#contents ⇒ Object (readonly)
Returns the value of attribute contents.
5 6 7 |
# File 'lib/praxis-blueprints/view.rb', line 5 def contents @contents end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/praxis-blueprints/view.rb', line 6 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
7 8 9 |
# File 'lib/praxis-blueprints/view.rb', line 7 def @options end |
#schema ⇒ Object (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
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 |
#describe ⇒ Object
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_fields ⇒ Object
27 28 29 30 31 32 |
# File 'lib/praxis-blueprints/view.rb', line 27 def @expanded_fields ||= begin contents # force evaluation of the contents FieldExpander.(self) end end |