Class: Waterworks::PipelineObject

Inherits:
Object
  • Object
show all
Defined in:
lib/waterworks/pipeline_object.rb

Defined Under Namespace

Classes: Field

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id = nil, name = nil) ⇒ PipelineObject

Returns a new instance of PipelineObject.



15
16
17
18
19
20
# File 'lib/waterworks/pipeline_object.rb', line 15

def initialize(id = nil, name = nil)
  @id = id
  @name = name
  @fields = []
  self
end

Class Method Details

.base_attrsObject



6
7
8
# File 'lib/waterworks/pipeline_object.rb', line 6

def self.base_attrs
  [:id, :name]
end

.from_hash(hash, pipe_object = nil) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/waterworks/pipeline_object.rb', line 80

def self.from_hash(hash, pipe_object = nil)
  hash = Util.indifferentify(hash)
  pipe_object = PipelineObject.new if pipe_object.nil?

  hash.each do |key, value|
    if key == 'id'
      pipe_object.id = value
    elsif key == 'name'
      pipe_object.name = value
    else
      key = Util.deasterisk(key)
      if value.is_a? Hash
        Util.indifferentify(value)
        pipe_object.set_attrs(key.to_sym => value[:ref])
      else
        pipe_object.set_attrs(key.to_sym => value)
      end
    end
  end
  pipe_object
end

.from_json(blob) ⇒ Object



102
103
104
# File 'lib/waterworks/pipeline_object.rb', line 102

def self.from_json(blob)
  from_hash(JSON.parse(blob))
end

.safe_fieldsObject



10
11
12
# File 'lib/waterworks/pipeline_object.rb', line 10

def self.safe_fields
  {} # used by subclass
end

Instance Method Details

#<=>(obj) ⇒ Object



106
107
108
# File 'lib/waterworks/pipeline_object.rb', line 106

def <=>(obj)
  id <=> obj.id
end

#collect_attrs!Object



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/waterworks/pipeline_object.rb', line 49

def collect_attrs!
  @fields.clear
  self.class.safe_fields.map do |field, type|
    next unless instance_variable_defined? "@#{field}"
    val = instance_variable_get "@#{field}"
    if type == :string
      add_string_field(field, val)
    elsif type == :ref
      add_ref_field(field, val)
    end
  end
end

#set_attrs(attrs) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/waterworks/pipeline_object.rb', line 22

def set_attrs(attrs)
  raise 'InvalidAttributeHash: set_attr requires a Hash' unless attrs.is_a? Hash
  modifiable = self.class.safe_fields.keys.concat self.class.base_attrs
  attrs.each do |attr, value|
    if modifiable.include? attr
      instance_variable_set("@#{attr}", value)
    else
      raise "Attempted to set invalid attribute #{attr} on #{self.class}"
    end
  end
  self
end

#sourceifyObject



71
72
73
74
75
76
77
78
# File 'lib/waterworks/pipeline_object.rb', line 71

def sourceify
  collect_attrs!
  base = "#{self.class}.new('#{@id}', '#{@name}').set_attrs({\n"
  @fields.each do |field|
    base << "  #{field.sourceify_as_attr}, \n" unless field.key == :type
  end
  base + '})'
end

#to_fhashObject



62
63
64
65
66
67
68
69
# File 'lib/waterworks/pipeline_object.rb', line 62

def to_fhash
  collect_attrs!
  {
    id: @id,
    name: @name,
    fields: @fields.map(&:to_fhash)
  }
end

#to_hashObject



39
40
41
42
43
44
45
46
47
# File 'lib/waterworks/pipeline_object.rb', line 39

def to_hash
  collect_attrs!
  output = {
    id: @id,
    name: @name,
  }
  @fields.each { |f| output.merge!(f.to_hash) }
  output
end

#to_jsonObject



35
36
37
# File 'lib/waterworks/pipeline_object.rb', line 35

def to_json
  JSON.generate(to_hash)
end