Class: ATDIS::Model

Inherits:
Object
  • Object
show all
Includes:
TypeCastAttributes, Validators, ActiveModel::AttributeMethods, ActiveModel::Validations
Defined in:
lib/atdis/model.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Model

Returns a new instance of Model.



147
148
149
150
151
152
# File 'lib/atdis/model.rb', line 147

def initialize(params={})
  @attributes, @attributes_before_type_cast = {}, {}
  params.each do |attr, value|
    self.send("#{attr}=", value)
  end if params
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



46
47
48
# File 'lib/atdis/model.rb', line 46

def attributes
  @attributes
end

#attributes_before_type_castObject (readonly)

Returns the value of attribute attributes_before_type_cast.



46
47
48
# File 'lib/atdis/model.rb', line 46

def attributes_before_type_cast
  @attributes_before_type_cast
end

#json_left_oversObject

Stores any part of the json that could not be interpreted. Usually signals an error if it isn’t empty.



49
50
51
# File 'lib/atdis/model.rb', line 49

def json_left_overs
  @json_left_overs
end

#json_load_errorObject

Stores any part of the json that could not be interpreted. Usually signals an error if it isn’t empty.



49
50
51
# File 'lib/atdis/model.rb', line 49

def json_load_error
  @json_load_error
end

#urlObject

Returns the value of attribute url.



50
51
52
# File 'lib/atdis/model.rb', line 50

def url
  @url
end

Class Method Details

.attribute_keysObject



154
155
156
# File 'lib/atdis/model.rb', line 154

def self.attribute_keys
  attribute_types.keys
end

.attribute_namesObject

Does what the equivalent on Activerecord does



159
160
161
# File 'lib/atdis/model.rb', line 159

def self.attribute_names
  attribute_types.keys.map{|k| k.to_s}
end

.cast(value, type) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/atdis/model.rb', line 163

def self.cast(value, type)
  # If it's already the correct type (or nil) then we don't need to do anything
  if value.nil? || value.kind_of?(type)
    value
  # Special handling for arrays. When we typecast arrays we actually typecast each member of the array
  elsif value.kind_of?(Array)
    value.map {|v| cast(v, type)}
  elsif type == DateTime
    cast_datetime(value)
  elsif type == URI
    cast_uri(value)
  elsif type == String
    cast_string(value)
  elsif type == Fixnum
    cast_fixnum(value)
  elsif type == RGeo::GeoJSON
    cast_geojson(value)
  # Otherwise try to use Type.interpret to do the typecasting
  elsif type.respond_to?(:interpret)
    type.interpret(value) if value
  else
    raise
  end
end

.interpret(*params) ⇒ Object



89
90
91
92
# File 'lib/atdis/model.rb', line 89

def self.interpret(*params)
  used, unused = partition_by_used(*params)
  new(used.merge(json_left_overs: unused))
end

.partition_by_used(data) ⇒ Object

Partition the data into used and unused by returning [used, unused]



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/atdis/model.rb', line 56

def self.partition_by_used(data)
  used, unused = {}, {}
  if data.respond_to?(:each)
    data.each do |key, value|
      if attribute_keys.include?(key)
        used[key] = value
      else
        unused[key] = value
      end
    end
  else
    unused = data
  end
  [used, unused]
end

.read_json(text) ⇒ Object



78
79
80
81
82
83
84
85
86
87
# File 'lib/atdis/model.rb', line 78

def self.read_json(text)
  begin
    data = MultiJson.load(text, symbolize_keys: true)
    interpret(data)
  rescue MultiJson::LoadError => e
    a = interpret({response: []})
    a.json_load_error = e.to_s
    a
  end
end

.read_url(url) ⇒ Object



72
73
74
75
76
# File 'lib/atdis/model.rb', line 72

def self.read_url(url)
  r = read_json(RestClient.get(url.to_s).to_str)
  r.url = url.to_s
  r
end

Instance Method Details

#json_errorsObject



131
132
133
# File 'lib/atdis/model.rb', line 131

def json_errors
  json_errors_local + json_errors_in_children
end

#json_errors_in_childrenObject



116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/atdis/model.rb', line 116

def json_errors_in_children
  r = []
  attributes.each do |attribute_as_string, value|
    attribute = attribute_as_string.to_sym
    e = errors[attribute]
    if value.respond_to?(:json_errors)
       r += value.json_errors.map{|a, b| [{attribute => a}, b]}
    elsif value.kind_of?(Array)
      f = value.find{|v| v.respond_to?(:json_errors) && !v.json_errors.empty?}
      r += f.json_errors.map{|a, b| [{attribute => [a]}, b]} if f
    end
  end
  r
end

#json_errors_localObject



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/atdis/model.rb', line 100

def json_errors_local
  r = []
  # First show special json error
  if !errors[:json].empty?
    r << [nil, errors[:json]]
  end
  errors.keys.each do |attribute|
    # The :json attribute is special
    if attribute != :json
      e = errors[attribute]
      r << [{attribute => attributes_before_type_cast[attribute.to_s]}, e.map{|m| ErrorMessage["#{attribute} #{m}", m.spec_section]}] unless e.empty?
    end
  end
  r
end

#json_left_overs_is_emptyObject



140
141
142
143
144
145
# File 'lib/atdis/model.rb', line 140

def json_left_overs_is_empty
  if json_left_overs && !json_left_overs.empty?
    # We have extra parameters that shouldn't be there
    errors.add(:json, ErrorMessage["Unexpected parameters in json data: #{MultiJson.dump(json_left_overs)}", "4"])
  end
end

#json_loaded_correctly!Object



94
95
96
97
98
# File 'lib/atdis/model.rb', line 94

def json_loaded_correctly!
  if json_load_error
    errors.add(:json, ErrorMessage["Invalid JSON: #{json_load_error}", nil])
  end
end

#used_attribute?(a) ⇒ Boolean

Have we tried to use this attribute?

Returns:

  • (Boolean)


136
137
138
# File 'lib/atdis/model.rb', line 136

def used_attribute?(a)
  !attributes_before_type_cast[a].nil?
end