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



47
48
49
50
51
52
53
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
# File 'lib/lazy_resource/mapping.rb', line 47

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

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

    if resource.class.root_node_name && hash.key?(resource.class.root_node_name.to_s)
      other_attributes = hash
      hash = other_attributes.delete(resource.class.root_node_name.to_s)
      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