Class: Unimatrix::Activist::Operation

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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Operation.



5
6
7
8
9
# File 'lib/unimatrix/activist/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/activist/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/activist/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/activist/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/activist/operation.rb', line 41

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

#offset(_offset) ⇒ Object



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

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

#query(&block) ⇒ Object



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

def query( &block )
  result = nil
  Request.new.tap do | request |
    request.get( @path, @parameters ).tap do | response |
      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

#read(&block) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/unimatrix/activist/operation.rb', line 70

def read( &block )
  response = nil
  result = nil
  self.query do | _result, _response |
    result = _result
    response = _response
  end
  if response.success?
    result = result if result.present? && result.is_a?( Array )
    if block_given?
      case block.arity
        when 0; yield
        when 1; yield result
        when 2; yield result, response
      end
    end
  end
  result
end

#where(parameters) ⇒ Object



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

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

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



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/unimatrix/activist/operation.rb', line 90

def write( node, objects, &block )
  result = nil
  Request.new.tap do | request |
    serializer = Unimatrix::Activist::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