Class: LittleMonster::Core::Job::Data

Inherits:
Object
  • Object
show all
Defined in:
lib/little_monster/core/job_data.rb

Instance Method Summary collapse

Constructor Details

#initialize(job, input = {}) ⇒ Data

Returns a new instance of Data.



3
4
5
6
7
# File 'lib/little_monster/core/job_data.rb', line 3

def initialize(job, input = {})
  @outputs = input.fetch(:outputs, {})
  @key_owners = input.fetch(:owners, {})
  @job = job
end

Instance Method Details

#==(other) ⇒ Object



9
10
11
12
13
# File 'lib/little_monster/core/job_data.rb', line 9

def ==(other)
  return false unless is_valid?(other) && other.length == length
  @outputs.each { |k, v| return false unless other[k.to_sym] == v }
  true
end

#[](output_key) ⇒ Object



15
16
17
# File 'lib/little_monster/core/job_data.rb', line 15

def [](output_key)
  @outputs[output_key.to_sym]
end

#[]=(output_key, value) ⇒ Object

Raises:

  • (KeyError)


19
20
21
22
23
24
25
26
# File 'lib/little_monster/core/job_data.rb', line 19

def []=(output_key, value)
  raise KeyError, "The key #{output_key} already exists" if @outputs.include? output_key.to_sym
  @outputs[output_key.to_sym] = value

  owner = @job.current_action.to_sym
  @key_owners[owner] = [] unless @key_owners[owner].is_a? Array
  @key_owners[owner] << output_key.to_sym
end

#lengthObject



37
38
39
# File 'lib/little_monster/core/job_data.rb', line 37

def length
  @outputs.length
end

#to_hObject



32
33
34
35
# File 'lib/little_monster/core/job_data.rb', line 32

def to_h
  return {} if @outputs.empty?
  { outputs: @outputs, owners: @key_owners }
end

#to_jsonObject



28
29
30
# File 'lib/little_monster/core/job_data.rb', line 28

def to_json
  MultiJson.dump(to_h)
end