Class: Dam::Stream

Inherits:
Object
  • Object
show all
Defined in:
lib/dam/stream.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, definition, params = {}) ⇒ Stream

Returns a new instance of Stream.



139
140
141
142
143
# File 'lib/dam/stream.rb', line 139

def initialize(name, definition, params = {})
  @name = name
  @definition = definition
  @params = params.delete(:params)
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



137
138
139
# File 'lib/dam/stream.rb', line 137

def name
  @name
end

Class Method Details

.[](name) ⇒ Object



118
119
120
# File 'lib/dam/stream.rb', line 118

def Stream.[](name)
  lookup(name)
end

.allObject



132
133
134
# File 'lib/dam/stream.rb', line 132

def Stream.all
  @streams.values.collect {|stream| stream.respond_to?(:instances) ? stream.instances : stream }.flatten
end

.has_placeholder?(string) ⇒ Boolean

Returns:

  • (Boolean)


128
129
130
# File 'lib/dam/stream.rb', line 128

def Stream.has_placeholder? string
  string =~ PLACEHOLDER_PATTERN
end

.lookup(name) ⇒ Object



108
109
110
111
112
113
114
115
116
# File 'lib/dam/stream.rb', line 108

def Stream.lookup(name)
  @streams ||= {}
  if @streams.has_key? name
    @streams[name]
  else
    template = @streams.values.find {|stream| stream.respond_to?(:instances) && stream.matches?(name)  }
    template.apply(name) if template
  end
end

.register(name, stream) ⇒ Object



122
123
124
125
126
# File 'lib/dam/stream.rb', line 122

def Stream.register(name, stream)
  @streams ||= {}
  @streams[name] = stream
  stream
end

Instance Method Details

#allObject



153
154
155
156
157
# File 'lib/dam/stream.rb', line 153

def all
  Dam::Storage.get(self.name).collect do |json|
    Activity.from_json json
  end
end

#filtersObject



149
150
151
# File 'lib/dam/stream.rb', line 149

def filters
  @definition.filters
end

#firstObject



159
160
161
# File 'lib/dam/stream.rb', line 159

def first
  Activity.from_json(Dam::Storage.head(self.name))
end

#instantiate!Object



173
174
175
176
# File 'lib/dam/stream.rb', line 173

def instantiate!
  ensure_exists!
  self
end

#limitObject



145
146
147
# File 'lib/dam/stream.rb', line 145

def limit
  @definition.limit
end

#matches?(activity) ⇒ Boolean

Returns:

  • (Boolean)


163
164
165
166
167
168
169
170
171
# File 'lib/dam/stream.rb', line 163

def matches? activity
  filters.any? do |filter|
    return true if filter == :all
    
    filter.any? do |key, value|
      attr_match(value, activity.attributes[key.to_s])
    end
  end
end