Class: Xing::Mappers::Base

Inherits:
Object
  • Object
show all
Includes:
Services::Locator
Defined in:
lib/xing/mappers/base.rb

Defined Under Namespace

Classes: MissingLinkException

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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_dataObject

Returns the value of attribute error_data.



40
41
42
# File 'lib/xing/mappers/base.rb', line 40

def error_data
  @error_data
end

Returns the value of attribute links.



42
43
44
# File 'lib/xing/mappers/base.rb', line 42

def links
  @links
end

#locatorObject

Returns the value of attribute locator.



40
41
42
# File 'lib/xing/mappers/base.rb', line 40

def locator
  @locator
end

#locator_attribute_nameObject

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

#recordObject



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_nameObject



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_errorsObject



116
117
118
# File 'lib/xing/mappers/base.rb', line 116

def build_errors
  self.add_ar_errors(self.record)
end

#build_new_recordObject

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

#errorsObject



120
121
122
# File 'lib/xing/mappers/base.rb', line 120

def errors
  wrap_data(error_data)
end

#find_existing_recordObject

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_modelsObject

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_mappingObject



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

#saveObject

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_modelsObject



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


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