class StickShift::Model

Public Class Methods

attr_accessor(*accessors) click to toggle source
# File lib/stickshift-common/models/model.rb, line 67
def self.attr_accessor(*accessors)
  attr_reader(*accessors)
  attr_writer(*accessors)
end
attr_reader(*accessors) click to toggle source
# File lib/stickshift-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) click to toggle source
# File lib/stickshift-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 "        def #{m}=(val)
          #{m}_will_change! unless @#{m} == val
          instance_variable_set("@#{m}",val)
        end
"
      end
    end
exclude_attributes(*attributes) click to toggle source
# File lib/stickshift-common/models/model.rb, line 76
def self.exclude_attributes(*attributes)
  @excludes_attributes = [] unless @excludes_attributes
  @excludes_attributes += attributes
end
excludes_attributes() click to toggle source
# File lib/stickshift-common/models/model.rb, line 90
def self.excludes_attributes
  @excludes_attributes || []
end
gen_uuid() click to toggle source
# File lib/stickshift-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) click to toggle source
# File lib/stickshift-common/models/model.rb, line 81
def self.include_attributes(*attributes)
  @includes_attributes = [] unless @includes_attributes
  @includes_attributes += attributes
end
includes_attributes() click to toggle source
# File lib/stickshift-common/models/model.rb, line 86
def self.includes_attributes
  @includes_attributes || []
end
new() click to toggle source
# File lib/stickshift-common/models/model.rb, line 18
def initialize
  @persisted = false
  @new_record = true
  @deleted = false
end
primary_key(var_name) click to toggle source
# File lib/stickshift-common/models/model.rb, line 72
def self.primary_key(var_name)
  @primary_key = var_name
end
require_update_attributes(*attributes) click to toggle source
# File lib/stickshift-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_attributes() click to toggle source
# File lib/stickshift-common/models/model.rb, line 99
def self.requires_update_attributes
  #nil indicates all fields require updates
  @requires_update_attributes
end

Protected Class Methods

pk() click to toggle source
# File lib/stickshift-common/models/model.rb, line 159
def self.pk
  @primary_key
end

Public Instance Methods

attributes(should_convert_nested_models=false) click to toggle source
# File lib/stickshift-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 != StickShift::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) click to toggle source
# File lib/stickshift-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?() click to toggle source
# File lib/stickshift-common/models/model.rb, line 32
def deleted?
  @deleted
end
new_record?() click to toggle source
# File lib/stickshift-common/models/model.rb, line 24
def new_record?
  @new_record
end
persisted?() click to toggle source
# File lib/stickshift-common/models/model.rb, line 28
def persisted?
  @persisted
end
reset_state() click to toggle source
# File lib/stickshift-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 = {}) click to toggle source
# File lib/stickshift-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

Protected Instance Methods

convert_nested_models(obj) click to toggle source
# File lib/stickshift-common/models/model.rb, line 163
def convert_nested_models(obj)
  case obj
  when StickShift::Model
    obj.attributes(true)
  when Hash
    convert_nested_models_in_hash(obj)
  when Array
    convert_nested_models_in_array(obj)
  else
    obj
  end
end
convert_nested_models_in_array(arr) click to toggle source
# File lib/stickshift-common/models/model.rb, line 193
def convert_nested_models_in_array(arr)
  arr.map do |value|
    case value
    when StickShift::Model
      convert_nested_models(value)
    when Hash
      convert_nested_models_in_hash(value)
    when Array
      convert_nested_models_in_array(value)
    else
      value
    end
  end
end
convert_nested_models_in_hash(hash) click to toggle source
# File lib/stickshift-common/models/model.rb, line 176
def convert_nested_models_in_hash(hash)
  ret = {}
  hash.each do |k,value|
    case value
    when StickShift::Model
      ret[k]=convert_nested_models(value)
    when Hash
      ret[k]=convert_nested_models_in_hash(value)
    when Array
      ret[k]=convert_nested_models_in_array(value)
    else
      ret[k]=value
    end
  end
  ret
end