Class: OpenShift::Model

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Naming
Includes:
ActiveModel::AttributeMethods, ActiveModel::Conversion, ActiveModel::Dirty, ActiveModel::Observing, ActiveModel::Serializers::JSON, ActiveModel::Serializers::Xml, ActiveModel::Validations
Defined in:
lib/openshift-origin-common/models/model.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeModel

Returns a new instance of Model.



18
19
20
21
22
# File 'lib/openshift-origin-common/models/model.rb', line 18

def initialize
  @persisted = false
  @new_record = true
  @deleted = false
end

Class Method Details

.attr_accessor(*accessors) ⇒ Object



67
68
69
70
# File 'lib/openshift-origin-common/models/model.rb', line 67

def self.attr_accessor(*accessors)
  attr_reader(*accessors)
  attr_writer(*accessors)
end

.attr_reader(*accessors) ⇒ Object



42
43
44
45
46
47
48
49
50
51
# File 'lib/openshift-origin-common/models/model.rb', line 42

def self.attr_reader(*accessors)
  @attribute_methods_generated = false
  define_attribute_methods accessors

  accessors.each do |m|
    define_method(m) do
      instance_variable_get("@#{m}")
    end
  end
end

.attr_writer(*accessors) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/openshift-origin-common/models/model.rb', line 53

def self.attr_writer(*accessors)
  @attribute_methods_generated = false
  define_attribute_methods accessors

  accessors.each do |m|
    class_eval <<-EOF
    def #{m}=(val)
      #{m}_will_change! unless @#{m} == val
      instance_variable_set("@#{m}",val)
    end
    EOF
  end
end

.exclude_attributes(*attributes) ⇒ Object



76
77
78
79
# File 'lib/openshift-origin-common/models/model.rb', line 76

def self.exclude_attributes(*attributes)
  @excludes_attributes = [] unless @excludes_attributes
  @excludes_attributes += attributes
end

.excludes_attributesObject



90
91
92
# File 'lib/openshift-origin-common/models/model.rb', line 90

def self.excludes_attributes
  @excludes_attributes || []
end

.gen_uuidObject



36
37
38
39
40
# File 'lib/openshift-origin-common/models/model.rb', line 36

def self.gen_uuid
  File.open("/proc/sys/kernel/random/uuid", "r") do |file|
    file.gets.strip.gsub("-","")
  end
end

.include_attributes(*attributes) ⇒ Object



81
82
83
84
# File 'lib/openshift-origin-common/models/model.rb', line 81

def self.include_attributes(*attributes)
  @includes_attributes = [] unless @includes_attributes
  @includes_attributes += attributes
end

.includes_attributesObject



86
87
88
# File 'lib/openshift-origin-common/models/model.rb', line 86

def self.includes_attributes
  @includes_attributes || []
end

.primary_key(var_name) ⇒ Object



72
73
74
# File 'lib/openshift-origin-common/models/model.rb', line 72

def self.primary_key(var_name)
  @primary_key = var_name
end

.require_update_attributes(*attributes) ⇒ Object



94
95
96
97
# File 'lib/openshift-origin-common/models/model.rb', line 94

def self.require_update_attributes(*attributes)
  @requires_update_attributes = [] unless @requires_update_attributes
  @requires_update_attributes += attributes
end

.requires_update_attributesObject



99
100
101
102
# File 'lib/openshift-origin-common/models/model.rb', line 99

def self.requires_update_attributes
  #nil indicates all fields require updates
  @requires_update_attributes
end

Instance Method Details

#attributes(should_convert_nested_models = false) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/openshift-origin-common/models/model.rb', line 104

def attributes(should_convert_nested_models=false)
  @attributes = {}

  klass = self.class
  var_names = self.instance_variable_names.map{|n| n[1..-1]}
  while(klass != OpenShift::Model)
    var_names += klass.includes_attributes.map{|n| n.to_s}
    var_names -= ['attributes', 'changed_attributes', 'previously_changed', 'persisted', 'new_record', 'deleted', 'errors', 'validation_context']
    var_names -= klass.excludes_attributes.map{|n| n.to_s}
    klass = klass.superclass
  end

  var_names.each do |name|
    #next if ['attributes', 'changed_attributes', 'previously_changed', 'persisted', 'new_record', 'deleted', 'errors', 'validation_context'].include? name
    #next if self.class.excludes_attributes.include? name.to_sym
    value = self.send(name.to_s)
    if should_convert_nested_models
      @attributes[name] = convert_nested_models(value)
    else
      @attributes[name] = value
    end
  end
  @attributes
end

#attributes=(hash) ⇒ Object



129
130
131
132
133
134
135
136
# File 'lib/openshift-origin-common/models/model.rb', line 129

def attributes=(hash)
  return nil unless hash

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

#deleted?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/openshift-origin-common/models/model.rb', line 32

def deleted?
  @deleted
end

#new_record?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/openshift-origin-common/models/model.rb', line 24

def new_record?
  @new_record
end

#persisted?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/openshift-origin-common/models/model.rb', line 28

def persisted?
  @persisted
end

#reset_stateObject



138
139
140
141
142
143
# File 'lib/openshift-origin-common/models/model.rb', line 138

def reset_state
  @new_record = false
  @persisted = true
  @deleted = false
  @changed_attributes.clear if @changed_attributes
end

#to_xml(options = {}) ⇒ Object



150
151
152
153
154
155
# File 'lib/openshift-origin-common/models/model.rb', line 150

def to_xml(options = {})
  to_xml_opts = {:skip_types => true}
  to_xml_opts.merge!(options.slice(:builder, :skip_instruct))
  to_xml_opts[:root] = options[:tag_name] || self.class.name.underscore.gsub("_","-")
  self.attributes.to_xml(to_xml_opts)
end