Module: Taketo::AssociatedNodes::ClassMethods

Defined in:
lib/taketo/associated_nodes.rb

Instance Method Summary collapse

Instance Method Details

#has_nodes(name_plural, name_singular) ⇒ Object

Adds nodes collections to the construct

Example:

class Bar < BaseConstruct has_nodes :foos, :foo end

bar = Bar.new bar.foos # => foos collection bar.append_foo(foo) # adds node the collection bar.find_foo(:foo_name) # find foo in foos collection by name



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/taketo/associated_nodes.rb', line 26

def has_nodes(name_plural, name_singular)
  self.node_types << name_plural
  includable = Module.new do
    define_method "append_#{name_singular}" do |object|
      nodes(name_plural) << object
    end

    define_method "find_#{name_singular}" do |name|
      nodes(name_plural).find_by_name(name)
    end

    define_method name_plural do
      nodes(name_plural)
    end

    define_method "has_#{name_plural}?" do
      nodes(name_plural).any?
    end
  end
  self.send(:include, includable)
end

#node_typesObject



48
49
50
# File 'lib/taketo/associated_nodes.rb', line 48

def node_types
  @node_types ||= []
end