Module: WingedCouch::Models::API

Included in:
WingedCouch::Model
Defined in:
lib/winged_couch/models/api.rb

Overview

Module with sytax sugar methods

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object


8
9
10
# File 'lib/winged_couch/models/api.rb', line 8

def self.included(klass)
  klass.extend(ClassMethods)
end

Instance Method Details

#==(other) ⇒ Object


90
91
92
# File 'lib/winged_couch/models/api.rb', line 90

def ==(other)
  other.is_a?(self.class) && other._id == self._id
end

#initialize(attrs = nil) ⇒ Object

Constructor

Parameters:

  • attrs (Hash) (defaults to: nil)

    hash of attributes


70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/winged_couch/models/api.rb', line 70

def initialize(attrs = nil)
  attrs ||= {}
  attrs.each do |attribute, value|
    if attribute.to_s == "id"
      send("_id=", value)
    elsif attribute.to_s == "rev"
      send("_rev=", value)
    else
      send("#{attribute}=", value)
    end
  end
  run_hooks(:after, :initialize)
end

#inspectObject Also known as: to_s, to_str


85
86
87
# File 'lib/winged_couch/models/api.rb', line 85

def inspect
  "#<#{self.class.name} " + attributes.merge(_id: _id, _rev: _rev).map { |k, v| "#{k}: #{v.inspect}" }.join(", ") + ">"
end

#with(data = {}) ⇒ Object

Takes a hash of attributes to be assigned to copy of a current record

Parameters:

  • data (Hash) (defaults to: {})

Returns:

  • WingedCouch::Model copy of current document with assigned attributes


116
117
118
# File 'lib/winged_couch/models/api.rb', line 116

def with(data = {})
  dup.with!(data)
end

#with!(data = {}) ⇒ Object

Takes a hash of new attributes to be assigned to a record

Parameters:

  • data (Hash) (defaults to: {})

    hash of attributes

Returns:

  • self


103
104
105
106
107
108
# File 'lib/winged_couch/models/api.rb', line 103

def with!(data = {})
  data.each do |attr_name, attr_value|
    public_send("#{attr_name}=", attr_value)
  end
  self
end