Class: Sequence::OfObjectMethods

Inherits:
OfArray show all
Defined in:
lib/sequence/ofobjectmethods.rb

Constant Summary

Constants inherited from Sequence

SubSequence, VERSION

Instance Attribute Summary

Attributes inherited from Indexed

#data, #pos

Attributes inherited from Sequence

#last_match, #maxmatchlen

Instance Method Summary collapse

Methods inherited from OfArray

#append, #index, #new_data, #rindex

Methods included from ArrayLike

#data_class, #index, #like, #new_data, #push, #rindex, #scan, #scan_until, #scanback, #scanback_until, #unshift

Methods inherited from Indexed

_orig_new, #_pos=, #eof?, #maxmatchlen, #maxmatchlen=, new, #new_data

Methods inherited from UseData

#read, #readahead, #readback, #readbehind, #size

Methods inherited from Sequence

#+, #-, #<<, [], #[], #[]=, #_adjust_pos_on_change, #_default_maxmatchlen, #_delete_position, #_normalize_pos, #_parse_slice_args, #_pos=, #all_data, #append, #begin, #begin!, #check, #check_until, #checkback, #checkback_until, #close, #closed?, #delete, #each, #empty?, #end, #end!, #eof?, #exist?, #existback?, #first, #goto, #holding, #holding!, #holding?, #holding_position, #holding_position!, #holding_position?, #insert, #last, #length, #match?, #matchback?, #more_data?, #move, #move!, #nearbegin, #nearend, #new, #notify_change, #on_change_notify, #original__new, #overwrite, #pop, #pos, #pos=, #pos?, #position, #position=, #position?, #pred, #prepend, #prop, #read, #read!, #read1, #readahead, #readahead1, #readback, #readback1, #readbehind, #readbehind1, #rest_size, #reversed, #shift, #size, #skip, #skip_literal, #skip_until, #skip_until_literal, #skipback, #skipback_until, #slice, #slice!, #slice1, #slice1!, #subseq, #succ, #to_sequence, #was_data?, #write, #writeahead, #writeback, #writebehind

Constructor Details

#initialize(obj, exceptions = [], extras = []) ⇒ OfObjectMethods

,modifiable=false



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/sequence/ofobjectmethods.rb', line 11

def initialize obj, exceptions=[], extras=[] #,modifiable=false
  @obj=obj
  methods=obj.public_methods.-(Functional.nonfunctions_of(obj)).-(exceptions)
  methods.reject!{|name| /[!=]$/==name }
  @data.concat extras.map{|x| Symbol===x ? x.to_s : x }
  @data=methods.inject([]){|l,name| l+[name,nil]}
  #@data.freeze unless modifiable      
  
  @is_exception=0
  @is_reified=0
end

Instance Method Details

#is_exception?(index) ⇒ Boolean

Returns:

  • (Boolean)


23
# File 'lib/sequence/ofobjectmethods.rb', line 23

def is_exception?(index) @is_exception&(1<<index/2) == 0 end

#is_reified?(index) ⇒ Boolean

Returns:

  • (Boolean)


26
# File 'lib/sequence/ofobjectmethods.rb', line 26

def is_reified?(index) @is_reified&(1<<index/2) == 0 end

#modify(*args) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/sequence/ofobjectmethods.rb', line 29

def modify(*args)
  repldata=args.pop
  start,len,only1=_parse_slice_args(*args)
  len==1 or raise ArgumentError,"scalar modifications to objects only!"
  start.%(2).nonzero? or raise ArgumentError, "OfObjectMethods#modify will not change method names!"
  
  if Array===@data[start-1]
    if @data[start-1].first.to_s=='[]'
      @obj.send(:[]=, @data[1..-1],repldata)
    else raise ArgumentError, "trying to call settor with extra args"
    end
  else        
  
    @obj.send(@data[start-1]+"=",repldata.first)
    @data[start]=repldata.first
  end
  repldata
end

#reify(itemref) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/sequence/ofobjectmethods.rb', line 70

def reify(itemref)

  if itemref.is_a? String or itemref.is_a? Symbol
    i=0
    begin 
      i=@data.index(itemref.to_s,i)  
      i or @data.push itemref.to_s, nil
    end while i and i%2!=0
    reify_from_index(i)
  elsif itemref.is_a? Integer and itemref>=0 and itemref%2==0
    reify_from_index(itemref)
  else
    raise ArgumentError
  end
  return nil
end

#reify_from_index(i) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/sequence/ofobjectmethods.rb', line 48

def reify_from_index(i)
    #if the call raises an exception, store the exception instead of the result 
    #and remember (in @is_exception) that this particular result is an exception
    if is_reified? i
      raise @data[i+1] if is_exception? i
      return 
    end
    set_reified! i
      begin 
        @data[i+1]=
          if Array===@data[i]
            @obj.send(*@data[i])
          else
            @obj.send(@data[i]) 
          end
      rescue Exception=>exc
        set_exception!(i)
        @data[i+1]=exc
        raise
      end    
end

#set_exception!(index) ⇒ Object



24
# File 'lib/sequence/ofobjectmethods.rb', line 24

def set_exception!(index) @is_exception|= 1<<index/2 end

#set_reified!(index) ⇒ Object



27
# File 'lib/sequence/ofobjectmethods.rb', line 27

def set_reified!(index) @is_reified|= 1<<index/2 end