Module: LazyDoc::DSL::ClassMethods

Defined in:
lib/lazy_doc/dsl.rb

Instance Method Summary collapse

Instance Method Details

#access(*arguments) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/lazy_doc/dsl.rb', line 25

def access(*arguments)
  attributes, options = extract_from(arguments)

  if attributes.size == 1
    define_access_method_for(attributes[0], options)
  else
    define_access_methods_for(attributes)
  end
end

#create_method(attribute, required_command, *optional_commands) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/lazy_doc/dsl.rb', line 50

def create_method(attribute, required_command, *optional_commands)
  define_method attribute do
    memoizer.memoize attribute do
      value = required_command.execute(embedded_doc)

      optional_commands.inject(value) do |final_value, command|
        final_value = command.execute(final_value)
        final_value
      end
    end

  end

end

#define_access_method_for(attribute, options) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/lazy_doc/dsl.rb', line 35

def define_access_method_for(attribute, options)
  create_method(attribute,  Commands::ViaCommand.new(options[:via] || attribute),
                            Commands::DefaultValueCommand.new(options[:default]),
                            Commands::AsClassCommand.new(options[:as]),
                            Commands::ExtractCommand.new(options[:extract]),
                            Commands::FinallyCommand.new(options[:finally]))
end

#define_access_methods_for(attributes) ⇒ Object



43
44
45
46
47
48
# File 'lib/lazy_doc/dsl.rb', line 43

def define_access_methods_for(attributes)
  attributes.each do |attribute|
    create_method(attribute, Commands::ViaCommand.new(attribute))
  end

end

#extract_from(arguments) ⇒ Object



65
66
67
68
69
70
71
72
73
74
# File 'lib/lazy_doc/dsl.rb', line 65

def extract_from(arguments)
  arguments << {} unless arguments.last.is_a? Hash

  options = arguments.pop
  attributes = [arguments].flatten

  verify_arguments(attributes, options)

  [attributes, options]
end

#verify_arguments(attributes, options) ⇒ Object



76
77
78
79
80
# File 'lib/lazy_doc/dsl.rb', line 76

def verify_arguments(attributes, options)
  if attributes.size > 1 && !options.empty?
    raise ArgumentError, 'Options provided for multiple attributes'
  end
end