Class: Yutani::Resource

Inherits:
Object
  • Object
show all
Includes:
Hiera
Defined in:
lib/yutani/resource.rb

Direct Known Subclasses

SubResource

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Hiera

#hiera, init_hiera, lookup, pop, push, scope

Constructor Details

#initialize(resource_type, *namespace, &block) ⇒ Resource

Returns a new instance of Resource.



7
8
9
10
11
12
13
# File 'lib/yutani/resource.rb', line 7

def initialize(resource_type, *namespace, &block)
  @resource_type      = resource_type
  @namespace          = namespace
  @fields             = {}

  Docile.dsl_eval(self, &block) if block_given?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/yutani/resource.rb', line 40

def method_missing(name, *args, &block)
  if name =~ /ref_(.*)/
    # redirect ref_id, ref_name, etc, to ref()
    ref(*args, $1)
  elsif block_given?
    # handle sub resources, like tags, listener, etc
    sub = SubResource.new
    sub.instance_exec(&block)
    @fields[name] = sub.fields
  else
    @fields[name] = args.first
  end
end

Instance Attribute Details

#fieldsObject

Returns the value of attribute fields.



5
6
7
# File 'lib/yutani/resource.rb', line 5

def fields
  @fields
end

#namespaceObject

Returns the value of attribute namespace.



5
6
7
# File 'lib/yutani/resource.rb', line 5

def namespace
  @namespace
end

#resource_typeObject

Returns the value of attribute resource_type.



5
6
7
# File 'lib/yutani/resource.rb', line 5

def resource_type
  @resource_type
end

Instance Method Details

#ref(resource_type, *namespace, attr) ⇒ Object



27
28
29
30
# File 'lib/yutani/resource.rb', line 27

def ref(resource_type, *namespace, attr)
  "${%s}" % [resource_type, namespace.to_underscored_string, attr].
    join('.')
end

#resource_nameObject



15
16
17
# File 'lib/yutani/resource.rb', line 15

def resource_name
  @namespace.to_underscored_string
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/yutani/resource.rb', line 36

def respond_to_missing?(method_name, include_private = false)
  true
end

#template(path, **kv) ⇒ Object



32
33
34
# File 'lib/yutani/resource.rb', line 32

def template(path, **kv)
  Template.new(kv).render(path)
end

#to_hObject



19
20
21
22
23
24
25
# File 'lib/yutani/resource.rb', line 19

def to_h
  {
    @resource_type => {
      resource_name => @fields
    }
  }
end