Module: Persistence::Object::ParsePersistenceArgs::ObjectInstance

Includes:
Persistence::Object::ParsePersistenceArgs
Included in:
ObjectInstance
Defined in:
lib/persistence/object/parse_persistence_args/object_instance.rb

Overview

Object-specific implementation for parsing persistence args.

Internal helper for parsing args of the format: method, method( global_id ), method( index_name, value ),

method( index_instance, value ), method( index_name => value ), method( index_instance => value ).

Instance Method Summary collapse

Methods included from Persistence::Object::ParsePersistenceArgs

#process_file_key

Instance Method Details

#parse_object_args_for_index_value_no_value(args, require_value = false) ⇒ Array

Parse *args for index, key_value, no_value.

Parameters:

  • args (Array)

    An array of args of the format: method, method( global_id ), method( index_name, value ), method( index_instance, value ), method( index_name => value ), method( index_instance => value ).

  • require_value (true, false) (defaults to: false)

    Whether key value must be provided; will throw exception if true and key value is not provided.

Returns:

  • (Array)

    Array containing index instance, key value, whether key value was provided.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/persistence/object/parse_persistence_args/object_instance.rb', line 29

def parse_object_args_for_index_value_no_value( args, require_value = false )
  
  # * nil
  #   - Cursor to primary bucket
  # * :index
  #   - Cursor to index
  # * :index => persistence_key
  #   - Object(s) for indexed key value
  
  index = nil
  key_value = nil
  no_value = nil
  case args.count

    when 0

      no_value = true
      if require_value
        raise ::Persistence::Exception::KeyValueRequired, 
              'Key value required.'
      end

    when 1

      index_or_id = args[ 0 ]

      case index_or_id

        when ::Symbol, ::String

          index = self.class.index( index_or_id, true )
          no_value = true

        when ::Hash

          key_value = index_or_id.values[ 0 ]
          index_name = index_or_id.keys[ 0 ]

          case index_name

            when ::Symbol, ::String

              index = self.class.index( index_name, true )

          end

          no_value = false
        
        else

          if index_or_id.respond_to?( :index_object )

            index = index_or_id
            no_value = true
          
          else

            # persistence_id - anything other than Symbol, String, Hash
            key_value = args[ 0 ]
            no_value = false

          end
        
      end
      
    when 2

      index_or_name = args[ 0 ]

      
      if index_or_name.respond_to?( :index_object )
        index = index_or_name
      elsif index_or_name
        index = self.class.index( index_or_name, true )
      end
      
      key_value = args[ 1 ]

      no_value = false

    else

      raise 'Unexpected arguments ' << args.inspect + '.'

  end
  
  # if we have a file we need to persist it like we would a sub-object
  if key_value.is_a?( ::File )
    key_value = process_file_key( key_value )
  end
  
  return index, key_value, no_value
  
end