Skip to content

Commit

Permalink
Add #inch method to Definition and Config
Browse files Browse the repository at this point in the history
  • Loading branch information
nepalez committed Sep 13, 2017
1 parent 356d6a8 commit 31b62c7
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/dry/initializer/builders/attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def call

private

# rubocop: disable Style/MethodLength
# rubocop: disable Metrics/MethodLength
def initialize(definition)
@definition = definition
@option = definition.option
Expand All @@ -26,7 +26,7 @@ def initialize(definition)
@item = "__dry_initializer_definition__"
@val = @option ? "__dry_initializer_value__" : @source
end
# rubocop: enable Style/MethodLength
# rubocop: enable Metrics/MethodLength

def lines
[
Expand Down
12 changes: 12 additions & 0 deletions lib/dry/initializer/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,18 @@ def finalize
self
end

# Human-readable representation of configured params and options
# @return [String]
def inch
line = Builders::Signature[self]
line = line.gsub("__dry_initializer_options__", "options")
lines = ["@!method initialize(#{line})"]
lines += ["Initializes an instance of #{extended_class}"]
lines += definitions.values.map(&:inch)
lines += ["@return [#{extended_class}]"]
lines.join("\n")
end

private

def initialize(extended_class = nil, null: UNDEFINED)
Expand Down
9 changes: 9 additions & 0 deletions lib/dry/initializer/definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ def code
Builders::Reader[self]
end

def inch
@inch ||= (option ? "@option" : "@param ").tap do |text|
text << " [Object]"
text << (option ? " :#{source}" : " #{source}")
text << (optional ? " (optional)" : " (required)")
text << " #{desc}" if desc
end
end

private

def initialize(option, null, source, coercer = nil, **options)
Expand Down

0 comments on commit 31b62c7

Please sign in to comment.