class MdNotes::BaseModel

Base model.

Public Instance Methods

method_missing(method_sym, *arguments, &block) click to toggle source

Use to allow additional model properties.

Calls superclass method
# File lib/md_notes/models/base_model.rb, line 37
def method_missing(method_sym, *arguments, &block)
  method = method_sym.to_s
  if method.end_with? '='
    instance_variable_set(format('@%s', [method.chomp('=')]),
                          arguments.first)
  elsif instance_variable_defined?("@#{method}") && arguments.empty?
    instance_variable_get("@#{method}")
  else
    super
  end
end
respond_to_missing?(method_sym, include_private = false) click to toggle source

Override for additional model properties.

Calls superclass method
# File lib/md_notes/models/base_model.rb, line 50
def respond_to_missing?(method_sym, include_private = false)
  instance_variable_defined?("@#{method_sym}") ? true : super
end
to_hash() click to toggle source

Returns a Hash representation of the current object.

# File lib/md_notes/models/base_model.rb, line 10
def to_hash
  hash = {}
  instance_variables.each do |name|
    value = instance_variable_get(name)
    name = name[1..-1]
    key = self.class.names.key?(name) ? self.class.names[name] : name
    if value.instance_of? Array
      hash[key] = value.map { |v| v.is_a?(BaseModel) ? v.to_hash : v }
    elsif value.instance_of? Hash
      hash[key] = {}
      value.each do |k, v|
        hash[key][k] = v.is_a?(BaseModel) ? v.to_hash : v
      end
    else
      hash[key] = value.is_a?(BaseModel) ? value.to_hash : value
    end
  end
  hash
end
to_json(options = {}) click to toggle source

Returns a JSON representation of the curent object.

# File lib/md_notes/models/base_model.rb, line 31
def to_json(options = {})
  hash = to_hash
  hash.to_json(options)
end