class Boxxspring::Operation

Public Class Methods

new( path, parameters = {} ) click to toggle source
# File lib/boxxspring/operation.rb, line 5
def initialize( path, parameters = {} )
  @path = path 
  @parameters = ( parameters || {} ).deep_dup
  @key = nil
end

Public Instance Methods

destroy() click to toggle source
# File lib/boxxspring/operation.rb, line 30
def destroy
  result = nil
  Boxxspring::Request.new.tap do | request |
    response = request.destroy( @path, @parameters )
    if response.present?
      result = response.resources
    end
  end
  result
end
include( *arguments ) click to toggle source
# File lib/boxxspring/operation.rb, line 53
def include( *arguments )
  self.spawn( :include => self.normalize_include( *arguments ) )
end
key() click to toggle source
# File lib/boxxspring/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 ) click to toggle source
# File lib/boxxspring/operation.rb, line 45
def limit( _limit )
  self.spawn( count: _limit )
end
offset( _offset ) click to toggle source
# File lib/boxxspring/operation.rb, line 49
def offset( _offset )
  self.spawn( offset: _offset )
end
order( by, direction = 'desc' ) click to toggle source
# File lib/boxxspring/operation.rb, line 41
def order( by, direction = 'desc' )
  self.spawn( sort_by: by, sort_direction: direction )
end
query( ) { || ... } click to toggle source
# File lib/boxxspring/operation.rb, line 57
def query( &block )
  result = nil
  Boxxspring::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( ) { || ... } click to toggle source
# File lib/boxxspring/operation.rb, line 74
def read( &block )
  response = nil
  result = nil
  self.query do | _result, _response | 
    result = _result
    response = _response 
  end
  if response.success? 
    result = result.first if result.present? && result.is_a?( Enumerable )
    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 ) click to toggle source
# File lib/boxxspring/operation.rb, line 26
def where( parameters )
  self.spawn( parameters )
end
write( node, objects ) { || ... } click to toggle source
# File lib/boxxspring/operation.rb, line 94
def write( node, objects, &block )
  result = nil
  Boxxspring::Request.new.tap do | request |
    serializer = Boxxspring::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

Protected Instance Methods

normalize_include( *arguments ) click to toggle source
# File lib/boxxspring/operation.rb, line 120
           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
spawn( parameters ) click to toggle source
# File lib/boxxspring/operation.rb, line 113
           def spawn( parameters  )
  Boxxspring::Operation.new( 
    @path,
    @parameters.deep_merge( parameters || {} )
  )
end