Module: StaticDataModel

Defined in:
lib/static_data_model.rb,
lib/static_data_model/version.rb

Overview

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’

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

VERSION =
'0.2.1'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



36
37
38
# File 'lib/static_data_model.rb', line 36

def self.included(base)
  base.extend ClassMethods
end

Instance Method Details

#initialize(hash = {}) ⇒ Object

Build a new instance of the model from the params Hash

Parameters:

  • hash (Hash) (defaults to: {})

    The attributes to be set



24
25
26
27
28
29
30
31
32
33
34
# 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