contextuable 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +19 -9
- data/contextuable.gemspec +0 -1
- data/lib/contextuable.rb +31 -16
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8bd1b22c48cd964a9e88d7c24042a3a5237cf412
|
4
|
+
data.tar.gz: 29c4996901db2aaa4a4ca98618ddb2a912b11281
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7c766b2fb0e126589259d917cead49aaffbc31f97df7edb4ce649215ad50dc0b55ab9c2736b004a9096d8e1765c763ba0941298a1a07c79ff238da02b48a9999
|
7
|
+
data.tar.gz: 71dabd1609822f5401b0a23f98bedeba540f737c834fb96d1779611609c575a0d69780c626c01d006611891ac2df5beb112a212d126bd378ac991d853d862bb9
|
data/README.md
CHANGED
@@ -21,17 +21,21 @@ Or install it yourself as:
|
|
21
21
|
## Usage
|
22
22
|
|
23
23
|
__Extended OpenStruct:__
|
24
|
+
|
24
25
|
```ruby
|
25
26
|
context = Contextuable.new(name: 'John', surname: 'Doe')
|
26
27
|
context.name # => 'John'
|
27
|
-
context.
|
28
|
+
context.name_provided? # => true
|
28
29
|
context.surname # => 'Doe'
|
29
|
-
context.
|
30
|
+
context.foo_provided? # => false
|
31
|
+
context.foo_not_provided? # => true
|
30
32
|
context.foo = :bar
|
31
|
-
context.
|
33
|
+
context.foo_provided? # => true
|
34
|
+
context.foo_not_provided? # => false
|
32
35
|
context.foo # => :bar
|
33
36
|
context.to_h # => {:name=>"John", :surname=>"Doe", :foo=>:bar}
|
34
37
|
```
|
38
|
+
|
35
39
|
_more complex example_
|
36
40
|
```ruby
|
37
41
|
class Input < Contextuable
|
@@ -54,10 +58,12 @@ i = Input.new(name: 'Hotel', city: 'Barcelona', address: 'Happy street', not_per
|
|
54
58
|
i.types
|
55
59
|
# => ["lodging"]
|
56
60
|
i.long_name
|
57
|
-
# => "Hotel,Happy street,Barcelona"
|
61
|
+
# => "Hotel, Happy street, Barcelona"
|
62
|
+
i.name
|
63
|
+
# => "Hotel"
|
58
64
|
i.hotel_name
|
59
65
|
# => "Hotel"
|
60
|
-
i.
|
66
|
+
i.phone_number_provided?
|
61
67
|
# => false
|
62
68
|
i.not_permitted
|
63
69
|
# => nil
|
@@ -97,14 +103,18 @@ class Example2 < Contextuable
|
|
97
103
|
end
|
98
104
|
ex = Example2.new
|
99
105
|
ex.foo
|
100
|
-
=> :bar
|
106
|
+
# => :bar
|
101
107
|
ex.bar
|
102
|
-
=> :foo
|
108
|
+
# => :foo
|
109
|
+
ex.foo = :hello
|
110
|
+
ex.foo
|
111
|
+
# => :hello
|
112
|
+
|
103
113
|
ex2 = Example2.new(foo: 'something', bar: true)
|
104
114
|
ex2.foo
|
105
|
-
=> 'something'
|
115
|
+
# => 'something'
|
106
116
|
ex2.bar
|
107
|
-
=> true
|
117
|
+
# => true
|
108
118
|
```
|
109
119
|
|
110
120
|
**ensure_presence**
|
data/contextuable.gemspec
CHANGED
@@ -8,7 +8,6 @@ Gem::Specification.new do |spec|
|
|
8
8
|
spec.version = Contextuable::VERSION
|
9
9
|
spec.authors = ["Artur Pañach"]
|
10
10
|
spec.email = ["arturictus@gmail.com"]
|
11
|
-
|
12
11
|
spec.summary = %q{Structs with steroids.}
|
13
12
|
spec.description = %q{Better way to improve your data structs.}
|
14
13
|
spec.homepage = "https://www.github.com/arturictus/contextuable"
|
data/lib/contextuable.rb
CHANGED
@@ -1,11 +1,10 @@
|
|
1
|
-
# require 'forwardable'
|
2
1
|
class Contextuable
|
3
|
-
|
4
|
-
# delegate :[], to: :args
|
2
|
+
VERSION = "0.2.0"
|
5
3
|
|
6
|
-
VERSION = "0.1.0"
|
7
4
|
class RequiredFieldNotPresent < ArgumentError; end
|
8
5
|
class PresenceRequired < ArgumentError; end
|
6
|
+
class WrongArgument < ArgumentError; end
|
7
|
+
|
9
8
|
class << self
|
10
9
|
def required(*names)
|
11
10
|
@_required = names.map(&:to_sym)
|
@@ -34,13 +33,11 @@ class Contextuable
|
|
34
33
|
alias_method :to_hash, :args
|
35
34
|
|
36
35
|
def initialize(hash = {})
|
37
|
-
|
38
|
-
fail RequiredFieldNotPresent unless _required_args.map(&:to_sym).all? { |r| hash.keys.map(&:to_sym).include?(r) }
|
39
|
-
fail PresenceRequired if _presence_required.map(&:to_sym).any? { |r| hash[r].nil? }
|
36
|
+
check_input_errors(hash)
|
40
37
|
hash = hash.select{|k, v| _permitted.include?(k.to_sym) } if _only_permitted?
|
41
38
|
@args = _defaults.merge(hash)
|
42
39
|
args.each do |k, v|
|
43
|
-
|
40
|
+
define_contextuable_method(k, v)
|
44
41
|
end
|
45
42
|
end
|
46
43
|
|
@@ -60,10 +57,10 @@ class Contextuable
|
|
60
57
|
key = name.to_s.gsub('=', '').to_sym
|
61
58
|
set_attribute(key, value)
|
62
59
|
else
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
60
|
+
case name.to_s
|
61
|
+
when /\A\w+_not_provided\?\z/ then true
|
62
|
+
when /\A\w+_provided\?\z/ then false
|
63
|
+
end
|
67
64
|
end
|
68
65
|
end
|
69
66
|
|
@@ -71,13 +68,13 @@ class Contextuable
|
|
71
68
|
|
72
69
|
def set_attribute(key, value)
|
73
70
|
args[key] = value
|
74
|
-
|
71
|
+
define_contextuable_method(key, value)
|
75
72
|
end
|
76
73
|
|
77
|
-
def
|
74
|
+
def define_contextuable_method(key, value)
|
78
75
|
define_singleton_method(key) { args.fetch(key) }
|
79
|
-
define_singleton_method("#{key}?") { true }
|
80
|
-
define_singleton_method("
|
76
|
+
define_singleton_method("#{key}_provided?") { true }
|
77
|
+
define_singleton_method("#{key}_not_provided?") { false }
|
81
78
|
end
|
82
79
|
|
83
80
|
def find_in_equivalents(name)
|
@@ -121,4 +118,22 @@ class Contextuable
|
|
121
118
|
def _required_args
|
122
119
|
self.class.instance_variable_get(:@_required) || []
|
123
120
|
end
|
121
|
+
|
122
|
+
def check_input_errors(hash)
|
123
|
+
unless hash.class <= Hash
|
124
|
+
fail WrongArgument, "[Contextuable ERROR]: `#{self.class}` expects to receive an `Hash` or and object having `Hash` as ancestor."
|
125
|
+
end
|
126
|
+
|
127
|
+
_required_args.map(&:to_sym).each do |r|
|
128
|
+
unless hash.keys.map(&:to_sym).include?(r)
|
129
|
+
fail RequiredFieldNotPresent, "[Contextuable ERROR]: `#{self.class}` expect to be initialized with `#{r}` as an attribute."
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
_presence_required.map(&:to_sym).each do |r|
|
134
|
+
if hash[r].nil?
|
135
|
+
fail PresenceRequired, "[Contextuable ERROR]: `#{self.class}` expects to receive an attribute named `#{r}` not beeing `nil`"
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|
124
139
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: contextuable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Artur Pañach
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-03-
|
11
|
+
date: 2016-03-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -91,7 +91,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
91
91
|
version: '0'
|
92
92
|
requirements: []
|
93
93
|
rubyforge_project:
|
94
|
-
rubygems_version: 2.
|
94
|
+
rubygems_version: 2.2.3
|
95
95
|
signing_key:
|
96
96
|
specification_version: 4
|
97
97
|
summary: Structs with steroids.
|