Module: Pulp::Common::Lifecycle::ClassMethods

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

Instance Method Summary collapse

Instance Method Details

#has_collection(options = {}) ⇒ Object

optional features



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/pulp/common/lifecycle.rb', line 83

def has_collection(options={})
  
  instance_eval %{ def all
    base_get('').collect {|e| self.new(e) }
  end}
  
  options[:all_filters] && options[:all_filters].each do |filter|
    instance_eval %{
      def find_by_#{filter}(f)
        base_get('',nil,{ :#{filter} => f }).collect {|e| self.new(e) }
      end}
  end
end

#has_createObject



109
110
111
# File 'lib/pulp/common/lifecycle.rb', line 109

def has_create
  include Pulp::Common::Lifecycle::Create
end

#has_crud(options = {}) ⇒ Object



97
98
99
100
101
102
103
# File 'lib/pulp/common/lifecycle.rb', line 97

def has_crud(options={})
  has_collection(options[:collection]||{})
  has_create
  has_get
  has_update
  has_delete
end

#has_deleteObject



105
106
107
# File 'lib/pulp/common/lifecycle.rb', line 105

def has_delete
  include Pulp::Common::Lifecycle::Delete
end

#has_getObject



113
114
115
# File 'lib/pulp/common/lifecycle.rb', line 113

def has_get
  include Pulp::Common::Lifecycle::Get
end

#has_updateObject



117
118
119
# File 'lib/pulp/common/lifecycle.rb', line 117

def has_update
  include Pulp::Common::Lifecycle::Update
end

#locked_fieldsObject



160
161
162
# File 'lib/pulp/common/lifecycle.rb', line 160

def locked_fields
  @locked_fields ||= []
end

#pulp_action(action_name, options = {}) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/pulp/common/lifecycle.rb', line 121

def pulp_action(action_name, options={})
  options[:method] ||= :post
  options[:params] = true if options[:params].nil?
  # default is true
  slash = (options[:append_slash].nil? || options[:append_slash]) ? '/' : '' 
  if options[:returns]
    module_eval %{
    def #{action_name}(#{"params#{"={}" if options[:params] == :optional}" if options[:params]})
      #{options[:returns]}.new(self.class.base_#{(options[:parse] == false) ? 'unparsed_' : '' }#{options[:method]}('#{action_name}#{slash}',self.id,#{options[:params] ? 'params' : 'nil' }))
    end}
  else
    module_eval %{
    def #{action_name}(#{"params#{"={}" if options[:params] == :optional}" if options[:params]})
      self.class.base_#{(options[:parse] == false) ? 'unparsed_' : '' }#{options[:method]}('#{action_name}#{slash}',self.id,#{options[:params] ? 'params' : 'nil' })
    end}
  end
  if options[:task_list]
    module_eval %{
      def #{action_name}_tasks
        self.class.base_get('#{action_name}#{slash}',self.id).collect{|p| Pulp::Task.new(p) }
      end}
  end
end

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



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/pulp/common/lifecycle.rb', line 43

def pulp_deferred_field(field,options={})
  if options[:array]
    module_eval("def #{field}() self.class.plain_get(fields['#{field}']).collect{|p| #{options[:array]}.new(p) }; end")
  elsif options[:returns] == :plain
    module_eval("def #{field}() self.class.plain_base.connection[fields['#{field}'].sub(/^\\/pulp\\/api\\//,'')].get; end")
  elsif options[:returns]
    module_eval("def #{field}() (res = self.class.plain_get(fields['#{field}'])).empty? ? nil : #{options[:returns]}.new(res); end")
  else
    module_eval("def #{field}() self.class.plain_get(fields['#{field}']); end")
  end
  module_eval("def #{field}_link() fields['#{field}']; end")
end

#pulp_deferred_fields(*fields) ⇒ Object

Declare many deffered fields at once.



57
58
59
# File 'lib/pulp/common/lifecycle.rb', line 57

def pulp_deferred_fields(*fields)
  [*fields].each {|field| pulp_deferred_field(field) }
end

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

Allows for dynamically declaring fields that will come from Pulp.



29
30
31
32
33
34
35
36
# File 'lib/pulp/common/lifecycle.rb', line 29

def pulp_field(field,options={})
  locked_fields << field if options[:locked]

  module_eval("def #{field}() user_fields['#{field}'] || fields['#{field}']; end")
  module_eval("def #{field}=(val) user_fields['#{field}'] = val; end") unless options[:locked]
  
  record_fields[field] = options
end

#pulp_fields(*fields) ⇒ Object

Declare many fields at once.



39
40
41
# File 'lib/pulp/common/lifecycle.rb', line 39

def pulp_fields(*fields)
  fields.to_a.each {|field| pulp_field(field) }
end

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

declare a field that is locked



62
63
64
# File 'lib/pulp/common/lifecycle.rb', line 62

def pulp_locked_field(field,options={})
  pulp_field field, options.merge(:locked => true)
end

#pulp_locked_fields(*fields) ⇒ Object

Declare many locked fields at once.



67
68
69
# File 'lib/pulp/common/lifecycle.rb', line 67

def pulp_locked_fields(*fields)
  [*fields].each {|field| pulp_locked_field(field) }
end

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

special fields are locked and registered as being special



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

def pulp_special_field(field,options={})
  pulp_locked_field(field,options)
  special_fields << field
end

#pulp_special_fields(*fields) ⇒ Object

Declare multiple special fields at once



78
79
80
# File 'lib/pulp/common/lifecycle.rb', line 78

def pulp_special_fields(*fields)
  [*fields].each{|f| pulp_special_field(f) }
end

#pulp_update_action(action_name, options) ⇒ Object



145
146
147
148
149
150
# File 'lib/pulp/common/lifecycle.rb', line 145

def pulp_update_action(action_name,options)
  module_eval %{
    def update_#{action_name}(#{[*options[:params]].join(', ')})
      self.class.base_put('',self.id,{ #{[*options[:params]].collect{|p| ":#{p} => #{p}" }.join(', ')} })
    end}
end

#record_fieldsObject



156
157
158
# File 'lib/pulp/common/lifecycle.rb', line 156

def record_fields
  @record_fields ||= {}
end

#special_fieldsObject



152
153
154
# File 'lib/pulp/common/lifecycle.rb', line 152

def special_fields
  @special_fields ||= []
end