Class: Dystruct
- Inherits:
-
Object
- Object
- Dystruct
- Defined in:
- lib/dystruct.rb,
lib/dystruct/class_methods.rb,
lib/dystruct/instance_methods.rb
Defined Under Namespace
Modules: ClassMethods, InstanceMethods Classes: PresenceRequired, RequiredFieldNotPresent, WrongArgument
Constant Summary collapse
- VERSION =
"0.4.0"
Instance Attribute Summary collapse
-
#attrs ⇒ Object
(also: #to_h, #to_hash)
readonly
Returns the value of attribute attrs.
Class Method Summary collapse
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
-
#initialize(hash = {}) ⇒ Dystruct
constructor
A new instance of Dystruct.
- #method_missing(name, *args, &block) ⇒ Object
Constructor Details
#initialize(hash = {}) ⇒ Dystruct
Returns a new instance of Dystruct.
19 20 21 22 23 24 25 26 27 |
# File 'lib/dystruct.rb', line 19 def initialize(hash = {}) unless hash.class <= Hash fail WrongArgument, "[Dystruct ERROR]: `#{self.class}` expects to receive a `Hash` or and object having `Hash` as ancestor." end @attrs = hash @attrs.each do |k, v| define_dystruct_method(k, v) end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
37 38 39 40 41 42 43 44 45 |
# File 'lib/dystruct.rb', line 37 def method_missing(name, *args, &block) if name =~ /\A\w+=\z/ set_attribute_macro(name, *args, &block) elsif out = provided_macro(name) out[:out] else super end end |
Instance Attribute Details
#attrs ⇒ Object (readonly) Also known as: to_h, to_hash
Returns the value of attribute attrs.
15 16 17 |
# File 'lib/dystruct.rb', line 15 def attrs @attrs end |
Class Method Details
.inherited(subclass) ⇒ Object
10 11 12 13 |
# File 'lib/dystruct.rb', line 10 def self.inherited(subclass) subclass.extend ClassMethods subclass.include InstanceMethods end |
Instance Method Details
#[](key) ⇒ Object
29 30 31 |
# File 'lib/dystruct.rb', line 29 def [](key) attrs[key] end |
#[]=(key, value) ⇒ Object
33 34 35 |
# File 'lib/dystruct.rb', line 33 def []=(key, value) set_attribute(key, value) end |