Module: SmoothOperator::AttributeAssignment

Included in:
OpenStruct::Base
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



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

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

Instance Method Details

#assign_attributes(attributes = {}) ⇒ Object



55
56
57
58
59
# File 'lib/smooth_operator/attribute_assignment.rb', line 55

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



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/smooth_operator/attribute_assignment.rb', line 65

def get_internal_data(field, method = :value)
  result = internal_data[field]

  if result.nil?
    nil
  elsif method == :value
    result.is_a?(Attributes::Dirty) ? internal_data[field].send(method) : internal_data[field]
  else
    internal_data[field].send(method)
  end
end

#initialize(attributes = {}) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/smooth_operator/attribute_assignment.rb', line 47

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

  assign_attributes attributes

  after_initialize(attributes)
end

#internal_dataObject



61
62
63
# File 'lib/smooth_operator/attribute_assignment.rb', line 61

def internal_data
  @internal_data ||= {}
end

#push_to_internal_data(attribute_name, attribute_value) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/smooth_operator/attribute_assignment.rb', line 77

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?
    initiate_internal_data(attribute_name, attribute_value)
  else
    update_internal_data(attribute_name, attribute_value)
  end
end