Module: SmoothOperator::AttributeAssignment

Included in:
OpenStruct
Defined in:
lib/smooth_operator/attribute_assignment.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



7
8
9
# File 'lib/smooth_operator/attribute_assignment.rb', line 7

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#assign_attributes(attributes = {}) ⇒ Object



42
43
44
45
46
# File 'lib/smooth_operator/attribute_assignment.rb', line 42

def assign_attributes(attributes = {})
  return nil unless attributes.is_a?(Hash)
  
  attributes.each { |name, value| push_to_internal_data(name, value) }
end

#get_internal_data(field, method = :value) ⇒ Object



52
53
54
# File 'lib/smooth_operator/attribute_assignment.rb', line 52

def get_internal_data(field, method = :value)
  internal_data[field].nil? ? nil : internal_data[field].send(method)
end

#initialize(attributes = {}) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/smooth_operator/attribute_assignment.rb', line 34

def initialize(attributes = {})
  before_initialize(attributes)

  assign_attributes attributes

  after_initialize(attributes)
end

#internal_dataObject



48
49
50
# File 'lib/smooth_operator/attribute_assignment.rb', line 48

def internal_data
  @internal_data ||= {}
end

#push_to_internal_data(attribute_name, attribute_value) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/smooth_operator/attribute_assignment.rb', line 56

def push_to_internal_data(attribute_name, attribute_value)
  attribute_name = attribute_name.to_s

  return nil unless allowed_attribute(attribute_name)
  
  known_attributes.add attribute_name
  
  if internal_data[attribute_name].nil?
    internal_data[attribute_name] = InternalAttribute.new(attribute_name, attribute_value, internal_structure[attribute_name], self.class.turn_unknown_hash_to_open_struct)
  else
    internal_data[attribute_name].set_value(attribute_value)
  end
end