Class: FastOpenStruct

Inherits:
Object
  • Object
show all
Defined in:
lib/fast_ostruct.rb,
lib/fast_ostruct/config.rb,
lib/fast_ostruct/version.rb

Defined Under Namespace

Classes: Config

Constant Summary collapse

VERSION =
'1.1.0'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}, options = config.initialize_options) ⇒ FastOpenStruct

Returns a new instance of FastOpenStruct.



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/fast_ostruct.rb', line 23

def initialize(attributes = {}, options = config.initialize_options)
  if options[:deep_initialize]
    attributes.except(*options.keys).each_pair do |name, value|
      self[name] = deep_initialize(value, options)
    end
  else
    attributes.except(*options.keys).each_pair do |name, value|
      self[name] = value
    end
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(mid, *args) ⇒ Object (private)



157
158
159
160
161
162
163
# File 'lib/fast_ostruct.rb', line 157

def method_missing(mid, *args)
  if (mname = mid[/.*(?==\z)/m])
    self[mname] = args.first
  else
    self[mid]
  end
end

Class Method Details

.configObject



14
15
16
# File 'lib/fast_ostruct.rb', line 14

def config
  @config ||= Config.new
end

.configure {|config| ... } ⇒ Object

Yields:



10
11
12
# File 'lib/fast_ostruct.rb', line 10

def configure
  yield config
end

.define_methods!(name) ⇒ Object



123
124
125
126
127
128
129
# File 'lib/fast_ostruct.rb', line 123

def define_methods!(name)
  class_exec do
    attr_accessor name

    defined_methods << name
  end
end

.defined_methodsObject



119
120
121
# File 'lib/fast_ostruct.rb', line 119

def defined_methods
  @defined_methods ||= Set.new
end

Instance Method Details

#==(other) ⇒ Object

Comparable



85
86
87
# File 'lib/fast_ostruct.rb', line 85

def ==(other)
  other.is_a?(self.class) && attributes == other.attributes
end

#[](name) ⇒ Object

Hasheable



61
62
63
# File 'lib/fast_ostruct.rb', line 61

def [](name)
  attribute_get(name)
end

#[]=(name, value) ⇒ Object



65
66
67
# File 'lib/fast_ostruct.rb', line 65

def []=(name, value)
  attribute_set(name, value)
end

#as_json(*_args) ⇒ Object

Serializable



50
51
52
# File 'lib/fast_ostruct.rb', line 50

def as_json(*_args)
  attributes
end

#attributes(options = config.attributes_options) ⇒ Object Also known as: serializable_hash, to_h



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/fast_ostruct.rb', line 35

def attributes(options = config.attributes_options)
  key_formatter = if options[:symbolize_keys]
                    proc { |name| name.to_sym }
                  else
                    proc { |name| name.to_s }
                  end

  instance_variables.each_with_object({}) do |var, hash|
    name = var.to_s.delete('@')
    value = attribute_get(name)
    hash[key_formatter.call(name)] = value.is_a?(FastOpenStruct) ? value.attributes(options) : value
  end
end

#configObject



19
20
21
# File 'lib/fast_ostruct.rb', line 19

def config
  self.class.config
end

#delete_field(name) ⇒ Object



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

def delete_field(name)
  attribute_unset(name)
end

#dig(name, *identifiers) ⇒ Object



69
70
71
72
73
# File 'lib/fast_ostruct.rb', line 69

def dig(name, *identifiers)
  name = name.to_sym
  identifiers.map!(&:to_sym) if identifiers.any?
  attributes(symbolize_keys: true).dig(name, *identifiers)
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/fast_ostruct.rb', line 89

def eql?(other)
  other.is_a?(self.class) && attributes.eql?(other.attributes)
end

#new_record?Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/fast_ostruct.rb', line 102

def new_record?
  true
end

#persisted?Boolean

Persistent

Returns:

  • (Boolean)


94
95
96
# File 'lib/fast_ostruct.rb', line 94

def persisted?
  false
end

#saveObject Also known as: save!, create, create!, update, update!, delete, destroy, destroy!, reload



98
99
100
# File 'lib/fast_ostruct.rb', line 98

def save(*)
  self
end

#slice(*names) ⇒ Object



75
76
77
78
# File 'lib/fast_ostruct.rb', line 75

def slice(*names)
  names.map!(&:to_sym)
  attributes(symbolize_keys: true).slice(*names)
end

#to_json(*_args) ⇒ Object



54
55
56
# File 'lib/fast_ostruct.rb', line 54

def to_json(*_args)
  JSON.generate(attributes)
end