class AttrPermit

Constants

VERSION

Attributes

all_values_as_string[R]
big_decimal_as_string[R]
source[R]

Public Class Methods

attr_permit(*permissible_methods) click to toggle source
# File lib/attr_permit.rb, line 15
def attr_permit(*permissible_methods)
  self.permissible_methods.concat [*permissible_methods, *get_super_permissible_methods]
  self.permissible_methods.each do |meth|

    send(:define_method, meth) do
      call_method(meth)
    end

    attr_writer meth unless public_instance_methods.include?("#{meth}=")
  end
end
map_attribute(to, from) click to toggle source
# File lib/attr_permit.rb, line 33
def map_attribute(to, from)
  self.mapped_methods.concat [*to, *get_super_mapped_methods]
  if from.is_a? Proc
    send(:define_method, to) do
      source.instance_exec(&from)
    end
  else
    send(:define_method, to) do
      call_method(from)
    end
  end
end
map_attributes(map_hash) click to toggle source
# File lib/attr_permit.rb, line 27
def map_attributes(map_hash)
  map_hash.each do |to, from|
    map_attribute to, from
  end
end
mapped_methods() click to toggle source
# File lib/attr_permit.rb, line 11
def mapped_methods
  @mapped_methods ||= []
end
new(source=nil) click to toggle source
# File lib/attr_permit.rb, line 61
def initialize(source=nil)
  @source = source
  update(source)
end
permissible_methods() click to toggle source
# File lib/attr_permit.rb, line 7
def permissible_methods
  @permissible_methods ||= []
end

Protected Class Methods

get_super_mapped_methods() click to toggle source
# File lib/attr_permit.rb, line 52
def get_super_mapped_methods
  superclass.mapped_methods unless superclass == AttrPermit
end
get_super_permissible_methods() click to toggle source
# File lib/attr_permit.rb, line 48
def get_super_permissible_methods
  superclass.permissible_methods unless superclass == AttrPermit
end

Public Instance Methods

==(obj) click to toggle source
# File lib/attr_permit.rb, line 102
def ==(obj)
  self.hash == obj.hash
end
hash() click to toggle source
# File lib/attr_permit.rb, line 114
def hash
  self.to_hash.hash
end
is_equivalent?(obj) click to toggle source
# File lib/attr_permit.rb, line 106
def is_equivalent?(obj)
  self_hash = self.to_hash
  obj_hash = obj.to_hash
  self_hash.each { |k, v| self_hash[k] = v.to_s }
  obj_hash.each { |k, v| obj_hash[k] = v.to_s }
  self_hash == obj_hash
end
map_hash(big_decimal_as_string: false, all_values_as_string: false) click to toggle source
# File lib/attr_permit.rb, line 82
def map_hash(big_decimal_as_string: false, all_values_as_string: false)
  hasher([*self.class.mapped_methods], big_decimal_as_string, all_values_as_string)
end
non_nil_values(hash_type=:to_hash) click to toggle source
# File lib/attr_permit.rb, line 96
def non_nil_values(hash_type=:to_hash)
  hash = {}
  send(hash_type).each { |k, v| hash[k] = v unless v.nil? }
  hash
end
permit_hash(big_decimal_as_string: false, all_values_as_string: false) click to toggle source
# File lib/attr_permit.rb, line 86
def permit_hash(big_decimal_as_string: false, all_values_as_string: false)
  hasher([*self.class.permissible_methods], big_decimal_as_string, all_values_as_string)
end
to_enum() click to toggle source
# File lib/attr_permit.rb, line 118
def to_enum
  copy = self.dup
  copy.singleton_class.send(:include, Enumerable)

  def copy.each(&block)
    self.class.permissible_methods.each do |item|
      block.call(public_send(item))
    end
  end

  copy
end
to_h(big_decimal_as_string: false, all_values_as_string: false)
Alias for: to_hash
to_hash(big_decimal_as_string: false, all_values_as_string: false) click to toggle source
# File lib/attr_permit.rb, line 90
def to_hash(big_decimal_as_string: false, all_values_as_string: false)
  hasher([*self.class.permissible_methods, *self.class.mapped_methods], big_decimal_as_string, all_values_as_string)
end
Also aliased as: to_h
update(source) click to toggle source
# File lib/attr_permit.rb, line 66
def update(source)
  return if source.nil?
  source = OpenStruct.new(source) if source.class <= Hash
  self.class.permissible_methods.each do |meth|
    send("#{meth}=", source.send(meth)) if source.respond_to? meth
  end
end

Protected Instance Methods

call_method(meth) click to toggle source
# File lib/attr_permit.rb, line 74
def call_method(meth)
  callable = instance_variable_get("@#{meth}")
  instance_variable_set("@#{meth}", callable.call) if callable.respond_to?(:call)
  instance_variable_get("@#{meth}")
end

Private Instance Methods

all_values_as_string_convert(object) click to toggle source
# File lib/attr_permit.rb, line 143
def all_values_as_string_convert(object)
  val = if all_values_as_string
          object.to_s if object.respond_to?(:to_s)
        end
  return object if val.nil?
  val
end
array_to_hash(object) click to toggle source
# File lib/attr_permit.rb, line 162
def array_to_hash(object)
  if object.class <= Array
    value = object.map do |v|
      to_hash_object(v)
    end
  end
  return object if value.nil?
  value
end
big_decimal_as_string_convert(object) click to toggle source
# File lib/attr_permit.rb, line 135
def big_decimal_as_string_convert(object)
  val = if big_decimal_as_string
          object.to_s if object.class <= BigDecimal
        end
  return object if val.nil?
  val
end
hasher(methods, big_decimal_as_string, all_values_as_string) click to toggle source
# File lib/attr_permit.rb, line 172
def hasher(methods, big_decimal_as_string, all_values_as_string)
  @big_decimal_as_string = big_decimal_as_string
  @all_values_as_string = all_values_as_string
  hash = {}
  methods.each do |var|
    value = send(var)
    value = to_hash_object(value)
    value = big_decimal_as_string_convert(value)
    value = all_values_as_string_convert(value)
    value = array_to_hash(value)
    hash[var] = value
  end
  hash
end
to_hash_object(object) click to toggle source
# File lib/attr_permit.rb, line 151
def to_hash_object(object)
  if object.respond_to?(:to_hash) && object.class <= AttrPermit
    value = object.to_hash(big_decimal_as_string: big_decimal_as_string, all_values_as_string: all_values_as_string)
  end
  if object.respond_to?(:to_hash) && !(object.class <= AttrPermit)
    value = object.to_hash
  end
  return object if value.nil?
  value
end