lib/templates/rb_classes.erb in peto-0.2.4 vs lib/templates/rb_classes.erb in peto-0.2.5

- old
+ new

@@ -3,24 +3,46 @@ <%- name = @target[:name].camelize -%> <%- args = @target[:args] -%> module Peto class <%= name %> include PetoClass + + def self.create(hash_args) + instance = new + <%- args.each do |arg| -%> + <%- if atomic_types.include?(arg[:type].constantize) -%> + instance.<%=arg[:name]%> = hash_args["<%= arg[:name]%>"] + <%- elsif arg[:array_type] -%> + instance.<%=arg[:name]%> = hash_args["<%= arg[:name]%>"].map {|arg| <%=arg[:array_type]%>.create(arg) } + <%- else -%> + instance.<%=arg[:name]%> = <%=arg[:type]%>.create(hash_args["<%= arg[:name]%>"]) + <%- end -%> + <%- end -%> + return instance + end + def initialize(args={}) <%- args.each do |arg| -%> <%- if arg[:array_type] -%> @<%= arg[:name] %> = [] # for <%= arg[:array_type] %> <%- else -%> @<%= arg[:name] %> = nil <%- end -%> <%- end -%> - set_by_hash(args) + set_args(args) raise_errors unless valid? end <%- args.each do |arg| -%> attr_reader :<%= arg[:name] %> + <%- end -%> + + <%- args.each do |arg| -%> + def <%= arg[:name] %>=(value) + @<%=arg[:name]%> = value + raise_errors unless valid? + end <%- end -%> def members [<%= args.map{|arg| ":#{arg[:name]}"}.join(",") %>] end