Class: Boxxspring::Operation

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

Instance Method Summary collapse

Constructor Details

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



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

def initialize( path, parameters = {}, result = Array )
  @path = path 
  @parameters = ( parameters || {} ).deep_dup
end

Instance Method Details

#include(*arguments) ⇒ Object



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

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

#limit(_limit) ⇒ Object



18
19
20
# File 'lib/boxxspring/operation.rb', line 18

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

#normalize_include(*arguments) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/boxxspring/operation.rb', line 55

def normalize_include( *arguments )

  includes = {};
  arguments.each do | argument |
    case argument
    when Array
      argument.each do | value  |
        includes.deep_merge!( self.normalize_include( value ) )
      end
    when Hash 
      argument.each do | key, value |
        if !includes.include?( key ) || includes[ key ] === true 
          includes[ key ] = self.normalize_include( value )
        else
          includes[ key ].deep_merge!( self.normalize_include( value ) )
        end
      end
    else 
      includes[ argument ] = true
    end
  end
  includes 

end

#offset(_offset) ⇒ Object



22
23
24
# File 'lib/boxxspring/operation.rb', line 22

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

#order(by, direction = 'desc') ⇒ Object



14
15
16
# File 'lib/boxxspring/operation.rb', line 14

def order( by, direction = 'desc' )
  self.spawn( sort_by: by, sort_direction: direction )
end

#queryObject



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

def query
  result = nil
  Boxxspring::Request.new.tap do | request |
    request.get( @path, @parameters ).tap do | response |
      parser = Boxxspring::Parser.new( response.content )
      result = parser.resources
      result = result.first if result.length > 0 && @result == Object 
    end
  end
  result
end

#readObject



42
43
44
45
46
# File 'lib/boxxspring/operation.rb', line 42

def read 
  result = self.query
  result = result.first if result.present? && result.is_a?( Enumerable )
  result
end

#where(parameters) ⇒ Object



10
11
12
# File 'lib/boxxspring/operation.rb', line 10

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