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.
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/yarrow/schema.rb', line 107 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
90 91 92 93 94 95 96 |
# File 'lib/yarrow/schema.rb', line 90 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
98 99 100 |
# File 'lib/yarrow/schema.rb', line 98 def dictionary @dictionary ||= Hash.new end |
Instance Method Details
#dictionary ⇒ Object
103 104 105 |
# File 'lib/yarrow/schema.rb', line 103 def dictionary self.class.dictionary end |