Module: Cistern::Attributes::InstanceMethods

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

Instance Method Summary collapse

Instance Method Details

#_dump(level) ⇒ Object



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

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

#attributesObject



126
127
128
# File 'lib/cistern/attributes.rb', line 126

def attributes
  @attributes ||= {}
end

#attributes=(attributes) ⇒ Object



130
131
132
# File 'lib/cistern/attributes.rb', line 130

def attributes=(attributes)
  @attributes = attributes
end

#dupObject



134
135
136
137
138
# File 'lib/cistern/attributes.rb', line 134

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

#identityObject



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

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

#identity=(new_identity) ⇒ Object



144
145
146
# File 'lib/cistern/attributes.rb', line 144

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

#merge_attributes(new_attributes = {}) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/cistern/attributes.rb', line 148

def merge_attributes(new_attributes = {})
  new_attributes.each do |key, value|
    # find nested paths
    value.is_a?(::Hash) && self.class.attributes.each do |name, options|
      if (options[:squash] || []).first == key
        send("#{name}=", {key => value})
      end
    end
    unless self.class.ignored_attributes.include?(key)
      if self.class.aliases.has_key?(key)
        self.class.aliases[key].each do |aliased_key|
          send("#{aliased_key}=", value)
        end
      end

      if self.respond_to?("#{key}=", true)
        send("#{key}=", value)
      end
    end
  end
  self
end

#new_record?Boolean

Returns:

  • (Boolean)


171
172
173
# File 'lib/cistern/attributes.rb', line 171

def new_record?
  !identity
end

#requires(*args) ⇒ Object

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



176
177
178
179
180
181
182
183
# File 'lib/cistern/attributes.rb', line 176

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



185
186
187
188
189
190
# File 'lib/cistern/attributes.rb', line 185

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