Class: Factbook::Profile
- Inherits:
-
Object
- Object
- Factbook::Profile
- Includes:
- LogUtils::Logging
- Defined in:
- lib/factbook-fields/profile.rb
Class Method Summary collapse
-
.read(path) ⇒ Object
convenience helper.
Instance Method Summary collapse
-
#[](key) ⇒ Object
convenience shortcut.
- #add(category) ⇒ Object (also: #<<)
-
#initialize ⇒ Profile
constructor
A new instance of Profile.
- #size ⇒ Object
- #to_h ⇒ Object
-
#to_json(minify: false) ⇒ Object
convenience helper for data.to_json; note: pretty print by default!.
Constructor Details
#initialize ⇒ Profile
Returns a new instance of Profile.
19 20 21 |
# File 'lib/factbook-fields/profile.rb', line 19 def initialize @categories = {} end |
Class Method Details
.read(path) ⇒ Object
convenience helper
12 13 14 15 16 |
# File 'lib/factbook-fields/profile.rb', line 12 def self.read( path ) ## convenience helper text = File.open( path, 'r:utf-8' ) { |f| f.read } b = ProfileBuilder.new( text ) b.profile end |
Instance Method Details
#[](key) ⇒ Object
convenience shortcut
28 29 30 31 32 33 34 |
# File 'lib/factbook-fields/profile.rb', line 28 def [](key) ### convenience shortcut if key.is_a?( Integer ) ## allow access by 0,1,2, etc. @categories.values[ key ] else @categories[key] end end |
#add(category) ⇒ Object Also known as: <<
23 24 25 |
# File 'lib/factbook-fields/profile.rb', line 23 def add( category ) @categories[ category.title ] = category end |
#size ⇒ Object
36 |
# File 'lib/factbook-fields/profile.rb', line 36 def size() @categories.size; end |
#to_h ⇒ Object
40 41 42 43 44 45 46 |
# File 'lib/factbook-fields/profile.rb', line 40 def to_h data = {} @categories.each do |_,category| data[ category.title ] = category.data end data end |
#to_json(minify: false) ⇒ Object
convenience helper for data.to_json; note: pretty print by default!
48 49 50 51 52 53 54 |
# File 'lib/factbook-fields/profile.rb', line 48 def to_json( minify: false ) ## convenience helper for data.to_json; note: pretty print by default! if minify to_h.to_json else ## note: pretty print by default! JSON.pretty_generate( to_h ) end end |