Class: Unimatrix::Operation

Inherits:
Object
  • Object
show all
Defined in:
lib/unimatrix/operation.rb

Direct Known Subclasses

Authorization::Operation, BlueprintOperation

Instance Method Summary collapse

Constructor Details

#initialize(path, parameters = {}) ⇒ Operation

Returns a new instance of Operation.



5
6
7
8
9
# File 'lib/unimatrix/operation.rb', line 5

def initialize( path, parameters = {} )
  @path = path
  @parameters = ( parameters || {} ).deep_dup
  @key = nil
end

Instance Method Details

#destroyObject



30
31
32
33
34
35
36
37
38
39
# File 'lib/unimatrix/operation.rb', line 30

def destroy
  result = nil
  Request.new.tap do | request |
    response = request.destroy( @path, @parameters )
    if response.present?
      result = response.resources
    end
  end
  result
end

#include(*arguments) ⇒ Object



49
50
51
# File 'lib/unimatrix/operation.rb', line 49

def include( *arguments )
  self.spawn( :include => self.normalize_include( *arguments ) )
end

#keyObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/unimatrix/operation.rb', line 11

def key
  return @key ||= begin
    result = 0
    query = @parameters.to_param
    if ( @path.present? || @query.present? )
      query = query.split( '&' ).sort.join( '&' )
      addressable = Addressable::URI.new
      addressable.path = @path
      addressable.query = query unless query.blank?
      result = FNV.new.fnv1a_32( addressable.to_s )
    end
    result
  end
end

#limit(_limit) ⇒ Object



41
42
43
# File 'lib/unimatrix/operation.rb', line 41

def limit( _limit )
  self.spawn( count: _limit )
end

#offset(_offset) ⇒ Object



45
46
47
# File 'lib/unimatrix/operation.rb', line 45

def offset( _offset )
  self.spawn( offset: _offset )
end

#read(&block) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/unimatrix/operation.rb', line 53

def read( &block )
  result = nil
  response = nil
  Request.new.tap do | request |
    request.get( @path, @parameters ).tap do | response |
      if response.is_a?( Error )
        result = response
      else
        result = response.resources
        if block_given?
          case block.arity
            when 0; yield
            when 1; yield result
            when 2; yield result, response
          end
        end
      end
    end
  end
  result
end

#read_in_batches(options = {}, &block) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/unimatrix/operation.rb', line 75

def read_in_batches( options = {}, &block )
  
  total_limit = @parameters[ :count ]
  start = @parameters[ :offset ] || 0
  batch_size = options[ :batch_size ] || 100 

  while total_limit.nil? || start < total_limit
    
    result = nil
    response = nil
    operation = self.limit( batch_size ).offset( start )

    operation.read do | _result, _response |
      result = _result
      response = _response
    end

    unlimited_count = response.body[ '$this' ][ 'unlimited_count' ]
    total_limit = unlimited_count if total_limit.nil? && !unlimited_count.nil?
    start += batch_size

    if block_given?
      case block.arity
        when 0; yield
        when 1; yield result
        when 2; yield result, response
      end
    end
    break if result.nil? || result.size < batch_size
  end

end

#where(parameters) ⇒ Object



26
27
28
# File 'lib/unimatrix/operation.rb', line 26

def where( parameters )
  self.spawn( parameters )
end

#write(node, objects, &block) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/unimatrix/operation.rb', line 108

def write( node, objects, &block )
  result = nil
  Request.new.tap do | request |
  
    serializer = Unimatrix::Serializer.new( objects )
    response = request.post( @path, @parameters, serializer.serialize( node ) )
    if response.present?
      result = response.resources
      if block_given?
        case block.arity
          when 0; yield
          when 1; yield result
          when 2; yield result, response
        end
      end
    end
  end
  result
end