Class: Mother

Inherits:
Object
  • Object
show all
Defined in:
lib/mother.rb,
lib/mother/version.rb,
lib/mother/collection.rb

Defined Under Namespace

Classes: Collection

Constant Summary collapse

VERSION =
"0.2.0"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argument) ⇒ Mother

Returns a new instance of Mother.



9
10
11
12
13
# File 'lib/mother.rb', line 9

def initialize(argument)
  self.class.argument_failure! unless argument.is_a?(Hash)

  @data = argument
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



27
28
29
# File 'lib/mother.rb', line 27

def method_missing(method, *args, &block)
  self[method]
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



7
8
9
# File 'lib/mother.rb', line 7

def data
  @data
end

Class Method Details

.argument_failure!Object

Raises:

  • (ArgumentError)


62
63
64
# File 'lib/mother.rb', line 62

def argument_failure!
  raise ArgumentError.new('Must be a hash, or YAML/JSON filename')
end

.create(thing) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/mother.rb', line 42

def create(thing)
  case thing
  when Array
    Collection.new self, thing
  when Hash
    new thing
  when String
    case
    when thing.match(/\.ya?ml$/)
      self.create YAML.load(File.read(thing))
    when thing.match(/\.json$/)
      self.create JSON.parse(File.read(thing))
    else
      thing
    end
  else
    thing
  end
end

Instance Method Details

#[](key) ⇒ Object



15
16
17
# File 'lib/mother.rb', line 15

def [](key)
  self.class.create ___fetch(key)
end

#keysObject



19
20
21
# File 'lib/mother.rb', line 19

def keys
  @data.keys
end

#respond_to_missing?(method, include_private = true) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/mother.rb', line 31

def respond_to_missing?(method, include_private = true)
  keys.include?(method) || keys.include?(method.to_s) || super
end

#valuesObject



23
24
25
# File 'lib/mother.rb', line 23

def values
  @data.values
end