class Whiteprint::Attributes

Public Class Methods

new(attributes = nil, model: nil) click to toggle source
# File lib/whiteprint/attributes.rb, line 107
def initialize(attributes = nil, model: nil)
  attributes  = Hash[attributes] if attributes.is_a?(Array)
  @attributes = (attributes || {}).dup
  @model      = model
end

Public Instance Methods

[](name) click to toggle source
# File lib/whiteprint/attributes.rb, line 203
def [](name)
  return unless name
  @attributes[name.to_sym]
end
add(name:, type:, **options) click to toggle source
# File lib/whiteprint/attributes.rb, line 113
def add(name:, type:, **options)
  @attributes[name.to_sym] = Attribute.new(name: name.to_sym, type: type.to_sym, model: @model, **options)
end
as_json(*args) click to toggle source
Calls superclass method
# File lib/whiteprint/attributes.rb, line 117
def as_json(*args)
  super['attributes']
end
diff(diff, type: nil) click to toggle source
# File lib/whiteprint/attributes.rb, line 121
def diff(diff, type: nil)
  # TODO: Clean up
  added   = diff.slice(*(diff.keys - keys))
  changed = diff.to_diff_a(type) - to_diff_a(type) - added.to_diff_a(type)
  changed = Attributes.new(Hash[changed].map { |key, options| { key => Attribute.new(persisted: true, model: @model, **options) } }.inject(&:merge), model: @model)
  removed = slice(*(keys - diff.keys))

  { added: added, changed: changed, removed: removed }
end
for_meta(instance = nil) click to toggle source
# File lib/whiteprint/attributes.rb, line 163
def for_meta(instance = nil)
  where(::Whiteprint.config.meta_attribute_options).to_h.map do |key, attribute|
    {key => attribute.for_meta(instance)}
  end.inject(&:merge)
end
for_permitted() click to toggle source
# File lib/whiteprint/attributes.rb, line 175
def for_permitted
  # TODO: move specifics to activerecord adapater

  self.not(readonly: true).not(private: true).not(name: [:updated_at, :created_at]).to_h.map do |name, attribute|
    if attribute.array
      {name => []}
    elsif attribute.type == :has_and_belongs_to_many
      {:"#{name.to_s.singularize}_ids" => []}
    elsif attribute.type == :references
      :"#{attribute.name}_id"
    else
      name
    end
  end
end
for_permitted_json() click to toggle source
# File lib/whiteprint/attributes.rb, line 191
def for_permitted_json
  self.not(readonly: true).where(type: [:json, :jsonb]).keys
end
for_persisted() click to toggle source
# File lib/whiteprint/attributes.rb, line 155
def for_persisted
  persisted_scope = self.not(virtual: true)
  persisted_scope.to_h.each do |name, attribute|
    persisted_scope.to_h[name] = attribute.for_persisted
  end
  persisted_scope
end
for_serializer() click to toggle source
# File lib/whiteprint/attributes.rb, line 169
def for_serializer
  # TODO: move specifics to activerecord adapater

  self.not(type: :references).not(type: :has_and_belongs_to_many).not(private: true).keys
end
keys() click to toggle source
# File lib/whiteprint/attributes.rb, line 139
def keys
  @attributes.keys
end
method_missing(name) click to toggle source
# File lib/whiteprint/attributes.rb, line 208
def method_missing(name)
  self[name]
end
not(*keys, **conditions) click to toggle source
# File lib/whiteprint/attributes.rb, line 135
def not(*keys, **conditions)
  AttributeScope.new(@attributes, model: @model).not(*keys, **conditions)
end
slice(*keys) click to toggle source
# File lib/whiteprint/attributes.rb, line 143
def slice(*keys)
  where(name: keys)
end
to_a() click to toggle source
# File lib/whiteprint/attributes.rb, line 151
def to_a
  @attributes.values
end
to_diff_a(type) click to toggle source
# File lib/whiteprint/attributes.rb, line 195
def to_diff_a(type)
  if type
    to_h.map { |name, attr| [name, attr.send("for_#{type}").to_h] }
  else
    to_h.map { |name, attr| [name, attr.to_h] }
  end
end
to_h() click to toggle source
# File lib/whiteprint/attributes.rb, line 147
def to_h
  @attributes
end
where(*keys, **conditions) click to toggle source
# File lib/whiteprint/attributes.rb, line 131
def where(*keys, **conditions)
  AttributeScope.new(@attributes, model: @model).where(*keys, **conditions)
end