Module: Quiver::Mapper

Defined in:
lib/quiver/mapper.rb,
lib/quiver/mapper/hook.rb,
lib/quiver/mapper/soft_delete.rb,
lib/quiver/mapper/mapper_result.rb,
lib/quiver/mapper/not_found_error.rb,
lib/quiver/mapper/simple_query_builder.rb

Defined Under Namespace

Modules: ClassMethods, Hook, SoftDelete Classes: MapperResult, NotFoundError, SimpleQueryBuilder

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(host) ⇒ Object



3
4
5
# File 'lib/quiver/mapper.rb', line 3

def self.included(host)
  host.send(:extend, ClassMethods)
end

Instance Method Details

#allObject



121
122
123
# File 'lib/quiver/mapper.rb', line 121

def all
  query({})
end

#countObject



174
175
176
# File 'lib/quiver/mapper.rb', line 174

def count
  adapter.count
end

#filter(params) ⇒ Object



178
179
180
# File 'lib/quiver/mapper.rb', line 178

def filter(params)
  SimpleQueryBuilder.new(self).filter(params)
end

#find(pk) ⇒ Object



112
113
114
115
116
117
118
119
# File 'lib/quiver/mapper.rb', line 112

def find(pk)
  adapter.find(pk).when(
    success: -> (attributes, result) {
      Quiver::Mapper::MapperResult.new { |e| hydrate(attributes) }
    },
    failure: -> (errors, result) { result }
  )
end

#hard_delete(model) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/quiver/mapper.rb', line 125

def hard_delete(model)
  self.class.parents[-2]::Mappers.transaction do |transaction|
    attributes = dehydrate(model)

    Quiver::Mapper::MapperResult.new(nil, nil, {adapter_op: :hard_delete}) do |errors|
      adapter.hard_delete(attributes, transaction).when(
        success: -> (attributes, result) {
          {}
        },
        failure: -> (adapter_errors, result) { errors.add(adapter_errors); nil }
      )
    end
  end
end

#initialize(adapter_type = nil) ⇒ Object



72
73
74
75
76
# File 'lib/quiver/mapper.rb', line 72

def initialize(adapter_type=nil)
  adapter_type ||= self.class.parents[-2]::Application.default_adapter_type
  self.adapter_type = adapter_type
  self.adapter = self.class.send(:adapter_for, adapter_type).new
end

#paginate(params) ⇒ Object



186
187
188
# File 'lib/quiver/mapper.rb', line 186

def paginate(params)
  SimpleQueryBuilder.new(self).paginate(params)
end

#restore(model) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/quiver/mapper.rb', line 157

def restore(model)
  self.class.parents[-2]::Mappers.transaction do |transaction|
    model.deleted_at = nil

    attributes = dehydrate(model)

    Quiver::Mapper::MapperResult.new(nil, nil, {adapter_op: :restore}) do |errors|
      adapter.update(attributes, transaction).when(
        success: -> (attributes, result) {
          hydrate(attributes)
        },
        failure: -> (adapter_errors, result) { errors.add(adapter_errors); nil }
      )
    end
  end
end

#save(model) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/quiver/mapper.rb', line 78

def save(model)
  self.class.parents[-2]::Mappers.transaction do |transaction|
    hooks = self.class.send(:hooks) || []

    if self.class.type_attribute
      type_key = model.send(self.class.type_attribute)
    end

    if hooks.is_a?(Hash)
      hooks = hooks[type_key.to_sym] || []
    end

    hooks = hooks.map(&:new)

    model = hooks.inject(model) do |model, hook|
      hook.before_save(model)
    end

    if model.persisted_by.include?(adapter_type)
      result = update(model, transaction)
    else
      result = create(model, transaction)

      if result.success?
        # model.persisted_by!(adapter_type)
      end
    end

    hooks.inject(result) do |result, hook|
      hook.after_save(result)
    end
  end
end

#soft_delete(model) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/quiver/mapper.rb', line 140

def soft_delete(model)
  self.class.parents[-2]::Mappers.transaction do |transaction|
    model.deleted_at = Time.now

    attributes = dehydrate(model)

    Quiver::Mapper::MapperResult.new(nil, nil, {adapter_op: :soft_delete}) do |errors|
      adapter.update(attributes, transaction).when(
        success: -> (attributes, result) {
          hydrate(attributes)
        },
        failure: -> (adapter_errors, result) { errors.add(adapter_errors); nil }
      )
    end
  end
end

#sort(params) ⇒ Object



182
183
184
# File 'lib/quiver/mapper.rb', line 182

def sort(params)
  SimpleQueryBuilder.new(self).sort(params)
end