Module: Arrest::HasAttributes

Included in:
AbstractResource, HasMany, HasView, NestedResource, Ref
Defined in:
lib/arrest/attributes/has_attributes.rb

Defined Under Namespace

Modules: HasAttributesClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#attribute_valuesObject

Returns the value of attribute attribute_values.



20
21
22
# File 'lib/arrest/attributes/has_attributes.rb', line 20

def attribute_values
  @attribute_values
end

Class Method Details

.included(base) ⇒ Object

enables the implicit inclusion of these methods as class methods in the including class (AbstractResource)



45
46
47
# File 'lib/arrest/attributes/has_attributes.rb', line 45

def self.included(base) # :nodoc:
  base.extend HasAttributesClassMethods
end

Instance Method Details

#attributesObject



78
79
80
# File 'lib/arrest/attributes/has_attributes.rb', line 78

def attributes
  @attribute_values ||= {}
end

#attributes=(attribute_hash = {}) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/arrest/attributes/has_attributes.rb', line 82

def attributes=(attribute_hash = {})
  fields = self.class.all_fields
  field_names = fields.map(&:name)

  attribute_hash.each_pair do |k, v|
    matching_fields = fields.find_all{|f| f.name.to_s == k.to_s}
    field = matching_fields.first
    if field
      converted = field.from_hash(self, v)
      self.send("#{k}=", converted)
    end
  end
end

#init_from_hash(as_i = {}, from_json = false) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/arrest/attributes/has_attributes.rb', line 49

def init_from_hash(as_i={}, from_json = false)
  raise "hash expected but got #{as_i.class}" unless as_i.is_a?(Hash)
  @attribute_values ||= {}
  as = {}
  as_i.each_pair do |k,v|
    as[k.to_sym] = v
  end
  self.class.all_fields.each do |field|
    if from_json
      key = field.json_name
    else
      key = field.name
    end
    value = as[key]
    converted = field.from_hash(self, value)

    @attribute_values[field.name.to_sym] = converted

    self.send(field.name.to_s + '=', converted) unless converted == nil
  end
  self.reset_dirtiness
end

#initialize(hash = {}, from_json = false, &blk) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/arrest/attributes/has_attributes.rb', line 33

def initialize(hash = {}, from_json = false, &blk)
  raise "hash expected but got #{hash.class}" unless hash.is_a?(Hash)
  if block_given?
    @stubbed = true
    @load_blk = blk
  else
    init_from_hash(hash, from_json)
  end
end

#initialize_has_attributes(hash, from_json = false, &blk) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/arrest/attributes/has_attributes.rb', line 22

def initialize_has_attributes(hash, from_json = false, &blk)
  if block_given?
    @stubbed = true
    @load_blk = blk
  else
    @stubbed = false
  end
  raise "hash expected but got #{hash.class}" unless hash.is_a?(Hash)
  init_from_hash(hash, from_json)
end

#load_from_stubObject



101
102
103
104
# File 'lib/arrest/attributes/has_attributes.rb', line 101

def load_from_stub
  @load_blk.call
  @stubbed = false
end

#render_field_to_hash?(field, show_all_fields, action) ⇒ Boolean

decides whether attribute field will be rendered to hash either all, or, if an CRUD action given, only those sensitive to this action and if the action is update only those being dirty

Returns:



135
136
137
# File 'lib/arrest/attributes/has_attributes.rb', line 135

def render_field_to_hash?(field, show_all_fields, action)
  show_all_fields || !action || (field.actions.include?(action) && (action != :update || field.dirty?))
end

#reset_dirtinessObject



72
73
74
75
76
# File 'lib/arrest/attributes/has_attributes.rb', line 72

def reset_dirtiness
  self.class.all_fields.each do |field|
    field.dirty = false
  end
end

#stubbed?Boolean

Returns:



195
196
197
# File 'lib/arrest/attributes/has_attributes.rb', line 195

def stubbed?
  @stubbed
end

#to_hash(show_all_fields = true, json_names = false, action = nil) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/arrest/attributes/has_attributes.rb', line 111

def to_hash(show_all_fields = true, json_names = false, action = nil)
  result = {}

  self.class.all_fields.each do |field|

   if render_field_to_hash?(field, show_all_fields, action)

      if json_names
        json_name = field.json_name
      else
        json_name = field.name
      end
      val = self.send(field.name)
      converted = field.to_hash val

      result[json_name] = converted
    end
  end
  result
end

#to_jhash(action) ⇒ Object



107
108
109
# File 'lib/arrest/attributes/has_attributes.rb', line 107

def to_jhash(action)
  to_hash(false, true, action)
end

#update_attributes(attribute_hash = {}) ⇒ Object



96
97
98
99
# File 'lib/arrest/attributes/has_attributes.rb', line 96

def update_attributes(attribute_hash = {})
  self.attributes= attribute_hash
  self.save
end