Module: Shale::Builder::AssignedAttributes

Extended by:
T::Helpers, T::Sig
Defined in:
lib/shale/builder/assigned_attributes.rb

Overview

Include in a class that already includes ‘Shale::Builder` to add support for getting a list of attributes that have been assigned.

@requires_ancestor: Object

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#__initializedObject (readonly)

: bool?



87
88
89
# File 'lib/shale/builder/assigned_attributes.rb', line 87

def __initialized
  @__initialized
end

Class Method Details

.included(mod) ⇒ Object

Gets called after including this module in a module or class. : (Module mod) -> void



22
23
24
25
# File 'lib/shale/builder/assigned_attributes.rb', line 22

def included(mod)
  mod.extend ClassMethods
  AssignedAttributes.prepare_mod(mod)
end

.prepare_mod(mod) ⇒ Object

Prepares the received module or class for dynamic method definition. : (Module mod) -> void



30
31
32
33
34
# File 'lib/shale/builder/assigned_attributes.rb', line 30

def prepare_mod(mod)
  assigned_attributes_methods_module = ::Module.new
  mod.instance_variable_set :@assigned_attributes_methods_module, assigned_attributes_methods_module
  mod.include assigned_attributes_methods_module
end

Instance Method Details

#assigned_attribute_namesObject

Returns a set of names of assigned shale attributes.

: -> Set



92
93
94
# File 'lib/shale/builder/assigned_attributes.rb', line 92

def assigned_attribute_names
  @assigned_attribute_names ||= Set.new
end

#assigned_attributesObject

Returns an array of shale attributes that have been assigned.

: -> Array



100
101
102
103
104
105
# File 'lib/shale/builder/assigned_attributes.rb', line 100

def assigned_attributes
  klass = self.class #: as untyped
  assigned_attribute_names.map do |name|
    klass.attributes.fetch(name)
  end
end

#assigned_valuesObject

Returns an array of shale values that have been assigned.

: -> Array



111
112
113
114
115
116
117
# File 'lib/shale/builder/assigned_attributes.rb', line 111

def assigned_values
  klass = self.class #: as untyped
  assigned_attribute_names.map do |name|
    attr = klass.attributes.fetch(name)
    Shale::Builder::Value.new(attr, public_send(attr.name))
  end
end

#initialize(*args, **kwargs, &block) ⇒ Object



81
82
83
84
# File 'lib/shale/builder/assigned_attributes.rb', line 81

def initialize(*args, **kwargs, &block)
  super
  @__initialized = true
end