Class: Requisite::ApiModel

Inherits:
BoundaryObject show all
Defined in:
lib/requisite/api_model.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BoundaryObject

attribute, attribute!, attribute_keys, attribute_keys_with_inheritance, serialized_attributes

Constructor Details

#initialize(model = {}) ⇒ ApiModel

Returns a new instance of ApiModel.



7
8
9
# File 'lib/requisite/api_model.rb', line 7

def initialize(model={})
  @model = model.kind_of?(Hash) ? Hash[model.map{ |k, v| [k.to_sym, v] }] : model
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



5
6
7
# File 'lib/requisite/api_model.rb', line 5

def model
  @model
end

Instance Method Details

#attribute_from_model(name) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/requisite/api_model.rb', line 19

def attribute_from_model(name)
  if @model.kind_of?(Hash)
    @model[name]
  else
    @model.send(name) if @model.respond_to?(name)
  end
end

#convert(name) ⇒ Object



11
12
13
# File 'lib/requisite/api_model.rb', line 11

def convert(name)
  attribute_from_model(name)
end

#convert!(name) ⇒ Object



15
16
17
# File 'lib/requisite/api_model.rb', line 15

def convert!(name)
  attribute_from_model(name) || (raise NotImplementedError.new("'#{name}' not found on model"))
end

#first_attribute_from_model(*attributes) ⇒ Object



82
83
84
85
86
87
88
89
90
# File 'lib/requisite/api_model.rb', line 82

def first_attribute_from_model(*attributes)
  attributes.each do |attribute|
    value = attribute_from_model(attribute)
    if value && !(value.kind_of?(Hash) && value.empty?)
      return value
    end
  end
  nil
end

#merge_attribute_if_exists!(to_merge, attribute_name) ⇒ Object



27
28
29
# File 'lib/requisite/api_model.rb', line 27

def merge_attribute_if_exists!(to_merge, attribute_name)
  attribute_from_model(attribute_name) ? to_merge.merge!(attribute_from_model(attribute_name)) : to_merge
end

#parse_scalar_hash(name) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/requisite/api_model.rb', line 56

def parse_scalar_hash(name)
  {}.tap do |result|
    passed_hash = attribute_from_model(name) || {}
    passed_hash.each do |key, value|
      raise BadTypeError.new(value, 'Numeric, String or Boolean') unless (value.kind_of?(Numeric) || value.kind_of?(String) || value.kind_of?(TrueClass) || value.kind_of?(FalseClass))
      result[key] = value
    end
  end
end

#parse_typed_array(name, type) ⇒ Object



66
67
68
69
70
71
72
73
74
# File 'lib/requisite/api_model.rb', line 66

def parse_typed_array(name, type)
  [].tap do |result|
    passed_array = attribute_from_model(name) || []
    passed_array.each do |value|
      raise_bad_type_if_type_mismatch(value, type)
      result << value
    end
  end
end

#parse_typed_hash(name, hash) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/requisite/api_model.rb', line 45

def parse_typed_hash(name, hash)
  {}.tap do |result|
    passed_hash = attribute_from_model(name)
    hash.each do |key, value|
      next unless passed_hash && passed_hash[key]
      raise_bad_type_if_type_mismatch(passed_hash[key], value)
      result[key] = passed_hash[key]
    end
  end
end

#to_hashObject



31
32
33
34
35
36
37
38
39
# File 'lib/requisite/api_model.rb', line 31

def to_hash
  preprocess_model
  {}.tap do |result|
    self.class.attribute_keys_with_inheritance.each do |meth|
      value = self.send(meth)
      result.merge!({meth => value}) unless value.nil?
    end
  end
end

#to_jsonObject



41
42
43
# File 'lib/requisite/api_model.rb', line 41

def to_json
  to_hash.to_json
end

#with_type!(desired_type) ⇒ Object



76
77
78
79
80
# File 'lib/requisite/api_model.rb', line 76

def with_type!(desired_type)
  yield.tap do |value|
    raise_bad_type_if_type_mismatch(value, desired_type) if value
  end
end