Module: Sandwich::Base::ClassMethods

Defined in:
lib/sandwich/helpers/base.rb

Instance Method Summary collapse

Instance Method Details

#default_attribute(name = nil, &block) ⇒ Object



4
5
6
7
8
9
10
11
12
# File 'lib/sandwich/helpers/base.rb', line 4

def default_attribute(name = nil, &block)
  if name
    @default_attribute = name
  elsif block_given?
    @default_attribute = block
  else
    raise "Need an attribute name or a block for #{self}"
  end
end

#default_attribute_args(value, op) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/sandwich/helpers/base.rb', line 14

def default_attribute_args(value, op)
  if @default_attribute.respond_to?(:call)
    case @default_attribute.arity
    when 1
      @default_attribute.call(value)
    when 2
      @default_attribute.call(value, op)
    else
      raise ArgumentError, "the block sent to #{name}.default_attribute needs 1 or 2 arguments"
    end
  elsif @default_attribute
    {@default_attribute => value}
  else
    raise "No default attribute defined for #{self}"
  end
end

#prepare_args(args, op) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/sandwich/helpers/base.rb', line 31

def prepare_args(args, op)
  case args
  when Hash
    Hash[*args.each_pair.map { |k, v| prepare_pair(k, v) }.flatten]
  when nil
    {}
  else
    default_attribute_args(args, op)
  end
end