Class: LiquidStream::Stream

Inherits:
Liquid::Drop
  • Object
show all
Defined in:
lib/liquid_stream/stream.rb

Direct Known Subclasses

Streams

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source = nil, stream_context = {}) ⇒ Stream

Returns a new instance of Stream.



6
7
8
9
# File 'lib/liquid_stream/stream.rb', line 6

def initialize(source=nil, stream_context={})
  @source = source
  @stream_context = stream_context
end

Instance Attribute Details

#sourceObject (readonly)

Returns the value of attribute source.



4
5
6
# File 'lib/liquid_stream/stream.rb', line 4

def source
  @source
end

#stream_contextObject (readonly)

Returns the value of attribute stream_context.



4
5
6
# File 'lib/liquid_stream/stream.rb', line 4

def stream_context
  @stream_context
end

Class Method Details

.stream(method_name, options = {}, &block) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
# File 'lib/liquid_stream/stream.rb', line 13

def self.stream(method_name, options={}, &block)
  self.liquid_streams ||= {}
  self.liquid_streams[method_name] = {options: options, block: block}

  # DefinesStreamMethod
  if block_given?
    if options.has_key?(:matching)
      self.define_method_with_block method_name, block
    else
      self.delegate_method_with_block_to_new_stream_instance method_name, block
    end
  else
    self.send(:define_method, method_name) do |*args|
      method_result = source.send(method_name)
      options = self.class.liquid_streams[method_name][:options]

      if method_result.respond_to?(:each)
        # BuildsStreamClassName
        streams_class_name = Utils.
          streams_class_name_from(options[:as] || method_name)

        # FailsIfStreamNotDefined
        unless Object.const_defined?(streams_class_name)
          fail StreamNotDefined, "`#{streams_class_name}` is not defined"
        end

        # BuildsStreamClass
        streams_class = streams_class_name.constantize

        # CreatesStreams
        streams_class.new(method_result, stream_context)
      else
        stream_class_name = Utils.
          stream_class_name_from(options[:as] || method_name)

        if Object.const_defined?(stream_class_name)
          stream_class = stream_class_name.constantize
          stream_class.new(method_result, stream_context)
        else
          method_result
        end
      end
    end
  end
end

Instance Method Details

#before_method(method_name) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/liquid_stream/stream.rb', line 59

def before_method(method_name)
  stream_name = matching_stream_names_for(method_name).first
  if stream_name
    options = self.liquid_streams[stream_name][:options]
    result = send(stream_name, method_name)

    if options[:as]
      if result.respond_to?(:each)
        # TODO: implement this IF we need to
        # streams_class_name = Util.stream_class_name_from(options[:as])
        # streams_class = streams_class_name.constantize
      else
        stream_class_name = Utils.stream_class_name_from(options[:as])
        stream_class = stream_class_name.constantize
        stream_class.new(result, stream_context)
      end
    else
      result
    end
  end
end