Class: RedGrape::Pipe::Base

Inherits:
Object show all
Defined in:
lib/red_grape/pipe/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(prev, *opts, &block) ⇒ Base

Returns a new instance of Base.



28
29
30
31
32
33
# File 'lib/red_grape/pipe/base.rb', line 28

def initialize(prev, *opts, &block)
  @prev = prev
  @next = nil
  @opts = opts
  @block = block
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/red_grape/pipe/base.rb', line 140

def method_missing(name, *args, &block)
  #auto_take = name =~ /(.*)!$/ # TODO
  #name = $1
  class_name = "#{name.to_s.sub(/^./){$&.upcase}.gsub(/_(.)/){$1.upcase}}Pipe"
  args.unshift block if block # 引数の数は変わるので、blockは必ず1番目に追加しておく
  pipe_class =
    if Pipe.const_defined? class_name
      eval "RedGrape::Pipe::#{class_name}"
    else
      args.unshift name
      PropertyPipe
    end
  @next = pipe_class.new self, *args
end

Instance Attribute Details

#itObject

Returns the value of attribute it.



26
27
28
# File 'lib/red_grape/pipe/base.rb', line 26

def it
  @it
end

#nextObject

Returns the value of attribute next.



26
27
28
# File 'lib/red_grape/pipe/base.rb', line 26

def next
  @next
end

#optsObject

Returns the value of attribute opts.



26
27
28
# File 'lib/red_grape/pipe/base.rb', line 26

def opts
  @opts
end

#prevObject

Returns the value of attribute prev.



26
27
28
# File 'lib/red_grape/pipe/base.rb', line 26

def prev
  @prev
end

#valueObject

Returns the value of attribute value.



26
27
28
# File 'lib/red_grape/pipe/base.rb', line 26

def value
  @value
end

Instance Method Details

#copy(depth = nil) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
# File 'lib/red_grape/pipe/base.rb', line 128

def copy(depth=nil)
  obj = self.class.new nil, @opts, &@block
  if depth.nil?
    obj.prev = self.prev.copy
    obj.prev.next = obj
  elsif 0 < depth
    obj.prev = self.prev.copy(depth - 1)
    obj.prev.next = obj
  end
  obj
end

#done?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/red_grape/pipe/base.rb', line 53

def done?
  not @value.nil?
end

#dupObject

note: both prev and next are not copied.



124
125
126
# File 'lib/red_grape/pipe/base.rb', line 124

def dup
  self.class.new nil, @opts, &@block
end

#first?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/red_grape/pipe/base.rb', line 45

def first?
  not @prev.kind_of? RedGrape::Pipe::Base
end

#first_pipeObject



39
40
41
42
43
# File 'lib/red_grape/pipe/base.rb', line 39

def first_pipe
  pipe = self
  pipe = pipe.prev until pipe.first?
  pipe
end

#last?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/red_grape/pipe/base.rb', line 49

def last?
  @next.nil?
end

#pass_next(context, pushed_obj, next_obj = nil, &block) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/red_grape/pipe/base.rb', line 76

def pass_next(context, pushed_obj, next_obj=nil, &block)
  next_obj ||= pushed_obj
  if self.last?
    block.call if block
    next_obj
  elsif pushed_obj.nil?
    block.call if block
    next_obj.pass_through self.next, context
  else
    context.push_history pushed_obj do |ctx|
      block.call if block
      next_obj.pass_through self.next, ctx
    end
  end
end

#pipe_eval(params = {}, &block) ⇒ Object



92
93
94
95
# File 'lib/red_grape/pipe/base.rb', line 92

def pipe_eval(params={}, &block)
  params.each {|k, v| self.send "#{k}=", v}
  instance_eval(&block)
end

#pipe_nameObject



35
36
37
# File 'lib/red_grape/pipe/base.rb', line 35

def pipe_name
  self.class.name.split('::').last.sub(/Pipe$/, '')
end

#sizeObject



113
114
115
116
117
118
119
120
121
# File 'lib/red_grape/pipe/base.rb', line 113

def size
  len = 0
  pipe = self
  while pipe.is_a?(Pipe::Base) and pipe.prev
    len += 1 
    pipe = pipe.prev
  end
  len
end

#take(context = Context.new) ⇒ Object Also known as: []



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/red_grape/pipe/base.rb', line 57

def take(context=Context.new)
  if first?
    val = @prev.pass_through self, context
    # TODO: should be refactored
    if context.aggregating?
      context.resume_from_aggregating
    elsif context.grouping?
      context.resume_from_grouping
    elsif context.accumulating?
      context.resume_from_accumulating
    else
      val
    end
  else
    @prev.take
  end
end

#to_aObject



103
104
105
106
107
108
109
110
111
# File 'lib/red_grape/pipe/base.rb', line 103

def to_a
  pipe = self
  ret = [pipe.pipe_name]
  until pipe.first?
    pipe = pipe.prev
    ret.unshift pipe.pipe_name
  end
  ret
end

#to_aryObject

TODO: i dont know why.



101
# File 'lib/red_grape/pipe/base.rb', line 101

def to_ary; nil end

#to_sObject



97
98
99
# File 'lib/red_grape/pipe/base.rb', line 97

def to_s
  Pipe.auto_take ? take.to_s : super
end