Module: SmoothOperator::AttributeAssignment

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

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#_meta_dataObject (readonly)

Returns the value of attribute _meta_data.



58
59
60
# File 'lib/smooth_operator/attribute_assignment.rb', line 58

def 
  @_meta_data
end

#_optionsObject (readonly)

Returns the value of attribute _options.



58
59
60
# File 'lib/smooth_operator/attribute_assignment.rb', line 58

def _options
  @_options
end

Class Method Details

.included(base) ⇒ Object



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

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

Instance Method Details

#assign_attributes(_attributes = {}, options = {}) ⇒ Object



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

def assign_attributes(_attributes = {}, options = {})
  return nil unless _attributes.is_a?(Hash)
  
  attributes = _attributes = Helpers.stringify_keys(_attributes)

  if _attributes.include?(resource_name)
    attributes = _attributes.delete(resource_name)
    @_meta_data = _attributes
  end
  
  options.each { |key, value| @_options[key] = value } if options.is_a?(Hash)

  attributes.each { |name, value| push_to_internal_data(name, value) }
end

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



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/smooth_operator/attribute_assignment.rb', line 90

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

#has_data_from_serverObject Also known as: from_server



80
81
82
# File 'lib/smooth_operator/attribute_assignment.rb', line 80

def has_data_from_server
  _options[:from_server] == true
end

#initialize(attributes = {}, options = {}) ⇒ Object



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

def initialize(attributes = {}, options = {})
  @_options = {}

  before_initialize(attributes, options)

  assign_attributes attributes, options

  after_initialize(attributes, options)
end

#internal_dataObject



86
87
88
# File 'lib/smooth_operator/attribute_assignment.rb', line 86

def internal_data
  @internal_data ||= {}
end

#parent_objectObject



76
77
78
# File 'lib/smooth_operator/attribute_assignment.rb', line 76

def parent_object
  _options[:parent_object]
end

#push_to_internal_data(attribute_name, attribute_value) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/smooth_operator/attribute_assignment.rb', line 102

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