class Otoroshi::Initializer
Drawing of initialize method
Attributes
Public Class Methods
Draw a stringified initialize method
@param properties [Hash] a description of the class properties
@return [String]
@example
<<-RUBY def initialize(number: 0, message:, fruits: []) self.number = number self.message = message self.fruits = fruits end RUBY
# File lib/otoroshi/initializer.rb, line 22 def draw(properties) new(properties).draw end
Initialize an instance
@param properties [Hash] a description of the class properties
# File lib/otoroshi/initializer.rb, line 30 def initialize(properties = {}) @properties = properties end
Public Instance Methods
Draws a stringified initialize method
@return [String]
@example
<<-RUBY def initialize(foo:, bar: 0) self.foo = foo self.bar = bar end RUBY
# File lib/otoroshi/initializer.rb, line 45 def draw <<~RUBY def initialize(#{initialize_parameters}) #{initialize_body} end RUBY end
Private Instance Methods
Generates the default value of a parameter depending on options
@return [String]
@example when nil is allowed and default is set
" \"default\""
@example when nil is allowed and default is not set
" nil"
@example when nil is not allowed
""
# File lib/otoroshi/initializer.rb, line 81 def default_parameter_for(options) default, allow_nil = options.values_at(:default, :allow_nil) return ' nil' if default.nil? && allow_nil return '' if default.nil? && !allow_nil " #{prefix(default)}#{default}#{suffix(default)}" end
Generates initialize method assignments
@return [String]
@example Given properties { foo: { allow_nil: false, default: nil }, { allow_nil: true, default: 0 } }
<<-RUBY self.foo = foo self.bar = bar RUBY
# File lib/otoroshi/initializer.rb, line 118 def initialize_body assignments = properties.keys.map do |name| "self.#{name} = #{name}" end assignments.join("\n ") end
Generates initialize method parameters
@return [String]
@example
"foo:, bar: 0"
# File lib/otoroshi/initializer.rb, line 63 def initialize_parameters parameters = properties.map do |key, options| "#{key}:#{default_parameter_for(options)}" end parameters.join(', ') end
Generates the characters to put before the value @note it avoids symbol without colon or string without quotes
which would be interpreted as methods
# File lib/otoroshi/initializer.rb, line 92 def prefix(default) case default when Symbol then ':' when String then '"' when Time, Date, DateTime then '"' end end
Generates the characters to put after the value @note it avoids string without quotes which would be interpreted as method
# File lib/otoroshi/initializer.rb, line 102 def suffix(default) case default when String then '"' when Time, Date, DateTime then '"' end end