yaml-methodizer 0.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/yaml-parser.rb +72 -0
- metadata +47 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ee5b1a1631e5bacaf480af4339f66921c463dd5b
|
4
|
+
data.tar.gz: e34144d31a9993fbd27e10864052a99ab01bd305
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 25de8f6c14625d30d6e600d6bfe4381f272ac72783b211b3cbb401d57dc57d553dd73c60ef2c69fa7c4d84d6bcf0db21e720cb9e304022e5296fec6c9f5b5bbe
|
7
|
+
data.tar.gz: cd4ec62a77c734313d02e5e7cd95a994f32fb9a0999b61f07616846b197c1618ed1b582cbf60b3c51f626af81636c13cf9aec0f81435c000926d855aad56d957
|
data/lib/yaml-parser.rb
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
|
3
|
+
class YAMLParser
|
4
|
+
|
5
|
+
@@hash = {}
|
6
|
+
|
7
|
+
def hash
|
8
|
+
@@hash
|
9
|
+
end
|
10
|
+
|
11
|
+
# Implement a method parse the contents of a YAML file and return
|
12
|
+
# an object whose values are accessible using the [] operator or method calls
|
13
|
+
#
|
14
|
+
# Note: Use of the YAML library is fine (encouraged, even) but please don't
|
15
|
+
# use any third-party gems that enable the required functionality.
|
16
|
+
def self.parse(filename)
|
17
|
+
# Open and read the file, store it as a variable 'file'
|
18
|
+
file = open(filename, &:read)
|
19
|
+
|
20
|
+
# Convert the file variable into a hash so that we can use the [] operator
|
21
|
+
@@hash = YAML.load(file)
|
22
|
+
custom = SpecialObject.new(@@hash)
|
23
|
+
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
class SpecialObject < YAMLParser
|
28
|
+
|
29
|
+
# Save the input object in an instance variable
|
30
|
+
def initialize(hash)
|
31
|
+
@object_hash = hash
|
32
|
+
end
|
33
|
+
|
34
|
+
# Catch missing method errors here and try to build the ones we need
|
35
|
+
def method_missing(meth_sym, *args, &block)
|
36
|
+
|
37
|
+
# If the object that has been passed in is an array, we have to treat it differently
|
38
|
+
if @object_hash.class == Array
|
39
|
+
|
40
|
+
# We only need to cover the first and last methods for the arrays, so we are going to
|
41
|
+
# check what the user called and act accordingly
|
42
|
+
if meth_sym.to_s == "first"
|
43
|
+
|
44
|
+
# If the array.first call returns a string (as opposed to other data types) then that is the answer we want to return
|
45
|
+
# Otherwise return a ew SpecialObject with the modified object as the argument
|
46
|
+
@object_hash[0].class == String ? @object_hash[0] : SpecialObject.new(@object_hash[0])
|
47
|
+
else
|
48
|
+
@object_hash[-1].class == String ? @object_hash[-1] : SpecialObject.new(@object_hash[-1])
|
49
|
+
end
|
50
|
+
|
51
|
+
# If the user tries to access the hash with the [] operator, let them, and just return a hash
|
52
|
+
elsif meth_sym.to_s == "[]"
|
53
|
+
@object_hash[*args]
|
54
|
+
|
55
|
+
# If the hash has a key that is the same as the method they called, return the value
|
56
|
+
elsif @object_hash[meth_sym.to_s]
|
57
|
+
|
58
|
+
# If the key has a value that is a string, that is the last step in the chain. Otherwise, return a new special object
|
59
|
+
@object_hash[meth_sym.to_s].class == String ? @object_hash[meth_sym.to_s] : SpecialObject.new(@object_hash["#{meth_sym}"])
|
60
|
+
|
61
|
+
# If none of the above is true, throw a no method error
|
62
|
+
else
|
63
|
+
super
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
# Make sure we respond to the new methods we are creating
|
68
|
+
def respond_to?(meth_sym, private_method = false)
|
69
|
+
true
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
metadata
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: yaml-methodizer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- "[Sam Barrientos]"
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-10-23 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Uses the YAML library as well as the method_missing method to parse a
|
14
|
+
yaml file and return an object that can be interacted with as a hash with the []
|
15
|
+
operator as well as . method calls
|
16
|
+
email: sstbarrientos@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- lib/yaml-parser.rb
|
22
|
+
homepage: http://rubygems.org/gems/yaml-methodizer
|
23
|
+
licenses:
|
24
|
+
- MIT
|
25
|
+
metadata: {}
|
26
|
+
post_install_message:
|
27
|
+
rdoc_options: []
|
28
|
+
require_paths:
|
29
|
+
- lib
|
30
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - ">="
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
35
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
requirements: []
|
41
|
+
rubyforge_project:
|
42
|
+
rubygems_version: 2.2.2
|
43
|
+
signing_key:
|
44
|
+
specification_version: 4
|
45
|
+
summary: Parse yaml files and make keys accessible through method calls as well as
|
46
|
+
the [] operator
|
47
|
+
test_files: []
|