Class: GroupDocs::Api::Entity

Inherits:
Object
  • Object
show all
Extended by:
Helpers::Accessor
Defined in:
lib/groupdocs/api/entity.rb

Instance Method Summary collapse

Methods included from Helpers::Accessor

alias_accessor

Constructor Details

#initialize(options = {}) {|self| ... } ⇒ Entity

Implements flexible API object creation.

You can pass hash of options to automatically set attributes.

You can also pass block to set up attributes.

Examples:

Create folder with options hash

GroupDocs::Storage::Folder.new(id: 1, name 'Test', url: 'http://groupdocs.com/folder/test')
#=> <#GroupDocs::Storage::Folder @id=1 @name="Test" @url="http://groupdocs.com/folder/test">

Create folder with block

GroupDocs::Storage::Folder.new do |folder|
  folder.id = 1
  folder.name = 'Test'
  folder.url = 'http://groupdocs.com/folder/test'
end
#=> <#GroupDocs::Storage::Folder @id=1 @name="Test" @url="http://groupdocs.com/folder/test">

Parameters:

  • options (Hash) (defaults to: {})

    Each option is object attribute

Yields:

  • (self)

    Use block to set up attributes



29
30
31
32
33
34
35
36
37
# File 'lib/groupdocs/api/entity.rb', line 29

def initialize(options = {}, &blk)
  if options.empty?
    yield self if block_given?
  else
    options.each do |attr, value|
      send(:"#{attr}=", value) if respond_to?(:"#{attr}=")
    end
  end
end

Instance Method Details

#inspectObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Inspects object using accessors instead of instance variables values.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/groupdocs/api/entity.rb', line 74

def inspect
  not_nil_variables = instance_variables.select do |variable|
    !send(variable.to_s.underscore.delete('@')).nil?
  end

  variables = not_nil_variables.map  do |variable|
    accessor = variable.to_s.underscore.delete('@').to_sym
    value = send(accessor)
    value = case value
            when Symbol then ":#{value}"
            when String then "\"#{value}\""
            else value
            end
    "@#{accessor}=#{value}"
  end

  inspected = self.to_s
  unless variables.empty?
    inspected.gsub!(/>$/, '')
    inspected << " #{variables.join(', ')}"
    inspected << ?>
  end

  inspected
end

#to_hashHash

Recursively converts object and all its attributes to hash.

Examples:

Convert simple object to hash

object = GroupDocs::Storage::File.new(id: 1, name, 'Test.pdf')
object.to_hash
#=> { id: 1, name: 'Test.pdf' }

Returns:

  • (Hash)


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/groupdocs/api/entity.rb', line 49

def to_hash
  hash = {}
  instance_variables.each do |variable|
    key = variable.to_s.delete('@').to_sym
    value = instance_variable_get(variable)

    hash[key] = case value
                when GroupDocs::Api::Entity
                  value.to_hash
                when Array
                  value.map do |i|
                    i.respond_to?(:to_hash) ? i.to_hash : i
                  end
                else
                  value
                end
  end

  hash
end