Class: Praxis::View

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of View.



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

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

Instance Attribute Details

#contentsObject (readonly)

Returns the value of attribute contents.



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

def contents
  @contents
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
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)


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/praxis-blueprints/view.rb', line 54

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

  attribute = self.schema.attributes.fetch(name) do
    raise "Attribute '#{name}' does not exist in #{self.schema}"
  end

  if block_given?
    view = View.new(name, attribute, &block)
    @contents[name] = view
  else
    raise "Invalid options (#{opts.inspect}) for #{name} while defining view #{@name}" unless opts.is_a?(Hash)
    @contents[name] = [attribute, opts]
  end

end

#describeObject



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/praxis-blueprints/view.rb', line 79

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

  self.contents.each do |k,(dumpable,dumpable_opts)|
    inner_desc = {}
    inner_desc[:view] = dumpable_opts[:view] if dumpable_opts && dumpable_opts[:view]
    view_attributes[k] = inner_desc
  end

  { attributes: view_attributes }
end

#dump(object, context: Attributor::DEFAULT_ROOT_CONTEXT, **opts) ⇒ Object Also known as: to_hash



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/praxis-blueprints/view.rb', line 25

def dump(object, context: Attributor::DEFAULT_ROOT_CONTEXT,**opts)
  self.contents.each_with_object({}) do |(name, (dumpable, dumpable_opts)), hash|
    next unless object.respond_to?(name)

    begin
      value = object.send(name)
    rescue => e
      raise Attributor::DumpError, context: context, name: name, type: object.class, original_exception: e
    end
    next if value.nil?
    
    # FIXME: this is such an ugly way to do this. Need attributor#67.
    if dumpable.kind_of?(View)
      new_context = context + [name]
      hash[name] = dumpable.dump(value, context: new_context ,**(dumpable_opts||{}))
    else
      type = dumpable.type
      if type.respond_to?(:attributes) || type.respond_to?(:member_attribute)
        new_context = context + [name]
        hash[name] = dumpable.dump(value, context: new_context ,**(dumpable_opts||{}))
      else
        hash[name] = value
      end
    end
  end
end

#example(context = nil) ⇒ Object



72
73
74
75
76
77
# File 'lib/praxis-blueprints/view.rb', line 72

def example(context=nil)
  object = self.schema.example(context)
  opts = {}
  opts[:context] = context if context
  self.dump(object, opts)
end