Module: LazyResource::Mapping

Extended by:
ActiveSupport::Concern
Included in:
Resource
Defined in:
lib/lazy_resource/mapping.rb

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#fetchedObject

Returns the value of attribute fetched.



5
6
7
# File 'lib/lazy_resource/mapping.rb', line 5

def fetched
  @fetched
end

#other_attributesObject

Returns the value of attribute other_attributes.



5
6
7
# File 'lib/lazy_resource/mapping.rb', line 5

def other_attributes
  @other_attributes
end

#persistedObject

Returns the value of attribute persisted.



5
6
7
# File 'lib/lazy_resource/mapping.rb', line 5

def persisted
  @persisted
end

Class Method Details

.root_node_nameObject



15
16
17
# File 'lib/lazy_resource/mapping.rb', line 15

def self.root_node_name
  @root_node_name
end

.root_node_name=(node) ⇒ Object



11
12
13
# File 'lib/lazy_resource/mapping.rb', line 11

def self.root_node_name=(node)
  @root_node_name = node
end

Instance Method Details

#fetched?Boolean

Returns:

  • (Boolean)


7
8
9
# File 'lib/lazy_resource/mapping.rb', line 7

def fetched?
  @fetched
end

#load(hash, persisted = true) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/lazy_resource/mapping.rb', line 54

def load(hash, persisted=true)
  hash.fetched = true and return hash if hash.kind_of?(LazyResource::Mapping)
  return if hash.nil?

  self.tap do |resource|
    resource.persisted = persisted
    resource.fetched = false

    if mapped_name = resource.class.mapped_root_node_name(hash)
      other_attributes = hash
      hash = other_attributes.delete(mapped_name)
      self.other_attributes = other_attributes
    end

    hash.each do |name, value|
      attribute = self.class.attributes[name.to_sym]
      next if attribute.nil?

      type = attribute[:type]
      if type.is_a?(::Array)
        if type.first.include?(LazyResource::Mapping)
          resource.send(:"#{name}=", type.first.load(value))
        else
          resource.send(:"#{name}=", value.map { |object| type.first.parse(object) })
        end
      elsif type.include?(LazyResource::Mapping)
        resource.send(:"#{name}=", type.load(value))
      else
        resource.send(:"#{name}=", type.parse(value)) rescue StandardError
      end
    end

    resource.fetched = true
  end
end