module StaticDataModel

Upon inclusion, provides basic static data model functionality:

class A

include StaticDataModel
self.model_data = [
  { id: 1, name: 'A1' },
  { id: 2, name: 'A2' }
]

end

This will give you:

A.all.first.id # => 1 A.all.last.name # => 'A2'

Constants

VERSION

Public Class Methods

included(base) click to toggle source
# File lib/static_data_model.rb, line 36
def self.included(base)
  base.extend ClassMethods
end
new(hash = {}) click to toggle source

Build a new instance of the model from the params Hash

@param [Hash] hash The attributes to be set

# File lib/static_data_model.rb, line 24
def initialize(hash = {})
  # hash.symbolize_keys!
  not_matching_keys = hash.keys - self.class.attribute_names
  if not_matching_keys.any?
    raise "#{self.class}::new does not accept keys #{not_matching_keys}."
  end

  hash.each do |name, value|
    instance_variable_set "@#{name}", value
  end
end