Class: LHS::Item

Inherits:
Proxy show all
Includes:
Destroy, Save, Update, Validation
Defined in:
lib/lhs/item.rb,
lib/lhs/concerns/item/save.rb,
lib/lhs/concerns/item/update.rb,
lib/lhs/concerns/item/destroy.rb,
lib/lhs/concerns/item/validation.rb

Overview

An item is a concrete record. It can be part of another proxy like collection.

Defined Under Namespace

Modules: Destroy, Save, Update, Validation

Instance Attribute Summary collapse

Attributes inherited from Proxy

#_data, #_loaded

Instance Method Summary collapse

Methods included from Validation

#valid?

Methods included from Update

#update, #update!

Methods included from Save

#save, #save!

Methods included from Destroy

#destroy

Methods inherited from Proxy

#load!, #reload!

Constructor Details

#initialize(data) ⇒ Item

Returns a new instance of Item.



19
20
21
22
# File 'lib/lhs/item.rb', line 19

def initialize(data)
  self.errors = LHS::Errors.new
  super(data)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &_block) ⇒ Object (protected)



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/lhs/item.rb', line 30

def method_missing(name, *args, &_block)
  return set(name, args.try(&:first)) if name.to_s[/=$/]
  name = args.first if name == :[]
  value = _data._raw[name.to_s]
  if value.nil? && _data._raw.present?
    value = _data._raw[name.to_sym]
    value = _data._raw[name.to_s.classify.to_sym] if value.nil?
  end
  if value.is_a?(Hash)
    handle_hash(value)
  elsif value.is_a?(Array)
    data = LHS::Data.new(value, _data)
    collection = LHS::Collection.new(data)
    LHS::Data.new(collection, _data)
  else
    convert(value)
  end
end

Instance Attribute Details

#errorsObject

prevent clashing with attributes of underlying data



15
16
17
# File 'lib/lhs/item.rb', line 15

def errors
  @errors
end

Instance Method Details

#item?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/lhs/item.rb', line 24

def item?
  true
end