Module: Structured::Struct
- Defined in:
- lib/structured/struct.rb
Defined Under Namespace
Modules: ClassMethods
Constant Summary collapse
- Boolean =
Alias these types here so these constant are part of the lookup path when being used in our struct definitions.
Structured::Boolean
- String =
Structured::String
- Integer =
Structured::Integer
- Dictionary =
Structured::Dictionary
- List =
Structured::List
- Nullable =
Structured::Nullable
Class Method Summary collapse
Instance Method Summary collapse
- #generate(_context) ⇒ Object
- #has_attribute?(name) ⇒ Boolean
- #initialize ⇒ Object
- #read_attribute(name) ⇒ Object
- #write_attribute(name, value) ⇒ Object
Class Method Details
.included(base) ⇒ Object
22 23 24 |
# File 'lib/structured/struct.rb', line 22 def self.included(base) base.extend(ClassMethods) end |
Instance Method Details
#generate(_context) ⇒ Object
30 31 32 33 34 35 36 |
# File 'lib/structured/struct.rb', line 30 def generate(_context) attrs = self.class.attributes.select do |name, _| has_attribute?(name) end attrs.keys.map { |name| [name, public_send(name)] }.to_h end |
#has_attribute?(name) ⇒ Boolean
42 43 44 |
# File 'lib/structured/struct.rb', line 42 def has_attribute?(name) !@attributes[name].nil? end |
#initialize ⇒ Object
26 27 28 |
# File 'lib/structured/struct.rb', line 26 def initialize @attributes = {} end |
#read_attribute(name) ⇒ Object
38 39 40 |
# File 'lib/structured/struct.rb', line 38 def read_attribute(name) @attributes.fetch(name) { self.class.attributes[name].default_value } end |
#write_attribute(name, value) ⇒ Object
46 47 48 |
# File 'lib/structured/struct.rb', line 46 def write_attribute(name, value) @attributes[name] = value end |