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.



107
108
109
110
111
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name) ⇒ Object



208
209
210
# File 'lib/whiteprint/attributes.rb', line 208

def method_missing(name)
  self[name]
end

Instance Method Details

#[](name) ⇒ Object



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

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

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



113
114
115
# 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) ⇒ Object



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

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

#diff(diff, type: nil) ⇒ Object



121
122
123
124
125
126
127
128
129
# 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) ⇒ Object



163
164
165
166
167
# 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_permittedObject



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# 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_jsonObject



191
192
193
# File 'lib/whiteprint/attributes.rb', line 191

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

#for_persistedObject



155
156
157
158
159
160
161
# 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_serializerObject



169
170
171
172
173
# 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

#keysObject



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

def keys
  @attributes.keys
end

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



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

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

#slice(*keys) ⇒ Object



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

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

#to_aObject



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

def to_a
  @attributes.values
end

#to_diff_a(type) ⇒ Object



195
196
197
198
199
200
201
# 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_hObject



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

def to_h
  @attributes
end

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



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

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