Skip to content

Commit

Permalink
Auto generated Struct child classes
Browse files Browse the repository at this point in the history
  • Loading branch information
ismasan committed Apr 13, 2020
1 parent 0c4af47 commit bb65f98
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 39 deletions.
36 changes: 0 additions & 36 deletions bench/notes.mkd

This file was deleted.

12 changes: 9 additions & 3 deletions lib/parametric/struct.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ def parametric_after_define_schema(schema)
include Struct
end
klass.schema = field.meta_data[:schema]
self.const_set("Sub_#{field.key}", klass)
self.const_set(__class_name(field.key), klass)
klass.parametric_after_define_schema(field.meta_data[:schema])
else
self.const_set("Sub_#{field.key}", field.meta_data[:schema])
self.const_set(__class_name(field.key), field.meta_data[:schema])
end
end
self.class_eval <<-RUBY, __FILE__, __LINE__ + 1
Expand All @@ -89,14 +89,20 @@ def wrap(key, value)
case value
when Hash
# find constructor for field
cons = self.const_get("Sub_#{key}")
cons = self.const_get(__class_name(key))
cons ? cons.new(value) : value.freeze
when Array
value.map{|v| wrap(key, v) }.freeze
else
value.freeze
end
end

PLURAL_END = /s$/.freeze

def __class_name(key)
key.to_s.split('_').map(&:capitalize).join.sub(PLURAL_END, '')
end
end
end
end

0 comments on commit bb65f98

Please sign in to comment.