class Encounter::Base

Basic class for Encounter objects. Implements lazy-load attribute definition.

@abstract

Public Class Methods

define_export_attrs(*items) click to toggle source

Defining list of export attributes. This attributes will be returned by {#to_json} and {#to_hash} methods.

@param [Array<Symbol>] items List of attributet names.

# File lib/encounter/base.rb, line 37
def self.define_export_attrs(*items)
  unless items.all? { |x| x.is_a? Symbol }
    raise ArgumentError, 'Want symbol parameters'
  end
  define_method(:export_fields) do
    items.freeze
  end
end
lazy_attr_accessor(*var_names) click to toggle source

Defines attribute accessor with lazy load function. {#load_data} method is called if this attribute value is requested while it was not defined.

@param [Array<Symbol>] var_names Parameter names

# File lib/encounter/base.rb, line 26
def self.lazy_attr_accessor(*var_names)
  var_names.each do |var_name|
    lazy_attr_reader var_name
    attr_writer var_name
  end
end
lazy_attr_reader(*var_names) click to toggle source

Defines attribute reader with lazy load function. {#load_data} method is called if this attribute value is requested while it was not defined.

@param [Array<Symbol>] var_names Parameter names

# File lib/encounter/base.rb, line 12
def self.lazy_attr_reader(*var_names)
  var_names.each do |var_name|
    define_method(var_name) do
      load_data unless instance_variable_defined? "@#{var_name}"
      instance_variable_get "@#{var_name}"
    end
  end
end
new(conn, params) click to toggle source

@param [Encounter::Connection] conn @param [Hash] params You can pass values in this parameters to predefine

attributes. Any class attribute can be set.
# File lib/encounter/base.rb, line 51
def initialize(conn, params)
  raise 'Connection needed' unless conn.is_a? Encounter::Connection
  raise 'Only hash parameter accepted' unless params.is_a? Hash

  @conn = conn

  assign_values(params)
end

Public Instance Methods

load_data() click to toggle source

This method should be defined in subclasses and load all data needed to parse data. @abstract

# File lib/encounter/base.rb, line 77
def load_data
  raise NotImplementedError, 'Should be defined in subclass'
end
to_hash() click to toggle source

Exports attributes, listed in {.define_export_attrs} as Hash.

@return [Hash]

# File lib/encounter/base.rb, line 70
def to_hash
  Hash[export_fields.map { |f| [f, send(f)] }]
end
to_json(options = nil) click to toggle source

Exports attributes, listed in {.define_export_attrs} as JSON string.

@return [String]

# File lib/encounter/base.rb, line 63
def to_json(options = nil)
  to_hash.to_json(options)
end

Private Instance Methods

assign_values(hash) click to toggle source

Assign values from hash to object attributes.

@param hash [Hash]

# File lib/encounter/base.rb, line 86
def assign_values(hash)
  raise ArgumentError, 'Parameter must be hash.' unless hash.is_a? Hash

  hash.each do |p, v|
    raise ArgumentError, "Wrong attribute: #{p}" unless respond_to? p
    instance_variable_set("@#{p}", v)
  end
end