Code Monkey home page Code Monkey logo

rdmapper's Introduction

Rdmapper Build Status

Rdmapper is a library for implementing the Mapper in the DataMapper pattern on Ruby

Usage

Let's say you have a User object:

  class User
    attr_reader :id, :first_name, :last_name

    def initialize(first_name:, last_name:)
      @first_name = first_name
      @last_name  = last_name
    end

    def change_first_name(first_name)
      @first_name = first_name
    end
  end

Create mapper class and declare two build_object and build_hash methods:

class UserMapper
  include Rdmapper::Mapper

  def build_object(hash)
    User.allocate.tap do |p|
      p.instance_variable_set(:@id, hash[:id])
      p.instance_variable_set(:@first_name, hash[:first_name])
      p.instance_variable_set(:@last_name, hash[:last_name])
    end
  end

  def build_hash(object)
    { first_name: object.first_name,
      last_name: object.last_name }
  end
end

Now use this mapper in your Repository class to map entity to hash and vise versa:

  class UsersRepository
    def initialize(db = DB, mapper = UserMapper.new)
      @mapper = mapper
      @db = db
    end

    def put(user)
      hash = @mapper.to_hash(user)
      id = @db[:users].insert(hash)
      user.instance_variable_set(:@id, id)
      user
    end

    def find(id)
      hash = @db[:users].where(id: id).first
      return nil unless hash
      @mapper.to_object hash
    end

    def update(user)
      hash = @mapper.to_hash(user)
      @db[:users].where(id: user.id).update(hash)
      user
    end

    def delete(user)
      @db[:users].where(id: user.id).delete
    end
  end

Note that DB is an Sequel object which provides convenient interface for accessing the database. For full example see repository_example_spec.rb

Author

Albert Gazizov, @deeper4k

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.