Class: Ruson::Base
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Value
#get_val
Methods included from Querying
included
#delete_file_from_disk, #destroy, #ensure_model_folder_exists, #generate_uniq_id, included, #model_path, #save, #update, #write_file_to_disk
Methods included from Nilable
#check_nilable
Methods included from Json
#get_hash_from_json, #id_from_file_path
Methods included from Converter
#convert_to_hash
Constructor Details
#initialize(json, root_key: nil) ⇒ Base
~~~~ Instance Methods ~~~~
79
80
81
82
83
84
|
# File 'lib/ruson/base.rb', line 79
def initialize(json, root_key: nil)
params = get_hash_from_json(json)
params = params[root_key.to_s] unless root_key.nil?
init_attributes(self.class.accessors, params)
end
|
Class Method Details
.accessors ⇒ Object
33
34
35
|
# File 'lib/ruson/base.rb', line 33
def accessors
@accessors
end
|
.ensure_output_folder_is_defined ⇒ Object
71
72
73
74
75
76
|
# File 'lib/ruson/base.rb', line 71
def self.ensure_output_folder_is_defined
return if Ruson.output_folder
raise ArgumentError, 'No output folder defined. You can define it ' \
'using Ruson.output_folder = "/path/to/db/folder"'
end
|
.enum(attr, values) ⇒ Object
28
29
30
31
|
# File 'lib/ruson/base.rb', line 28
def enum(attr, values)
define_enum_methods attr.to_s, values.map(&:to_sym)
add_accessor attr.to_s
end
|
.field(attr, options = {}) ⇒ Object
23
24
25
26
|
# File 'lib/ruson/base.rb', line 23
def field(attr, options = {})
instance_eval("attr_accessor :#{attr.to_s}")
add_accessor attr.to_s, options
end
|
Instance Method Details
#to_hash ⇒ Object
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
# File 'lib/ruson/base.rb', line 86
def to_hash
res = convert_to_hash(self.class.accessors)
res.inject({}) do |result, attributes|
key, value = attributes
accessor = self.class.accessors[key]
if accessor && accessor.key?(:name)
result[accessor[:name]] = value
else
result[key] = value
end
result
end
end
|
#to_json(options = {}) ⇒ Object
101
102
103
104
105
106
107
|
# File 'lib/ruson/base.rb', line 101
def to_json(options = {})
hash = to_hash
options[:exclude].each { |key| hash.delete(key) } if options[:exclude]
hash.to_json
end
|