Module: Cobbler::Common::Lifecycle::ClassMethods

Defined in:
lib/cobbler/common/lifecycle.rb

Instance Method Summary collapse

Instance Method Details

#api_methodsObject



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/cobbler/common/lifecycle.rb', line 58

def api_methods
    return @api_methods if @api_methods
    model_name = self.name.gsub(/.*::/,'').underscore
    @api_methods = {
      :find_all => "get_#{model_name.pluralize}",
      :find_one => "get_#{model_name}",
      :handle => "get_#{model_name}_handle",
      :remove => "remove_#{model_name}",
      :save => "save_#{model_name}",
      :new => "new_#{model_name}",
      :modify => "modify_#{model_name}"
    }
end

#cobbler_collection(field, options = {}) ⇒ Object

Allows a field to be defined as a collection of objects. The type for that other class must be provided.



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/cobbler/common/lifecycle.rb', line 130

def cobbler_collection(field, options={})
    classname = options[:type] || 'String'
    packing = options[:packing] ? options[:packing].to_s.classify : 'Array'
    
    packing_code = {
              'Array' => "(definitions['#{field}']||[]).each{|value| new_value << #{classname}.new(value) }",
              'Hash' => "(definitions['#{field}']||{}).each{|key,value| new_value[key] = #{classname}.new(value) }"
    }
    
    cobbler_collections_store_callbacks << options[:store] if options[:store]
    # unless we have a seperate store callback we store collections normally
    cobbler_record_fields << field unless options[:store]
    
    module_eval <<-"MEND"
    def #{field}
        if !user_definitions['#{field}'] && !definitions['#{field}'].is_a?(#{packing})
                                                                           new_value = #{packing}.new
                                                                           #{packing_code[packing]}
                                                                           definitions['#{field}'] = new_value
        end
        user_definitions['#{field}'] ||= definitions['#{field}']
        # return always the user_definitions as we might do operations on these objects, e.g. <<
        user_definitions['#{field}'] 
    end
    
    def #{field}=(value)
        user_definitions['#{field}'] = value
    end
    MEND
end

#cobbler_collections_store_callbacksObject



80
81
82
# File 'lib/cobbler/common/lifecycle.rb', line 80

def cobbler_collections_store_callbacks
    @cobbler_collections_store_callbacks ||= []
end

#cobbler_field(field, options = {}) ⇒ Object

Allows for dynamically declaring fields that will come from Cobbler.



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/cobbler/common/lifecycle.rb', line 94

def cobbler_field(field,options={})
    # name is always locked and findable as this is a special field
    if field == :name
        options[:locked] = true if options[:locked] || options[:locked].nil?
        options[:findable] = api_methods[:find_one] if options[:findable] || options[:findable].nil? 
    end
    options.each do |key,value|
        case key
            when :findable then
            if value
                module_eval <<-"MEND"
                def self.find_by_#{field}(value)
                    properties = in_transaction{ make_call('#{value}',value) }
                    valid_properties?(properties) ? new(properties,false) : nil
                end
                MEND
            end
            when :locked then
            locked_fields << field if value
        end
    end
    
    module_eval("def #{field}() user_definitions['#{field}'] || definitions['#{field}']; end")
    module_eval("def #{field}=(val) user_definitions['#{field}'] = val; end")
    
    cobbler_record_fields << field
end

#cobbler_fields(*fields) ⇒ Object

Declare many fields at once.



123
124
125
# File 'lib/cobbler/common/lifecycle.rb', line 123

def cobbler_fields(*fields)
    fields.to_a.each {|field| cobbler_field field }
end

#cobbler_lifecycle(lookup_methods = {}) ⇒ Object

Define/adjust all necessary lookup methods for a usual cobbler item.



87
88
89
# File 'lib/cobbler/common/lifecycle.rb', line 87

def cobbler_lifecycle(lookup_methods={})
    api_methods.merge!(lookup_methods)
end

#cobbler_record_fieldsObject



72
73
74
# File 'lib/cobbler/common/lifecycle.rb', line 72

def cobbler_record_fields
    @cobbler_record_fields ||= []
end

#locked_fieldsObject



76
77
78
# File 'lib/cobbler/common/lifecycle.rb', line 76

def locked_fields
    @locked_fields ||= []
end