Class: DNApi::Struct

Inherits:
Struct
  • Object
show all
Extended by:
Test::Ext, Forwardable
Defined in:
lib/dnapi/struct.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Test::Ext

default_fauxture_name, fixture, generate, generate_attributes, pick

Constructor Details

#initialize(attrs = {}) ⇒ Struct

Returns a new instance of Struct.

Raises:

  • (ArgumentError)


90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/dnapi/struct.rb', line 90

def initialize(attrs = {})
  raise(ArgumentError, "#{attrs.inspect} is not a hash") unless attrs.is_a?(Hash)

  mattrs = attrs.to_mash
  super(*members.map {|m| mattrs.delete(m) })

  self::class._many.each do |assoc|
    send(:"#{assoc}=", mattrs.delete(assoc) || [])
  end

  self::class._ones.each do |assoc|
    send(:"#{assoc}=", mattrs.delete(assoc) || nil)
  end

  mattrs.each do |key, value|
    send("#{key}=", value)
  end
end

Instance Attribute Details

#parentObject

Returns the value of attribute parent.



15
16
17
# File 'lib/dnapi/struct.rb', line 15

def parent
  @parent
end

Class Method Details

._manyObject



11
# File 'lib/dnapi/struct.rb', line 11

def _many() @_many ||= [] end

._onesObject



12
# File 'lib/dnapi/struct.rb', line 12

def _ones() @_ones ||= [] end

._umembersObject



13
# File 'lib/dnapi/struct.rb', line 13

def _umembers() @_umembers ||= [] end

.belongs_to(name) ⇒ Object



80
81
82
83
# File 'lib/dnapi/struct.rb', line 80

def self.belongs_to(name)
  alias_method name, :parent
  alias_method "#{name}=", :parent=
end

.from(jexp) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/dnapi/struct.rb', line 30

def self.from(jexp)
  _many.each do |assoc|
    jexp[assoc.to_s] = (jexp[assoc.to_s] || []).map do |item|
      if klass = map[assoc.to_sym]
        klass.from(item)
      end
    end.compact
  end

  _ones.each do |assoc|
    if jexp[assoc.to_s]
      jexp[assoc.to_s] = begin
        item = jexp[assoc.to_s]
        if klass = map[assoc.to_sym]
          klass.from(item)
        end
      end
    end
  end
  new(jexp)
end

.many(*names) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/dnapi/struct.rb', line 52

def self.many(*names)
  _many.push(*names)
  attr_reader(*names)
  names.each do |name|
    class_eval <<-EOT, __FILE__, __LINE__ + 1
      def #{name}=(values)
        values.each do |value|
          value.parent = self if value.is_a?(DNApi::Struct)
        end
        @#{name} = values
      end
    EOT
  end
end

.mapObject



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/dnapi/struct.rb', line 17

def self.map
  {
    :instances  => DNApi::Instance,
    :apps       => DNApi::App,
    :crons      => DNApi::Cron,
    :gems       => DNApi::GemDep,
    :ebuilds    => DNApi::EbuildDep,
    :ssl_cert   => DNApi::SSLCert,
    :vhosts     => DNApi::VHost,
    :components => DNApi::Component,
  }
end

.one(*names) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/dnapi/struct.rb', line 67

def self.one(*names)
  _ones.push(*names)
  attr_reader(*names)
  names.each do |name|
    class_eval <<-EOT, __FILE__, __LINE__ + 1
      def #{name}=(value)
        value.parent = self if value.is_a?(DNApi::Struct)
        @#{name} = value
      end
    EOT
  end
end

.unserialized_member(*names) ⇒ Object



85
86
87
88
# File 'lib/dnapi/struct.rb', line 85

def self.unserialized_member(*names)
  _umembers.push(*names)
  attr_accessor(*names)
end

Instance Method Details

#==(other) ⇒ Object



134
135
136
137
138
# File 'lib/dnapi/struct.rb', line 134

def ==(other)
  super && self.class._umembers.all? do |umember|
    self.send(umember) == other.send(umember)
  end
end

#_manyObject



109
110
111
# File 'lib/dnapi/struct.rb', line 109

def _many
  self.class._many
end

#_many_valuesObject



117
118
119
# File 'lib/dnapi/struct.rb', line 117

def _many_values
  _many.map {|assoc| send(assoc)}
end

#_onesObject



113
114
115
# File 'lib/dnapi/struct.rb', line 113

def _ones
  self.class._ones
end

#_ones_valuesObject



121
122
123
# File 'lib/dnapi/struct.rb', line 121

def _ones_values
  _ones.map {|assoc| send(assoc)}
end

#inspectObject



140
141
142
# File 'lib/dnapi/struct.rb', line 140

def inspect
  super.gsub(/>$/, ' ' + umember_inspect + '>')
end

#to_hashObject



125
126
127
128
129
130
131
132
# File 'lib/dnapi/struct.rb', line 125

def to_hash
  data = members.zip(values)
  association_data = _many.zip(_many_values) + _ones.zip(_ones_values)
  hash = (data + association_data).inject({}) do |h,(k,v)| 
    h.update(k => v)
  end #.reject {|k,v| v.nil? }
  hash
end

#umember_inspectObject



144
145
146
# File 'lib/dnapi/struct.rb', line 144

def umember_inspect
  self.class._umembers.map {|u| u.to_s + '=' + send(u).inspect } * ' '
end