Class: ActiveModel::AttributeSet

Inherits:
Hash
  • Object
show all
Includes:
ActiveModel::AttributeFilters::AttributeSetMethods
Defined in:
lib/attribute-filters/attribute_set.rb,
lib/attribute-filters/attribute_set_enum.rb,
lib/attribute-filters/attribute_set_query.rb,
lib/attribute-filters/attribute_set_attrquery.rb,
lib/attribute-filters/attribute_set_annotations.rb

Overview

module AttributeFilters

Defined Under Namespace

Modules: Enumerable Classes: AttrQuery, Enumerator, Query

Constant Summary

Constants included from ActiveModel::AttributeFilters::AttributeSetMethods

ActiveModel::AttributeFilters::AttributeSetMethods::AFHelpers

Instance Method Summary collapse

Methods included from ActiveModel::AttributeFilters::AttributeSetMethods

#&, #+, #-, #^, #add, #to_a, #to_attribute_set, #to_hash, #to_set

Methods included from Enumerable

#each_name_value, #select_accessible

Constructor Details

#initialize(*args) ⇒ AttributeSet

Creates a new instance of attribute set.



186
187
188
189
190
191
# File 'lib/attribute-filters/attribute_set.rb', line 186

def initialize(*args)
  return super if args.count == 0
  r = super()
  add(args)
  r
end

Instance Method Details

#annotate(atr_name, name, value) ⇒ nil Also known as: add_op, bind_op

Adds an annotation to the given attribute.

Parameters:

  • atr_name (String, Symbol)

    attribute name

  • name (String, Symbol)

    annotation key

  • value (Object)

    annotation value

Returns:

  • (nil)

Raises:

  • (ArgumentError)

    when the given attribute name does not exist in a set



183
184
185
186
187
188
189
190
191
# File 'lib/attribute-filters/attribute_set_annotations.rb', line 183

def annotate(atr_name, name, value)
  atr_name = atr_name.to_s unless atr_name.blank?
  unless key?(atr_name)
    raise ArgumentError, "attribute '#{atr_name}' must exist in order to annotate it"
  end
  self[atr_name] = Hash.new unless self[atr_name].is_a?(Hash)
  self[atr_name][name.to_sym] = value
  nil
end

#annotation(atr_name, *annotation_names) ⇒ Object, ... Also known as: get_annotation, get_annotations

Gets an annotation for the specified attribute. If the second argument is ommited, it returns all annotations for the specified attribute.

Parameters:

  • atr_name (String, Symbol)

    attribute name

  • annotation_names (Array<String,Symbol>)

    optional annotation key(s)

Returns:

  • (Object, Hash, nil)

    duplicate of annotations hash, value of a single annotation or nil if not found, or array of values (filled with nil objects if not found)



231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/attribute-filters/attribute_set_annotations.rb', line 231

def annotation(atr_name, *annotation_names)
  atr_name.present? or return nil
  has_annotations? or return nil
  an_group = self[atr_name.to_s]
  return nil if an_group.nil? || !an_group.is_a?(Hash)
  case annotation_names.size
  when 0
    r = Hash.new
    an_group.each_pair { |k, v| r[k] = AFHelpers.safe_dup(v) }
    r
  when 1
    AFHelpers.safe_dup(an_group[annotation_names.first.to_sym])
  else
    annotation_names.map { |a| AFHelpers.safe_dup(an_group[a.to_sym]) }
  end
end

#annotationsHash{String => Hash}

Returns all annotations that are set.

Returns:

  • (Hash{String => Hash})

    annotations indexed by attribute names



285
286
287
288
289
# File 'lib/attribute-filters/attribute_set_annotations.rb', line 285

def annotations
  Hash.new.tap do |r|
    each_pair { |atr_name, an| r[atr_name] = an.deep_dup if an.is_a?(Hash) }
  end
end

#delete_annotation(atr_name, annotation = nil) ⇒ Hash, ... Also known as: delete_annotations

Deletes annotations or single annotation key for the given attribute. If the annotation argument is not given or is nil then all annotation keys for the given attribute name are deleted.

Parameters:

  • atr_name (String, Symbol)

    attribute name

  • annotation (String, Symbol) (defaults to: nil)

    annotation key

Returns:

  • (Hash, Object, nil)

    deleted annotations (hash), deleted annotation value or nil if there wasn’t anything to delete



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/attribute-filters/attribute_set_annotations.rb', line 258

def delete_annotation(atr_name, annotation = nil)
  return nil if atr_name.blank?
  atr_name = atr_name.to_s
  return nil unless key?(atr_name)
  ag = self[atr_name]
  return nil if ag === true
  if annotation.nil? || !ag.is_a?(Hash)
    self[atr_name] = true
    return ag
  else
    r = ag.delete(annotation.to_sym)
    self[atr_name] = true if ag.empty?
    r
  end
end

#has_annotation?Boolean #has_annotation?(attribute_name) ⇒ Boolean #has_annotation?(attribute_name, *annotation_keys) ⇒ Boolean Also known as: has_annotations?

Tests if an annotation of the given name exists in a set or if set has annotations.

Overloads:

  • #has_annotation?Boolean

    Tests if set has any annotations.

    Returns:

    • (Boolean)

      true if the current set has any annotations, false otherwise

  • #has_annotation?(attribute_name) ⇒ Boolean

    Tests if any annotation key for the attribute of the given name exists in a set.

    Parameters:

    • attribute_name (Symbol, String)

      name of an attribute

    Returns:

    • (Boolean)

      true if the current set has any annotations for attribute_name, false otherwise

  • #has_annotation?(attribute_name, *annotation_keys) ⇒ Boolean

    Tests if any of the annotation keys for the attribute of the given name exists in a set.

    Parameters:

    • attribute_name (Symbol, String)

      name of an attribute

    • annotation_keys (Array<String,Symbol>)

      annotation key names to check

    Returns:

    • (Boolean)

      true if the current set has at least one of the given annotation_keys for attribute_name, false otherwise



212
213
214
215
216
217
218
219
220
# File 'lib/attribute-filters/attribute_set_annotations.rb', line 212

def has_annotation?(*args)
  return false if empty?
  return true if args.size == 0
  atr_name = args.shift.to_s
  a_group = self[atr_name]
  return false if a_group.blank? || !a_group.is_a?(Hash)
  args.empty? and return true
  args.any? { |a_name| a_group.key?(a_name.to_sym) }
end

#remove_annotationsnil

Removes all annotations.

Returns:

  • (nil)


277
278
279
280
# File 'lib/attribute-filters/attribute_set_annotations.rb', line 277

def remove_annotations
  each_pair { |k, v| self[k] = true }
  nil
end