Class: RailsConnector::AbstractObj

Inherits:
Object
  • Object
show all
Defined in:
lib/reactor/tools/sower.rb,
app/models/rails_connector/abstract_obj.rb

Direct Known Subclasses

SeedObject

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#keep_editedObject

Returns the value of attribute keep_edited.



18
19
20
# File 'lib/reactor/tools/sower.rb', line 18

def keep_edited
  @keep_edited
end

Class Method Details

.compute_type(type_name) ⇒ Object



28
29
30
# File 'app/models/rails_connector/abstract_obj.rb', line 28

def self.compute_type(type_name)
  try_type { type_name.constantize } || self
end

.plant(path, &block) ⇒ Object

Raises:

  • (ActiveRecord::RecordNotFound)


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/reactor/tools/sower.rb', line 20

def self.plant(path, &block)
  obj = Obj.find_by_path(path)
  raise ActiveRecord::RecordNotFound, "plant: Ground not found:" + path if obj.nil?

  # obj.objClass = 'Container' # TODO: enable it!
  # obj.save!
  # obj.release!
  obj.send(:reload_attributes)
  obj.instance_eval(&block) if block_given?
  # ActiveRecord is incompatible with changing the obj class, therefore you get RecordNotFound
  begin
    obj.save!
  rescue ActiveRecord::RecordNotFound
  end
  obj.release unless obj.keep_edited
  obj
end

.with(path, objClass = "Container", &block) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/reactor/tools/sower.rb', line 64

def self.with(path, objClass = "Container", &block)
  splitted_path = path.split("/")
  name = splitted_path.pop
  # ensure path exists
  splitted_path.length.times do |i|
    subpath = splitted_path[0, (i + 1)].join("/").presence || "/"
    subpath_parent = splitted_path[0, i].join("/").presence || "/"
    subpath_name = splitted_path[i]
    unless subpath_name.blank? || Obj.find_by_path(subpath)
      create(name: subpath_name, parent: subpath_parent, obj_class: "Container")
    end
  end
  parent_path = splitted_path.join("/").presence || "/"
  parent = Obj.find_by_path(parent_path)
  parent.obj(name, objClass, &block)
end

Instance Method Details

#do_not_release!Object



81
82
83
# File 'lib/reactor/tools/sower.rb', line 81

def do_not_release!
  @keep_edited = true
end

#obj(name, objClass = "Container", &block) ⇒ Object

creates of fetches an obj with given name (within context), executes a block on it (instance_eval) saves and releases (unless keep_edited = true was called) the object afterwards



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/reactor/tools/sower.rb', line 42

def obj(name, objClass = "Container", &block)
  obj = Obj.find_by_path(File.join(path.to_s, name.to_s))
  if obj.nil?
    obj = Obj.create(name: name, parent: path, obj_class: objClass)
  else
    obj = Obj.find_by_path(File.join(path.to_s, name.to_s))
    if obj.obj_class != objClass
      obj.obj_class = objClass
      begin
        obj.save!
      rescue ActiveRecord::RecordNotFound
      end
      obj = Obj.find_by_path(File.join(path.to_s, name.to_s))
    end
  end
  obj.send(:reload_attributes, objClass)
  obj.instance_eval(&block) if block_given?
  obj.save!
  obj.release unless obj.keep_edited || !obj.reload.edited?
  obj
end

#t(key, **opts) ⇒ Object



85
86
87
# File 'lib/reactor/tools/sower.rb', line 85

def t(key, **opts)
  I18n.t(key, **opts)
end