Class: Yarrow::Schema::Entity
- Inherits:
-
Object
- Object
- Yarrow::Schema::Entity
- Defined in:
- lib/yarrow/schema.rb
Overview
Entity with comparison by reference equality. Generates attribute helpers for a declared set of props. Used to replace Hashie::Mash without dragging in a whole new library.
Class Method Summary collapse
Instance Method Summary collapse
- #dictionary ⇒ Object
-
#initialize(config) ⇒ Entity
constructor
A new instance of Entity.
Constructor Details
#initialize(config) ⇒ Entity
Returns a new instance of Entity.
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/yarrow/schema.rb', line 95 def initialize(config) dictionary.each_key do |name| raise "missing declared attribute #{name}" unless dictionary.key?(name) end config.each_pair do |key, value| raise "#{key} not a declared attribute" unless dictionary.key?(key) defined_type = dictionary[key] unless value.is_a?(defined_type) raise "#{key} accepts #{defined_type} but #{value.class} given" end instance_variable_set("@#{key}", value) end end |
Class Method Details
.attribute(name, value_type) ⇒ Object
78 79 80 81 82 83 84 |
# File 'lib/yarrow/schema.rb', line 78 def attribute(name, value_type) # define_method("map_#{name}".to_sym) do |input| # value_type.coerce(input) # end dictionary[name] = value_type attr_reader(name) end |
.dictionary ⇒ Object
86 87 88 |
# File 'lib/yarrow/schema.rb', line 86 def dictionary @dictionary ||= Hash.new end |
Instance Method Details
#dictionary ⇒ Object
91 92 93 |
# File 'lib/yarrow/schema.rb', line 91 def dictionary self.class.dictionary end |