Class: Xing::Mappers::Base
- Inherits:
-
Object
- Object
- Xing::Mappers::Base
- Includes:
- Services::Locator
- Defined in:
- lib/xing/mappers/base.rb
Defined Under Namespace
Classes: MissingLinkException
Instance Attribute Summary collapse
-
#error_data ⇒ Object
Returns the value of attribute error_data.
-
#links ⇒ Object
readonly
Returns the value of attribute links.
-
#locator ⇒ Object
Returns the value of attribute locator.
-
#locator_attribute_name ⇒ Object
Returns the value of attribute locator_attribute_name.
- #record ⇒ Object
Class Method Summary collapse
Instance Method Summary collapse
- #add_ar_errors(object) ⇒ Object
- #assign_values(data_hash) ⇒ Object
- #build_errors ⇒ Object
-
#build_new_record ⇒ Object
Default for building a new record - override this or define #record_class (e.g. ‘return Page`.
- #errors ⇒ Object
-
#find_existing_record ⇒ Object
Default for finding an existing record - override this or define #record_class (e.g. ‘return Page`.
-
#initialize(json, locator = nil) ⇒ Base
constructor
When updating records, pass the locator (e.g. DB id, url_slug, or other unique resource extracted from the resource path) as the second argument.
-
#map_nested_models ⇒ Object
Do nothing if there are no nested models Override this method in subclass if necessary.
- #perform_mapping ⇒ Object
-
#save ⇒ Object
Default save - subclasses might override.
- #save_nested_models ⇒ Object
- #unwrap_data(hash) ⇒ Object
- #unwrap_links(hash) ⇒ Object
- #wrap_data(hash) ⇒ Object
Methods included from Services::Locator
#normalize_path, #route_to, #router
Constructor Details
#initialize(json, locator = nil) ⇒ Base
When updating records, pass the locator (e.g. DB id, url_slug, or other unique resource extracted from the resource path) as the second argument.
31 32 33 34 35 36 37 38 39 |
# File 'lib/xing/mappers/base.rb', line 31 def initialize(json, locator = nil) @source_json = json if @source_json.is_a? String @source_hash = JSON.parse(json).with_indifferent_access else @source_hash = @source_json end @locator = locator end |
Instance Attribute Details
#error_data ⇒ Object
Returns the value of attribute error_data.
40 41 42 |
# File 'lib/xing/mappers/base.rb', line 40 def error_data @error_data end |
#links ⇒ Object (readonly)
Returns the value of attribute links.
42 43 44 |
# File 'lib/xing/mappers/base.rb', line 42 def links @links end |
#locator ⇒ Object
Returns the value of attribute locator.
40 41 42 |
# File 'lib/xing/mappers/base.rb', line 40 def locator @locator end |
#locator_attribute_name ⇒ Object
Returns the value of attribute locator_attribute_name.
40 41 42 |
# File 'lib/xing/mappers/base.rb', line 40 def locator_attribute_name @locator_attribute_name end |
#record ⇒ Object
94 95 96 97 98 99 100 |
# File 'lib/xing/mappers/base.rb', line 94 def record @record ||= if !locator.nil? find_existing_record else build_new_record end end |
Class Method Details
.locator_attribute_name ⇒ Object
44 45 46 |
# File 'lib/xing/mappers/base.rb', line 44 def self.locator_attribute_name :id end |
Instance Method Details
#add_ar_errors(object) ⇒ Object
124 125 126 127 |
# File 'lib/xing/mappers/base.rb', line 124 def add_ar_errors(object) object_errors = Xing::Services::ErrorConverter.new(object).convert error_data.deep_merge!(object_errors) end |
#assign_values(data_hash) ⇒ Object
102 103 104 105 106 |
# File 'lib/xing/mappers/base.rb', line 102 def assign_values(data_hash) # Override in subclasses to assign needed values here record # force loading or creation of the underlying DB record update_record end |
#build_errors ⇒ Object
116 117 118 |
# File 'lib/xing/mappers/base.rb', line 116 def build_errors self.add_ar_errors(self.record) end |
#build_new_record ⇒ Object
Default for building a new record - override this or define #record_class (e.g. ‘return Page`
65 66 67 |
# File 'lib/xing/mappers/base.rb', line 65 def build_new_record @record = record_class.new end |
#errors ⇒ Object
120 121 122 |
# File 'lib/xing/mappers/base.rb', line 120 def errors wrap_data(error_data) end |
#find_existing_record ⇒ Object
Default for finding an existing record - override this or define #record_class (e.g. ‘return Page`
59 60 61 |
# File 'lib/xing/mappers/base.rb', line 59 def find_existing_record @record = record_class.find(@locator) end |
#map_nested_models ⇒ Object
Do nothing if there are no nested models Override this method in subclass if necessary
110 111 |
# File 'lib/xing/mappers/base.rb', line 110 def map_nested_models end |
#perform_mapping ⇒ Object
69 70 71 72 73 74 75 76 77 |
# File 'lib/xing/mappers/base.rb', line 69 def perform_mapping data = unwrap_data(@source_hash) @links = unwrap_links(@source_hash) self.error_data = Hash.new { |hash, key| hash[key] = {} } assign_values(data) map_nested_models build_errors end |
#save ⇒ Object
Default save - subclasses might override
49 50 51 52 53 54 55 |
# File 'lib/xing/mappers/base.rb', line 49 def save perform_mapping unless self.errors[:data].present? save_nested_models self.record.save end end |
#save_nested_models ⇒ Object
113 114 |
# File 'lib/xing/mappers/base.rb', line 113 def save_nested_models end |
#unwrap_data(hash) ⇒ Object
83 84 85 86 |
# File 'lib/xing/mappers/base.rb', line 83 def unwrap_data(hash) return hash['data'] if hash['data'].is_a?(Array) hash['data'].with_indifferent_access end |
#unwrap_links(hash) ⇒ Object
79 80 81 |
# File 'lib/xing/mappers/base.rb', line 79 def unwrap_links(hash) hash['links'].with_indifferent_access if hash['links'] end |
#wrap_data(hash) ⇒ Object
88 89 90 91 92 |
# File 'lib/xing/mappers/base.rb', line 88 def wrap_data(hash) { data: hash } end |