class JSONAPI::Item

Models a Item's key -> value relationship

Attributes

item[RW]

@return the value of an Item

Public Class Methods

new(obj) click to toggle source

Able to take a hash and dynamically create instance variables using the hash keys

Ex: obj == { :name => 'fields', :value => {'articles' => 'title,body,author', 'people' => 'name' }}

@param obj [Object] Can be anything, but if a hash is provided, dynamic instance variable can be created

upon trying to access them.
# File lib/easy/jsonapi/item.rb, line 15
def initialize(obj)
  if obj.is_a? Hash
    ensure_keys_are_sym(obj)
  end
  @item = obj
end

Public Instance Methods

to_h() click to toggle source

Represent item as a hash

# File lib/easy/jsonapi/item.rb, line 39
def to_h
  @item.to_h
end
to_s() click to toggle source

A special to_string method if @item is a hash.

# File lib/easy/jsonapi/item.rb, line 23
def to_s
  return @item.to_s unless @item.is_a? Hash
  tr = '{ '
  first = true
  @item.each do |k, v|
    if first
      first = false
      tr += "\"#{k}\": \"#{v}\", "
    else
      tr += "\"#{k}\": \"#{v}\""
    end
  end
  tr += ' }'
end

Private Instance Methods

ensure_keys_are_sym(obj) click to toggle source

Ensures that hash keys are symbol (and not String) when passing a hash to item. @param obj [Object] A hash that can represent an item.

# File lib/easy/jsonapi/item.rb, line 67
def ensure_keys_are_sym(obj)
  obj.each_key do |k|
    raise "All keys must be Symbols. '#{k}' was #{k.class}" unless k.is_a? Symbol
  end
end
method_missing(method_name, *args, &block) click to toggle source

Only used if implementing Item directly.

dynamically creates accessor methods for instance variables
created in the initialize
Calls superclass method
# File lib/easy/jsonapi/item.rb, line 48
def method_missing(method_name, *args, &block)
  return super unless is_a? JSONAPI::Item
  return super unless @item.is_a? Hash
  if should_update_var?(method_name)
    @item[method_name[0..-2].to_sym] = args[0]
  elsif should_get_var?(method_name)
    @item[method_name]
  else
    super
  end
end
respond_to_missing?(method_name, *args) click to toggle source

Needed when using method_missing

Calls superclass method
# File lib/easy/jsonapi/item.rb, line 61
def respond_to_missing?(method_name, *args)
  instance_variables.include?("@#{method_name}".to_sym) || super
end
should_get_var?(method_name) click to toggle source

Checks to see if the method has the same name as an existing instance

variable

@param (see method_missing)

# File lib/easy/jsonapi/item.rb, line 84
def should_get_var?(method_name)
  @item[method_name].nil? == false
end
should_update_var?(method_name) click to toggle source

Checks to see if the method name has a '=' at the end and if the

prefix before the '=' has the same name as an existing instance
variable.

@param (see method_missing)

# File lib/easy/jsonapi/item.rb, line 77
def should_update_var?(method_name)
  method_name.to_s[-1] == '=' && @item[method_name[0..-2].to_sym].nil? == false
end