Class: Lurch::Changeset

Inherits:
Object
  • Object
show all
Defined in:
lib/lurch/changeset.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, attributes = {}) ⇒ Changeset

Returns a new instance of Changeset.



6
7
8
9
10
11
12
# File 'lib/lurch/changeset.rb', line 6

def initialize(type, attributes = {})
  is_resource = type.is_a?(Resource)
  @type = is_resource ? type.type : Inflector.decode_type(type)
  @id = is_resource ? type.id : nil
  @attributes = attributes
  @relationships = {}
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



4
5
6
# File 'lib/lurch/changeset.rb', line 4

def attributes
  @attributes
end

#errorsObject

Returns the value of attribute errors.



4
5
6
# File 'lib/lurch/changeset.rb', line 4

def errors
  @errors
end

#idObject (readonly)

Returns the value of attribute id.



3
4
5
# File 'lib/lurch/changeset.rb', line 3

def id
  @id
end

#relationshipsObject

Returns the value of attribute relationships.



4
5
6
# File 'lib/lurch/changeset.rb', line 4

def relationships
  @relationships
end

#typeObject (readonly)

Returns the value of attribute type.



3
4
5
# File 'lib/lurch/changeset.rb', line 3

def type
  @type
end

Instance Method Details

#inspectObject



24
25
26
27
28
29
30
# File 'lib/lurch/changeset.rb', line 24

def inspect
  attrs = ["id: #{id.inspect}"]
  attrs = attrs.concat(attributes.map { |name, value| "#{name}: #{value.inspect}" })
  attrs = attrs.concat(relationships.map { |name, value| "#{name}: #{value.inspect}" })
  inspection = attrs.join(", ")
  "#<#{self.class}[#{Inflector.classify(type)}] #{inspection}>"
end

#set(key, value) ⇒ Object



14
15
16
17
# File 'lib/lurch/changeset.rb', line 14

def set(key, value)
  attributes[key.to_sym] = value
  self
end


19
20
21
22
# File 'lib/lurch/changeset.rb', line 19

def set_related(relationship_key, related_resources)
  @relationships[relationship_key.to_sym] = related_resources
  self
end