Module: Monolens::Lens

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



8
9
10
# File 'lib/monolens/lens.rb', line 8

def options
  @options
end

Instance Method Details

#fetch_on(attr, arg) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/monolens/lens.rb', line 10

def fetch_on(attr, arg)
  if arg.key?(attr)
    [ attr, arg[attr] ]
  elsif arg.key?(attr_s = attr.to_s)
    [ attr_s, arg[attr_s] ]
  elsif arg.key?(attr_sym = attr.to_sym)
    [ attr_sym, arg[attr_sym] ]
  else
    [ attr, nil ]
  end
end

#initialize(options = {}) ⇒ Object



3
4
5
6
7
# File 'lib/monolens/lens.rb', line 3

def initialize(options = {})
  @options = options.each_with_object({}){|(k,v),memo|
    memo[k.to_sym] = v
  }
end

#is_array!(arg) ⇒ Object

Raises:



45
46
47
48
49
# File 'lib/monolens/lens.rb', line 45

def is_array!(arg)
  return if arg.is_a?(::Array)

  raise Monolens::Error, "Array expected, got #{arg.class}"
end

#is_enumerable!(arg) ⇒ Object

Raises:



39
40
41
42
43
# File 'lib/monolens/lens.rb', line 39

def is_enumerable!(arg)
  return if arg.is_a?(::Enumerable)

  raise Monolens::Error, "Enumerable expected, got #{arg.class}"
end

#is_hash!(arg) ⇒ Object

Raises:



33
34
35
36
37
# File 'lib/monolens/lens.rb', line 33

def is_hash!(arg)
  return if arg.is_a?(::Hash)

  raise Monolens::Error, "Hash expected, got #{arg.class}"
end

#is_string!(arg) ⇒ Object

Raises:



27
28
29
30
31
# File 'lib/monolens/lens.rb', line 27

def is_string!(arg)
  return if arg.is_a?(::String)

  raise Monolens::Error, "String expected, got #{arg.class}"
end

#option(name, default = nil) ⇒ Object



22
23
24
25
# File 'lib/monolens/lens.rb', line 22

def option(name, default = nil)
  _, option = fetch_on(name, options)
  option.nil? ? default : option
end