Class: Mother
- Inherits:
-
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.3.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
#data ⇒ Object
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
57
58
59
|
# File 'lib/mother.rb', line 57
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
|
# File 'lib/mother.rb', line 42
def create(thing)
case thing
when Array
Collection.new self, thing
when Hash
new thing
when String && /\.ya?ml$/
create YAML.load(File.read(thing))
when String && /\.json$/
create JSON.parse(File.read(thing))
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
|
#keys ⇒ Object
19
20
21
|
# File 'lib/mother.rb', line 19
def keys
self.class.create @data.keys
end
|
#respond_to_missing?(method, include_private = true) ⇒ 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
|
#values ⇒ Object
23
24
25
|
# File 'lib/mother.rb', line 23
def values
self.class.create @data.values
end
|