Module: Cistern::Attributes::InstanceMethods

Included in:
Collection, Model
Defined in:
lib/cistern/attributes.rb

Instance Method Summary collapse

Instance Method Details

#_dump(level) ⇒ Object



99
100
101
# File 'lib/cistern/attributes.rb', line 99

def _dump(level)
  Marshal.dump(attributes)
end

#attributesObject



103
104
105
# File 'lib/cistern/attributes.rb', line 103

def attributes
  @attributes ||= {}
end

#attributes=(attributes) ⇒ Object



107
108
109
# File 'lib/cistern/attributes.rb', line 107

def attributes=(attributes)
  @attributes = attributes
end

#dupObject



111
112
113
114
115
# File 'lib/cistern/attributes.rb', line 111

def dup
  copy = super
  copy.attributes= copy.attributes.dup
  copy
end

#identityObject



117
118
119
# File 'lib/cistern/attributes.rb', line 117

def identity
  send(self.class.instance_variable_get('@identity'))
end

#identity=(new_identity) ⇒ Object



121
122
123
# File 'lib/cistern/attributes.rb', line 121

def identity=(new_identity)
  send("#{self.class.instance_variable_get('@identity')}=", new_identity)
end

#merge_attributes(new_attributes = {}) ⇒ Object



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

def merge_attributes(new_attributes = {})
  for key, value in new_attributes
    unless self.class.ignored_attributes.include?(key)
      if aliased_key = self.class.aliases[key]
        send("#{aliased_key}=", value)
      elsif self.respond_to?("#{key}=", true)
        send("#{key}=", value)
      else
        # ignore data: unknown attribute : attributes[key] = value
      end
    end
  end
  self
end

#new_record?Boolean

Returns:

  • (Boolean)


140
141
142
# File 'lib/cistern/attributes.rb', line 140

def new_record?
  !identity
end

#requires(*args) ⇒ Object

check that the attributes specified in args exist and is not nil



145
146
147
148
149
150
151
152
# File 'lib/cistern/attributes.rb', line 145

def requires(*args)
  missing = missing_attributes(args)
  if missing.length == 1
    raise(ArgumentError, "#{missing.first} is required for this operation")
  elsif missing.any?
    raise(ArgumentError, "#{missing[0...-1].join(", ")} and #{missing[-1]} are required for this operation")
  end
end

#requires_one(*args) ⇒ Object



154
155
156
157
158
159
# File 'lib/cistern/attributes.rb', line 154

def requires_one(*args)
  missing = missing_attributes(args)
  if missing.length == args.length
    raise(ArgumentError, "#{missing[0...-1].join(", ")} or #{missing[-1]} are required for this operation")
  end
end