class Whiteprint::Attribute

Public Class Methods

new(persisted: nil, model: nil, **options) click to toggle source
# File lib/whiteprint/attributes.rb, line 3
def initialize(persisted: nil, model: nil, **options)
  @model     = model
  @options   = options
  @persisted = persisted
end

Public Instance Methods

==(other) click to toggle source
# File lib/whiteprint/attributes.rb, line 9
def ==(other)
  to_h == other.to_h
end
[](name) click to toggle source
# File lib/whiteprint/attributes.rb, line 47
def [](name)
  @options[name.to_sym]
end
for_meta(instance) click to toggle source
# File lib/whiteprint/attributes.rb, line 36
def for_meta(instance)
  ::Whiteprint.config.meta_attribute_options.map do |option|
    {option => send("meta_#{option}", instance)}
  end.inject(&:merge).select { |_, value| !value.nil? }
end
for_persisted(**config) click to toggle source
# File lib/whiteprint/attributes.rb, line 42
def for_persisted(**config)
  return merge(config) if @persisted
  self.class.new(persisted: true, name: @options[:name], type: @options[:type], options: persisted_options, **config)
end
has?(*keys, **conditions) click to toggle source
# File lib/whiteprint/attributes.rb, line 13
def has?(*keys, **conditions)
  keys.none? do |key|
    @options.values_at(*key).compact.empty?
  end && conditions.all? do |key, value|
    [*@options[key]] & [*value] != []
  end
end
merge(options) click to toggle source
# File lib/whiteprint/attributes.rb, line 32
def merge(options)
  self.class.new(persisted: @persisted, **@options, **options)
end
meta_enum(instance) click to toggle source
# File lib/whiteprint/attributes.rb, line 51
def meta_enum(instance)
  _enum = if enum.is_a?(Symbol)
            instance.send(enum)
          else
            enum
          end

  return nil unless _enum
  return _enum if _enum.is_a?(Hash)

  _enum.map do |value|
    {value => value}
  end.inject(&:merge)
end
method_missing(name, *args) click to toggle source
# File lib/whiteprint/attributes.rb, line 66
def method_missing(name, *args)
  if name.to_s.starts_with?('meta_')
    self[name.to_s.remove(/^meta_/)]
  else
    self[name]
  end
end
persisted_options() click to toggle source
# File lib/whiteprint/attributes.rb, line 21
def persisted_options
  @options.select do |key, value|
    Whiteprint.config.persisted_attribute_options.keys.include?(key) &&
    !(key == :default && value.is_a?(Symbol))
  end
end
to_h() click to toggle source
# File lib/whiteprint/attributes.rb, line 28
def to_h
  @options
end