parsed-attributes 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/parsed/base.rb +87 -0
- data/lib/parsed/json.rb +18 -0
- metadata +49 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1aae2201346318b078febd33f14aa52f11935c7f
|
4
|
+
data.tar.gz: 0de771e1ba13bd77f0384210dd42ec67ac808c44
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4e486091e68ed956d73c7b9f3870c3cffffc526eb7e3b1a545341a8e6de8089b3271e530142124c1e0c93cde18d56ce9b9af3f38a39e990b8e499a94c83f9c4d
|
7
|
+
data.tar.gz: 79a766d510c8f3dbca9c78fc219fc9301413254f0ad2344d4d1c2e92e5b603aeea0021bccd10c865c297dab2cb9e657a1a3d55dbd050a2e2fdbe163729412f18
|
data/lib/parsed/base.rb
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
module Parsed
|
2
|
+
module Base
|
3
|
+
|
4
|
+
##
|
5
|
+
# Helper to define the raw methods
|
6
|
+
# ---
|
7
|
+
# Expects:
|
8
|
+
# name (String):: Determines the method name.
|
9
|
+
# For example: 'yaml' will create the methods
|
10
|
+
# - #raw_yaml
|
11
|
+
# - #raw_yaml=
|
12
|
+
#
|
13
|
+
# And assumes the instance variables:
|
14
|
+
# - @raw_yaml
|
15
|
+
# - @parsed_yaml
|
16
|
+
def __define_parsed_attributes_raw_methods(name)
|
17
|
+
attr_reader "raw_#{name}"
|
18
|
+
|
19
|
+
define_method "raw_#{name}=" do |raw|
|
20
|
+
self.instance_variable_set("@raw_#{name}", raw)
|
21
|
+
if self.instance_variable_defined?("@parsed_#{name}")
|
22
|
+
self.remove_instance_variable("@parsed_#{name}")
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
##
|
28
|
+
# Helper to define the parsed method
|
29
|
+
# ---
|
30
|
+
# This handles the behavior around exceptions, and setting/removing the
|
31
|
+
# the instance variables.
|
32
|
+
#
|
33
|
+
# Expects:
|
34
|
+
# name (String):: Determines the method name.
|
35
|
+
# For example: 'yaml' will create the method
|
36
|
+
# - #parsed_yaml
|
37
|
+
#
|
38
|
+
# And assumes the instance variables:
|
39
|
+
# - @raw_yaml
|
40
|
+
# - @parsed_yaml
|
41
|
+
# options (acts like Hash):: Modifies the exception handling behavior
|
42
|
+
# raises_once (boolean):: Setting to true will raise on the first error.
|
43
|
+
#
|
44
|
+
# This is mostly so that if you are catching and
|
45
|
+
# printing error messages they won't flood the
|
46
|
+
# terminal.
|
47
|
+
#
|
48
|
+
# This overrides the raises option
|
49
|
+
#
|
50
|
+
# raises(boolean):: Setting to true will raise on every error.
|
51
|
+
#
|
52
|
+
# This method accepts a block, which is passed the raw value, and is expected to return
|
53
|
+
# the parsed data.
|
54
|
+
def __define_parsed_attributes_parsed_methods(name, options={}, &parse_block)
|
55
|
+
parsed_varname = "@parsed_#{name}"
|
56
|
+
|
57
|
+
define_method "parsed_#{name}" do
|
58
|
+
if self.instance_variable_defined? parsed_varname
|
59
|
+
return self.instance_variable_get parsed_varname
|
60
|
+
end
|
61
|
+
|
62
|
+
unless parse_block.nil?
|
63
|
+
begin
|
64
|
+
parsed = parse_block.call self.instance_variable_get("@raw_#{name}")
|
65
|
+
self.instance_variable_set(parsed_varname, parsed)
|
66
|
+
rescue StandardError => e
|
67
|
+
if options[:raise_once] == true
|
68
|
+
self.instance_variable_set(parsed_varname, nil)
|
69
|
+
end
|
70
|
+
raise e if options[:raise] == true || options[:raise_once] == true
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
return self.instance_variable_get(parsed_varname)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
##
|
79
|
+
# Shortcut method for if the default behavior is desired.
|
80
|
+
# ---
|
81
|
+
# Refer to the documentation for the other methods
|
82
|
+
def __define_parsed_attributes_all_methods(name, options={}, &parse_block)
|
83
|
+
__define_parsed_attributes_raw_methods name
|
84
|
+
__define_parsed_attributes_parsed_methods name, options, &parse_block
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
data/lib/parsed/json.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require_relative 'base'
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
module Parsed
|
5
|
+
module Json
|
6
|
+
include Parsed::Base
|
7
|
+
|
8
|
+
##
|
9
|
+
# Defines an attribute pair to handle JSON encoded date
|
10
|
+
# ---
|
11
|
+
# Accepts default options, see Parsed::Base
|
12
|
+
def json_attribute(name, options = {})
|
13
|
+
__define_parsed_attributes_all_methods name, options do |raw_value|
|
14
|
+
JSON.parse raw_value
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
metadata
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: parsed-attributes
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Morgen Peschke
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-07-31 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: |2
|
14
|
+
Helps with parsing data by creating paired
|
15
|
+
attributes, a raw attribute, and a linked read-only
|
16
|
+
parsed attribute. The parsed attribute auto-updates
|
17
|
+
when the raw counterpart is updated.
|
18
|
+
email: morgen.peschke@gmail.com
|
19
|
+
executables: []
|
20
|
+
extensions: []
|
21
|
+
extra_rdoc_files: []
|
22
|
+
files:
|
23
|
+
- lib/parsed/base.rb
|
24
|
+
- lib/parsed/json.rb
|
25
|
+
homepage: https://github.com/morgen-peschke/parsed-attributes
|
26
|
+
licenses:
|
27
|
+
- MIT
|
28
|
+
metadata: {}
|
29
|
+
post_install_message:
|
30
|
+
rdoc_options: []
|
31
|
+
require_paths:
|
32
|
+
- lib
|
33
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - ">="
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
43
|
+
requirements: []
|
44
|
+
rubyforge_project:
|
45
|
+
rubygems_version: 2.2.2
|
46
|
+
signing_key:
|
47
|
+
specification_version: 4
|
48
|
+
summary: Generates paired attributes that parse their contents.
|
49
|
+
test_files: []
|