Class: Whiteprint::Attributes

Inherits:
Object
  • Object
show all
Defined in:
lib/whiteprint/attributes.rb

Instance Method Summary collapse

Constructor Details

#initialize(attributes = nil, model: nil) ⇒ Attributes

Returns a new instance of Attributes.



106
107
108
109
110
# File 'lib/whiteprint/attributes.rb', line 106

def initialize(attributes = nil, model: nil)
  attributes  = Hash[attributes] if attributes.is_a?(Array)
  @attributes = (attributes || {}).dup
  @model      = model
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name) ⇒ Object



203
204
205
# File 'lib/whiteprint/attributes.rb', line 203

def method_missing(name)
  self[name]
end

Instance Method Details

#[](name) ⇒ Object



198
199
200
201
# File 'lib/whiteprint/attributes.rb', line 198

def [](name)
  return unless name
  @attributes[name.to_sym]
end

#add(name:, type:, **options) ⇒ Object



112
113
114
# File 'lib/whiteprint/attributes.rb', line 112

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) ⇒ Object



116
117
118
# File 'lib/whiteprint/attributes.rb', line 116

def as_json(*args)
  super['attributes']
end

#diff(diff, type: nil) ⇒ Object



120
121
122
123
124
125
126
127
128
# File 'lib/whiteprint/attributes.rb', line 120

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) ⇒ Object



162
163
164
165
166
# File 'lib/whiteprint/attributes.rb', line 162

def for_meta(instance)
  where(::Whiteprint.config.meta_attribute_options).to_h.map do |key, attribute|
    {key => attribute.for_meta(instance)}
  end.inject(&:merge)
end

#for_permittedObject



172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/whiteprint/attributes.rb', line 172

def for_permitted
  self.not(readonly: 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_jsonObject



186
187
188
# File 'lib/whiteprint/attributes.rb', line 186

def for_permitted_json
  self.not(readonly: true).where(type: [:json, :jsonb]).keys
end

#for_persistedObject



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

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_serializerObject



168
169
170
# File 'lib/whiteprint/attributes.rb', line 168

def for_serializer
  self.not(type: :references).not(private: true).keys
end

#keysObject



138
139
140
# File 'lib/whiteprint/attributes.rb', line 138

def keys
  @attributes.keys
end

#not(*keys, **conditions) ⇒ Object



134
135
136
# File 'lib/whiteprint/attributes.rb', line 134

def not(*keys, **conditions)
  AttributeScope.new(@attributes, model: @model).not(*keys, **conditions)
end

#slice(*keys) ⇒ Object



142
143
144
# File 'lib/whiteprint/attributes.rb', line 142

def slice(*keys)
  where(name: keys)
end

#to_aObject



150
151
152
# File 'lib/whiteprint/attributes.rb', line 150

def to_a
  @attributes.values
end

#to_diff_a(type) ⇒ Object



190
191
192
193
194
195
196
# File 'lib/whiteprint/attributes.rb', line 190

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_hObject



146
147
148
# File 'lib/whiteprint/attributes.rb', line 146

def to_h
  @attributes
end

#where(*keys, **conditions) ⇒ Object



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

def where(*keys, **conditions)
  AttributeScope.new(@attributes, model: @model).where(*keys, **conditions)
end