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, via_command, optional_commands = []) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/lazy_doc/dsl.rb', line 47

def create_method(attribute, via_command, optional_commands = [])
  define_method attribute do
    memoizer.memoize attribute do
      value = via_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
# File 'lib/lazy_doc/dsl.rb', line 35

def define_access_method_for(attribute, options)
  optional_commands = Commands.create_commands_for options
  create_method(attribute, Commands::ViaCommand.new(options[:via] || attribute), optional_commands)
end

#define_access_methods_for(attributes) ⇒ Object



40
41
42
43
44
45
# File 'lib/lazy_doc/dsl.rb', line 40

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

end

#extract_from(arguments) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/lazy_doc/dsl.rb', line 62

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



73
74
75
76
77
# File 'lib/lazy_doc/dsl.rb', line 73

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