Class: Embargoed::OrderedOptions

Inherits:
Hash
  • Object
show all
Defined in:
lib/embargoed/ordered_options.rb

Direct Known Subclasses

InheritableOptions

Instance Method Summary collapse

Constructor Details

#initialize(constructor = {}, &block) ⇒ OrderedOptions

Returns a new instance of OrderedOptions.



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/embargoed/ordered_options.rb', line 6

def initialize(constructor = {}, &block)
  if constructor.respond_to?(:to_hash)
    super()
    update(constructor, &block)
    hash = constructor.to_hash

    self.default = hash.default if hash.default
    self.default_proc = hash.default_proc if hash.default_proc
  else
    super()
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/embargoed/ordered_options.rb', line 41

def method_missing(name, *args)
  name_string = name.to_s
  if name_string.chomp!('=')
    self[name_string] = args.first
  else
    bangs = name_string.chomp!('!')

    if bangs
      value = fetch(name_string.to_sym)
      raise(RuntimeError.new("#{name_string} is blank.")) if value.nil? || value.empty?
      value
    else
      self[name_string]
    end
  end
end

Instance Method Details

#[](key) ⇒ Object



37
38
39
# File 'lib/embargoed/ordered_options.rb', line 37

def [](key)
  super(key.to_sym)
end

#[]=(key, value) ⇒ Object



33
34
35
# File 'lib/embargoed/ordered_options.rb', line 33

def []=(key, value)
  super(key.to_sym, value)
end

#except(*keys) ⇒ Object



58
59
60
# File 'lib/embargoed/ordered_options.rb', line 58

def except(*keys)
  dup.except!(*keys)
end

#except!(*keys) ⇒ Object



62
63
64
65
# File 'lib/embargoed/ordered_options.rb', line 62

def except!(*keys)
  keys.each { |key| delete(key) }
  self
end

#respond_to_missing?(name, include_private) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/embargoed/ordered_options.rb', line 68

def respond_to_missing?(name, include_private)
  true
end

#update(other_hash) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/embargoed/ordered_options.rb', line 19

def update(other_hash)
  if other_hash.is_a? Hash
    super(other_hash)
  else
    other_hash.to_hash.each_pair do |key, value|
      if block_given?
        value = yield(key, value)
      end
      self[key] = value
    end
    self
  end
end