Class: RailsConnector::AbstractObj

Inherits:
Object
  • Object
show all
Defined in:
lib/reactor/tools/sower.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.



21
22
23
# File 'lib/reactor/tools/sower.rb', line 21

def keep_edited
  @keep_edited
end

Class Method Details

.plant(path, &block) ⇒ Object

Raises:

  • (ActiveRecord::RecordNotFound)


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

def self.plant(path, &block)
  obj = Obj.find_by_path(path)
  raise ActiveRecord::RecordNotFound.new('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.last.edited?
  obj
end

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



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

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]
    create(:name => subpath_name, :parent => subpath_parent, :obj_class => 'Container') unless Obj.find_by_path(subpath) unless subpath_name.blank?
  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



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

def obj(name, objClass = 'Container', &block)
  obj = Obj.find_by_path(File.join(self.path.to_s, name.to_s))
  if obj.nil?
    obj = Obj.create(:name => name, :parent => self.path, :obj_class => objClass)
  else
    obj = Obj.find_by_path(File.join(self.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(self.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.last.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