Module: Raml::Parent::ClassMethods

Defined in:
lib/raml/mixin/parent.rb

Instance Method Summary collapse

Instance Method Details

#child_of(name, type) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/raml/mixin/parent.rb', line 15

def child_of(name, type)
  type = [ type ] unless type.is_a? Array

  self.instance_eval do
    define_method name do
      @children.select { |child| type.include? child.class }.first
    end
  end
end

#children_by(name, key, type, merge_parents = false) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/raml/mixin/parent.rb', line 35

def children_by(name, key, type, merge_parents=false)
  self.instance_eval do
    define_method name do
      result = Hash[
        @children.
          select { |child| child.is_a? type }.
          map    { |child| [ child.send(key.to_sym), child ] }
      ]

      if merge_parents and parent and parent.respond_to? name
        result = parent.send(name).merge result
      end

      result
    end
  end
end

#children_of(name, type) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/raml/mixin/parent.rb', line 25

def children_of(name, type)
  type = [ type ] unless type.is_a? Array

  self.instance_eval do
    define_method name do
      @children.select { |child| type.include? child.class }
    end
  end
end