Class: Marley::Reggae

Inherits:
Array show all
Defined in:
lib/marley/reggae.rb

Overview

See Also:

Direct Known Subclasses

ReggaeResource

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Reggae

Returns a new instance of Reggae.



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/marley/reggae.rb', line 30

def initialize(*args)
  super 
  if is_resource?
    @resource_type=self[0]=self[0].to_sym
    self[1]=Utils.hash_keys_to_syms(self[1]) if self[1].class==Hash
    @properties=self[1]
    @contents=self[2]
    self.class.mk_prop_methods
  else
    replace(map {|r| r.class==Array ? Reggae.new(r).to_resource : r})
  end
end

Instance Attribute Details

#contentsObject

Returns the value of attribute contents.



29
30
31
# File 'lib/marley/reggae.rb', line 29

def contents
  @contents
end

#propertiesObject

Returns the value of attribute properties.



29
30
31
# File 'lib/marley/reggae.rb', line 29

def properties
  @properties
end

#resource_typeObject (readonly)

Returns the value of attribute resource_type.



28
29
30
# File 'lib/marley/reggae.rb', line 28

def resource_type
  @resource_type
end

Class Method Details

.get_resource(*args) ⇒ Object



24
25
26
# File 'lib/marley/reggae.rb', line 24

def get_resource(*args)
  self.new(*args).to_resource
end

.mk_prop_methodsObject



18
19
20
21
22
23
# File 'lib/marley/reggae.rb', line 18

def mk_prop_methods
  @properties && @properties.each do |meth|
    define_method(meth) {properties[meth].respond_to?(:to_resource) ? properties[meth].to_resource : properties[meth]} 
    define_method(:"#{meth}=") {|val|properties[meth]=val} 
  end
end

.properties(*args) ⇒ Object



14
15
16
17
# File 'lib/marley/reggae.rb', line 14

def properties(*args)
  @properties=args if args
  @properties
end

Instance Method Details

#find_instances(rn, instances = Reggae.new([])) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/marley/reggae.rb', line 53

def find_instances(rn,instances=Reggae.new([]))
  if self.class==ReggaeInstance && self.name.to_s==rn.to_s 
    instances << self
  else
    (is_resource? ? contents : self).each {|a| a && Reggae.new(a).to_resource.find_instances(rn,instances)}
  end
  instances
end

#is_resource?Boolean

Returns:

  • (Boolean)


42
43
44
45
# File 'lib/marley/reggae.rb', line 42

def is_resource?
  #below is for 1.8-1.9 and is fucking awful!
  [String, Symbol].include?(self[0].class) && (Marley.constants.include?("reggae_#{self[0]}".camelcase) || Marley.constants.include?("reggae_#{self[0]}".camelcase.to_sym))
end

#to_resourceObject



50
51
52
# File 'lib/marley/reggae.rb', line 50

def to_resource
  is_resource? ? Marley.const_get("Reggae#{resource_type.to_s.camelize}".to_sym).new(self) : self
end