class Dish::Plate

Attributes

_original_hash[R]

Public Class Methods

coerce(key, klass_or_proc) click to toggle source
# File lib/dish/plate.rb, line 8
def coerce(key, klass_or_proc)
  coercions[key.to_s] = klass_or_proc
end
coercions() click to toggle source
# File lib/dish/plate.rb, line 4
def coercions
  @coercions ||= Hash.new(Plate)
end
new(hash) click to toggle source
# File lib/dish/plate.rb, line 13
def initialize(hash)
  @_original_hash = Hash[hash.map { |k, v| [k.to_s, v] }]
  @_value_cache = {}
end

Public Instance Methods

==(other) click to toggle source
# File lib/dish/plate.rb, line 22
def ==(other)
  return false unless other.respond_to?(:to_h)
  to_h == other.to_h
end
Also aliased as: eql?
as_hash() click to toggle source
# File lib/dish/plate.rb, line 49
def as_hash
  # TODO: Add the version number where this was deprecated?
  warn 'Dish::Plate#as_hash has been deprecated. Use Dish::Plate#to_h.'
  to_h
end
eql?(other)
Alias for: ==
hash() click to toggle source
# File lib/dish/plate.rb, line 18
def hash
  to_h.hash
end
method_missing(method, *args, &block) click to toggle source
# File lib/dish/plate.rb, line 29
def method_missing(method, *args, &block)
  method = method.to_s
  key = method[0..-2]
  if method.end_with?("?")
    !!_get_value(key)
  elsif method.end_with? '='
    _set_value(key, args.first)
  else
    _get_value(method)
  end
end
methods(regular = true) click to toggle source
Calls superclass method
# File lib/dish/plate.rb, line 67
def methods(regular = true)
  valid_keys = to_h.keys.map(&:to_sym)
  valid_keys + super
end
respond_to_missing?(method, *args) click to toggle source
Calls superclass method
# File lib/dish/plate.rb, line 41
def respond_to_missing?(method, *args)
  _check_for_presence(method.to_s) || super
end
to_h() click to toggle source
# File lib/dish/plate.rb, line 45
def to_h
  @_original_hash
end
to_json(*args) click to toggle source
# File lib/dish/plate.rb, line 55
def to_json(*args)
  # If we're using RubyMotion #to_json isn't available like with Ruby's JSON stdlib
  if defined?(Motion::Project::Config)
    # From BubbleWrap: https://github.com/rubymotion/BubbleWrap/blob/master/motion/core/json.rb#L30-L32
    NSJSONSerialization.dataWithJSONObject(to_h, options: 0, error: nil).to_str
  elsif defined?(JSON)
    to_h.to_json(*args)
  else
    raise "#{self.class}#to_json depends on Hash#to_json. Try again after using `require 'json'`."
  end
end

Private Instance Methods

_cache_key(value) click to toggle source
# File lib/dish/plate.rb, line 81
def _cache_key(value)
  [value.object_id, @_original_hash.hash].join('')
end
_check_for_presence(key) click to toggle source
# File lib/dish/plate.rb, line 89
def _check_for_presence(key)
  _original_hash.key?(key)
end
_convert_value(value, coercion) click to toggle source
# File lib/dish/plate.rb, line 93
def _convert_value(value, coercion)
  case value
  when Array then value.map { |v| _convert_value(v, coercion) }
  when Hash
    if coercion.is_a?(Proc)
      coercion.call(value)
    else
      coercion.new(value)
    end
  else
    if coercion.is_a?(Proc)
      coercion.call(value)
    else
      value
    end
  end
end
_get_value(key) click to toggle source
# File lib/dish/plate.rb, line 76
def _get_value(key)
  value = _original_hash[key]
  @_value_cache[_cache_key(value)] ||= _convert_value(value, self.class.coercions[key])
end
_set_value(key, value) click to toggle source
# File lib/dish/plate.rb, line 85
def _set_value(key, value)
  @_original_hash[key] = value
end