Module: Structured::Struct::ClassMethods

Includes:
Type
Defined in:
lib/structured/struct.rb

Instance Method Summary collapse

Methods included from Type

#default_value, #type_name

Instance Method Details

#attributesObject



53
54
55
# File 'lib/structured/struct.rb', line 53

def attributes
  @attributes ||= {}
end

#format_stack_frame_element(name) ⇒ Object



90
91
92
# File 'lib/structured/struct.rb', line 90

def format_stack_frame_element(name)
  ".#{name}"
end

#ignore_attribute(name) ⇒ Object



68
69
70
# File 'lib/structured/struct.rb', line 68

def ignore_attribute(name)
  attributes[name.to_s] = :ignore
end

#new_with_defaultsObject



81
82
83
84
85
86
87
88
# File 'lib/structured/struct.rb', line 81

def new_with_defaults
  new_instance = new
  attributes.each do |name, attribute|
    next if attribute == :ignore
    new_instance.send(:"#{name}=", attribute.default_value) if attribute.has_default?
  end
  new_instance
end

#optional_attribute(name, type, default: Attribute::UNDEFINED) ⇒ Object



62
63
64
65
66
# File 'lib/structured/struct.rb', line 62

def optional_attribute(name, type, default: Attribute::UNDEFINED)
  define_accessor(name.to_s)
  default = default == Attribute::UNDEFINED ? type.default_value : default
  attributes[name.to_s] = Attribute.new(self, type, default_value: default)
end

#parse(yaml_hash, stack:, context: nil) ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/structured/struct.rb', line 72

def parse(yaml_hash, stack:, context: nil)
  parsed_instance = new
  parse_attributes(yaml_hash || {}, stack: stack, context: context).each do |attribute, value|
    parsed_instance.send(:"#{attribute}=", value)
  end

  parsed_instance
end

#required_attribute(name, type) ⇒ Object



57
58
59
60
# File 'lib/structured/struct.rb', line 57

def required_attribute(name, type)
  define_accessor(name.to_s)
  attributes[name.to_s] = Attribute.new(self, type)
end