Class: Obvious::Entity
- Inherits:
-
Object
- Object
- Obvious::Entity
- Includes:
- EntityMixin
- Defined in:
- lib/obvious/entity.rb
Instance Method Summary collapse
-
#initialize(input) ⇒ Entity
constructor
A new instance of Entity.
- #to_hash ⇒ Object
Methods included from EntityMixin
Constructor Details
#initialize(input) ⇒ Entity
Returns a new instance of Entity.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/obvious/entity.rb', line 38 def initialize input shape = self.class.shape validations = self.class.validations || [] self.class.shape.freeze self.class.validations.freeze @values = {} input.each do |k, v| unless shape[k] raise Obvious::ShapeError.new "Invalid input field: #{k}" else @values[k] = v end end @values.freeze # this might need to be recursive? shape.each do |k, v| unless @values[k].class == v msg = "Validation Error: Invalid value for #{k}, should be a #{v}" raise Obvious::TypeError.new msg end end validations.each { |method| send method } freeze end |
Instance Method Details
#to_hash ⇒ Object
68 69 70 |
# File 'lib/obvious/entity.rb', line 68 def to_hash {}.tap {|h| @values.each { |k, v| h[k] = v } } end |