Skip to content

Instantly share code, notes, and snippets.

@rippinrobr
Created November 19, 2010 18:22
Show Gist options
  • Save rippinrobr/706896 to your computer and use it in GitHub Desktop.
Save rippinrobr/706896 to your computer and use it in GitHub Desktop.
adding a constructor to our generated code
def add_constructor
arg_name = "repo"
# Create the constructor's object
ctor = CodeConstructor.new
ctor.Attributes = MemberAttributes.Public
ctor.Parameters.Add(CodeParameterDeclarationExpression.new("IRepository", arg_name))
# Grab the references for both the parameter we just added and the field we created
# earlier
arg_ref = CodeArgumentReferenceExpression.new(arg_name)
repo_ref = CodeFieldReferenceExpression.new
repo_ref.FieldName = @repo_field_name
# Creating the condition statement for the if/else that will wrap our assignment statement
condition_statement = CodeSnippetExpression.new("#{arg_name} != null")
# Create the statement that will be used when the condition statement is true
assign_statement = CodeAssignStatement.new(repo_ref, arg_ref)
ctor.Statements.Add(create_if_else_statement(condition_statement, assign_statement,@arg_null_exception_stmt))
@target_class.Members.Add(ctor)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment