Class: GitDS::ModelItemClassProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/git-ds/model/item_proxy.rb

Overview

Proxy a ModelItem class.

This is used to store a link to a ModelItem class instance. A ModelItemList can be passed a ModelItemClassProxy instance as its cls parameter in order to store a list of links to ModelItem class instances.

Instance Method Summary collapse

Constructor Details

#initialize(cls) ⇒ ModelItemClassProxy

Returns a new instance of ModelItemClassProxy.



28
29
30
# File 'lib/git-ds/model/item_proxy.rb', line 28

def initialize(cls)
  @true_class = cls
end

Instance Method Details

#create(parent, args) ⇒ Object

Create a link to ModelItem.

The ModelItem class ident() method will be used to find the ident of the instance in the args Hash.

The full path to the instance is expected to be in the :path key of the args Hash.

If args is not nil or false, the link file will be created on-filesystem as well as in-db.

Raises:



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/git-ds/model/item_proxy.rb', line 74

def create(parent, args)
  link_path = instance_path(parent.path, @true_class.ident(args))
  raise ProxyItemError.new("Invalid ProxyItem path: #{link_path}") if \
        (not link_path) || (link_path.empty?)

  path = args[:path]
  raise ProxyItemError.new('Invalid ModelItem path') if (not path) || \
        (path.empty?)

  # write path to ModelItem into link file at 'instance path'
  args[:fs] ? parent.model.add_fs_item(link_path, path.to_s + "\n") \
            : parent.model.add_item(link_path, path.to_s + "\n")
end

#instance_path(base_path, ident) ⇒ Object

This is passed to the proxied class, as it just returns class_dir + ident.



58
59
60
# File 'lib/git-ds/model/item_proxy.rb', line 58

def instance_path(base_path, ident)
  @true_class.instance_path(base_path, ident)
end

#list_in_path(model, path) ⇒ Object

List ModelItem class instances contained in this list.

Note: this is passed to the proxied class, as it is just a list of idents.

Instantiating and adding an ident is handled by this class.



39
40
41
# File 'lib/git-ds/model/item_proxy.rb', line 39

def list_in_path(model, path)
  @true_class.list_in_path(model, path)
end

#new(model, link_path) ⇒ Object

Return instance of ModelItem class for ‘ident’.

Raises:



46
47
48
49
50
51
52
53
# File 'lib/git-ds/model/item_proxy.rb', line 46

def new(model, link_path)
  # read path to ModelItem instance from link file at 'link_path'
  instance_path = model.get_item(link_path)
  raise ProxyItemError.new("Invalid ProxyItem path: #{link_path}") if \
        (not instance_path) || (instance_path.empty?)

  @true_class.new(model, instance_path.chomp)
end