class Collector::BaseModel

Public Class Methods

attribute(*args)
Alias for: attributes
attribute_opt(*args)
Alias for: attributes_opt
attributes(*args) click to toggle source
# File lib/collector/base_model.rb, line 7
def attributes(*args)
  @attributes ||= []
  @attributes += args
  args.each{|attr| attr_accessor attr }
end
Also aliased as: attribute
attributes_opt(*args) click to toggle source
# File lib/collector/base_model.rb, line 12
def attributes_opt(*args)
  @attributes_opt ||= []
  @attributes_opt += args
  args.each{|attr| attr_accessor attr }
end
Also aliased as: attribute_opt
new(hash = {}) click to toggle source
# File lib/collector/base_model.rb, line 49
def initialize(hash = {})
  validate_input(hash) unless self.class.swallow_unsupported_attributes?
  all_attributes.each do |attr|
    val = hash[attr] || hash[attr.to_s]
    self.send("#{attr}=", val)
  end
end
swallow_unsupported_attributes() click to toggle source
# File lib/collector/base_model.rb, line 21
def swallow_unsupported_attributes
  @swallow_unsupported_attributes = true
end
swallow_unsupported_attributes?() click to toggle source
# File lib/collector/base_model.rb, line 25
def swallow_unsupported_attributes?
  @swallow_unsupported_attributes
end

Public Instance Methods

==(other) click to toggle source
# File lib/collector/base_model.rb, line 57
def ==(other)
  self.class.attributes.each do |attr|
    return false if self.send(attr) != other.send(attr)
  end
  true
end
all_attributes() click to toggle source
# File lib/collector/base_model.rb, line 38
def all_attributes
  attributes + attributes_opt
end
attributes() click to toggle source
# File lib/collector/base_model.rb, line 30
def attributes
  self.class.instance_variable_get("@attributes") || []
end
attributes_opt() click to toggle source
# File lib/collector/base_model.rb, line 34
def attributes_opt
  self.class.instance_variable_get("@attributes_opt") || []
end
has_required_attributes?() click to toggle source
# File lib/collector/base_model.rb, line 64
def has_required_attributes?
  missing_attributes.empty?
end
missing_attributes() click to toggle source
# File lib/collector/base_model.rb, line 68
def missing_attributes
  own_missing + nested_missing
end
missing_attributes_human_readable(prefix = "", missing_attr = nil) click to toggle source
# File lib/collector/base_model.rb, line 97
def missing_attributes_human_readable(prefix = "", missing_attr = nil)
  missing_attr = missing_attributes if missing_attr.nil?
  missing_key_paths = missing_attr.map.each_with_index do |item|
    if item.kind_of? Hash
      key = item.keys.first
      missing_attributes_human_readable(prefix + "#{key}.", item[key])
    else
      "#{prefix}#{item}"
    end
  end
  if prefix.empty?
    "Missing attributes: " + missing_key_paths.flatten.join(", ")
  else
    missing_key_paths
  end
end
nested_missing() click to toggle source
# File lib/collector/base_model.rb, line 76
def nested_missing
  missing = []
  all_attributes.each do |attr|
    val = send(attr)
    if !!val
      if val.kind_of? BaseModel
        nested_missing = val.missing_attributes
        missing << {attr => nested_missing} unless nested_missing.empty?
      elsif val.kind_of? Array
        val.each_with_index do |nested_val, index|
          if nested_val.kind_of? BaseModel
            nested_missing = nested_val.missing_attributes
            missing << {"#{attr}[#{index}]" => nested_missing} unless nested_missing.empty?
          end
        end
      end
    end
  end
  missing
end
own_missing() click to toggle source
# File lib/collector/base_model.rb, line 72
def own_missing
  attributes.select{|attr| send(attr).nil? }
end
validate_input(hash) click to toggle source
# File lib/collector/base_model.rb, line 42
def validate_input(hash)
  unsupported_keys = hash.keys.map(&:to_sym) - all_attributes
  unless unsupported_keys.empty?
    raise ArgumentError.new("Unsupported attribute(s): #{unsupported_keys}")
  end
end