Class: ATDIS::Model
- Inherits:
-
Object
- Object
- ATDIS::Model
- Includes:
- TypeCastAttributes, Validators, ActiveModel::AttributeMethods, ActiveModel::Validations
- Defined in:
- lib/atdis/model.rb
Direct Known Subclasses
ATDIS::Models::Address, ATDIS::Models::Application, ATDIS::Models::Authority, ATDIS::Models::Document, ATDIS::Models::Event, ATDIS::Models::Info, ATDIS::Models::LandTitleRef, ATDIS::Models::Location, ATDIS::Models::Page, ATDIS::Models::Pagination, ATDIS::Models::Person, ATDIS::Models::Reference, ATDIS::Models::Response, ATDIS::Models::TorrensTitle
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#attributes_before_type_cast ⇒ Object
readonly
Returns the value of attribute attributes_before_type_cast.
-
#json_left_overs ⇒ Object
Stores any part of the json that could not be interpreted.
-
#json_load_error ⇒ Object
Stores any part of the json that could not be interpreted.
-
#url ⇒ Object
Returns the value of attribute url.
Class Method Summary collapse
- .attribute_keys ⇒ Object
-
.attribute_names ⇒ Object
Does what the equivalent on Activerecord does.
- .cast(value, type) ⇒ Object
- .interpret(*params) ⇒ Object
-
.partition_by_used(data) ⇒ Object
Partition the data into used and unused by returning [used, unused].
- .read_json(text) ⇒ Object
- .read_url(url) ⇒ Object
Instance Method Summary collapse
-
#initialize(params = {}) ⇒ Model
constructor
A new instance of Model.
- #json_errors ⇒ Object
- #json_errors_in_children ⇒ Object
- #json_errors_local ⇒ Object
- #json_left_overs_is_empty ⇒ Object
- #json_loaded_correctly! ⇒ Object
-
#used_attribute?(a) ⇒ Boolean
Have we tried to use this attribute?.
Constructor Details
#initialize(params = {}) ⇒ Model
Returns a new instance of Model.
146 147 148 149 150 151 |
# File 'lib/atdis/model.rb', line 146 def initialize(params={}) @attributes, @attributes_before_type_cast = {}, {} params.each do |attr, value| self.send("#{attr}=", value) end if params end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
45 46 47 |
# File 'lib/atdis/model.rb', line 45 def attributes @attributes end |
#attributes_before_type_cast ⇒ Object (readonly)
Returns the value of attribute attributes_before_type_cast.
45 46 47 |
# File 'lib/atdis/model.rb', line 45 def attributes_before_type_cast @attributes_before_type_cast end |
#json_left_overs ⇒ Object
Stores any part of the json that could not be interpreted. Usually signals an error if it isn’t empty.
48 49 50 |
# File 'lib/atdis/model.rb', line 48 def json_left_overs @json_left_overs end |
#json_load_error ⇒ Object
Stores any part of the json that could not be interpreted. Usually signals an error if it isn’t empty.
48 49 50 |
# File 'lib/atdis/model.rb', line 48 def json_load_error @json_load_error end |
#url ⇒ Object
Returns the value of attribute url.
49 50 51 |
# File 'lib/atdis/model.rb', line 49 def url @url end |
Class Method Details
.attribute_keys ⇒ Object
153 154 155 |
# File 'lib/atdis/model.rb', line 153 def self.attribute_keys attribute_types.keys end |
.attribute_names ⇒ Object
Does what the equivalent on Activerecord does
158 159 160 |
# File 'lib/atdis/model.rb', line 158 def self.attribute_names attribute_types.keys.map{|k| k.to_s} end |
.cast(value, type) ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/atdis/model.rb', line 162 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
88 89 90 91 |
# File 'lib/atdis/model.rb', line 88 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]
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/atdis/model.rb', line 55 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
77 78 79 80 81 82 83 84 85 86 |
# File 'lib/atdis/model.rb', line 77 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
71 72 73 74 75 |
# File 'lib/atdis/model.rb', line 71 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_errors ⇒ Object
130 131 132 |
# File 'lib/atdis/model.rb', line 130 def json_errors json_errors_local + json_errors_in_children end |
#json_errors_in_children ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/atdis/model.rb', line 115 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_local ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/atdis/model.rb', line 99 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_empty ⇒ Object
139 140 141 142 143 144 |
# File 'lib/atdis/model.rb', line 139 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
93 94 95 96 97 |
# File 'lib/atdis/model.rb', line 93 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?
135 136 137 |
# File 'lib/atdis/model.rb', line 135 def used_attribute?(a) !attributes_before_type_cast[a].nil? end |