class Objection::Base

Public Class Methods

input_types(*args) click to toggle source
# File lib/objection.rb, line 17
def self.input_types(*args)
  @input_types = args[0]
  true
end
new(*args) click to toggle source
# File lib/objection.rb, line 22
def initialize(*args)
  @values = normalize_input(*args)

  check_values!
  define_accessors
  apply_structures!
  check_types!
end
optionals(*args) click to toggle source
# File lib/objection.rb, line 14
def self.optionals(*args)
  @optional_fields = args
end
requires(*args) click to toggle source
# File lib/objection.rb, line 11
def self.requires(*args)
  @required_fields = args
end

Public Instance Methods

to_hash() click to toggle source
# File lib/objection.rb, line 31
def to_hash
  hash = {}
  @values.each_pair do |key, value|
    if value.is_a?(Array)
      hash[key] = value.inject([]) do |items, item|
        if item.respond_to?(:to_hash)
          items << item.to_hash
        else
          items << item
        end
      end
    elsif value.respond_to?(:to_hash)
      hash[key] = value.to_hash
    else
      hash[key] = value
    end
  end
  hash
end

Private Instance Methods

apply_structures!() click to toggle source
# File lib/objection.rb, line 64
def apply_structures!
  input_types.each do |field, type|
    value = self.send(field)
    if !value.is_a?(type) && ( value.is_a?(Hash) || value.class.to_s == 'ActionController::Parameters' )
      @values[field] = type.new(value)
    end
    if !value.is_a?(type) && value.is_a?(Array)
      values = []
      value.each do |item|
        values << type.new(item)
      end
      @values[field] = values
    end
  end
end
blank_required_fields() click to toggle source
# File lib/objection.rb, line 138
def blank_required_fields
  required_fields.select do |field|
    value = @values[field]
    value == '' || value.nil?
  end
end
blank_required_fields?() click to toggle source
# File lib/objection.rb, line 134
def blank_required_fields?
  blank_required_fields.any?
end
check_types!() click to toggle source
# File lib/objection.rb, line 80
def check_types!
  input_types.each do |field, type|
    value = self.send(field)
    unless value.nil?
      if !(value.is_a?(type) || value.is_a?(Array))
        raise TypeError, "#{field} has the wrong type; #{type} was expected, but got #{value.class}"
      end
    end
  end
end
check_values!() click to toggle source
# File lib/objection.rb, line 58
def check_values!
  raise UnknownFieldGiven, unknown_fields.join(', ') + ' for class: ' + self.class.name             if unknown_fields_present?
  raise RequiredFieldMissing, missing_required_fields.join(', ') + ' for class: ' + self.class.name if missing_required_fields?
  raise RequiredFieldEmpty, blank_required_fields.join(', ') + ' for class: ' + self.class.name     if blank_required_fields?
end
define_accessors() click to toggle source
# File lib/objection.rb, line 52
def define_accessors
  known_fields.each{|field| define_getter(field)}
  optional_fields.each{|field| define_optional_setter(field)}
  required_fields.each{|field| define_required_setter(field)}
end
define_getter(fieldname) click to toggle source
# File lib/objection.rb, line 91
def define_getter(fieldname)
  self.class.send(:define_method, "#{fieldname}") do
    @values[fieldname]
  end
end
define_optional_setter(fieldname) click to toggle source
# File lib/objection.rb, line 97
def define_optional_setter(fieldname)
  self.class.send(:define_method, "#{fieldname}=") do |arg|
    @values[fieldname] = arg
    check_types!
  end
end
define_required_setter(fieldname) click to toggle source
# File lib/objection.rb, line 104
def define_required_setter(fieldname)
  self.class.send(:define_method, "#{fieldname}=") do |arg|
    raise RequiredFieldMadeEmpty, fieldname if arg.nil? || arg == ''
    @values[fieldname] = arg
    check_types!
  end
end
input_types() click to toggle source
# File lib/objection.rb, line 165
def input_types
  self.class.instance_variable_get('@input_types') || {}
end
known_fields() click to toggle source
# File lib/objection.rb, line 122
def known_fields
  required_fields + optional_fields
end
missing_required_fields() click to toggle source
# File lib/objection.rb, line 149
def missing_required_fields
  required_fields - present_fields
end
missing_required_fields?() click to toggle source
# File lib/objection.rb, line 145
def missing_required_fields?
  missing_required_fields.any?
end
normalize_input(*args) click to toggle source
# File lib/objection.rb, line 112
def normalize_input(*args)
  if args.any? && ( args[0].is_a?(Hash) || args[0].class.to_s == 'ActionController::Parameters' )
    args[0].inject({}) do |out, (key, value)|
      out.merge(key.to_sym => value)
    end
  else
    {}
  end
end
optional_fields() click to toggle source
# File lib/objection.rb, line 161
def optional_fields
  self.class.instance_variable_get('@optional_fields') || []
end
present_fields() click to toggle source
# File lib/objection.rb, line 153
def present_fields
  @values.keys
end
required_fields() click to toggle source
# File lib/objection.rb, line 157
def required_fields
  self.class.instance_variable_get('@required_fields') || []
end
unknown_fields() click to toggle source
# File lib/objection.rb, line 130
def unknown_fields
  present_fields - known_fields
end
unknown_fields_present?() click to toggle source
# File lib/objection.rb, line 126
def unknown_fields_present?
  unknown_fields.any?
end