Class: Aston

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

Overview

Aston class keeps, modifies, and produces JSON which is isomorphic to XML

Defined Under Namespace

Classes: Attributes, Content, Error

Constant Summary collapse

VERSION =
'0.2.2'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, attributes: {}, content: []) ⇒ Aston

Returns a new instance of Aston.



83
84
85
86
87
# File 'lib/aston.rb', line 83

def initialize(name, attributes: {}, content: [])
  @name = name
  @attributes = Attributes.new(attributes)
  @content = Content.new(content)
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



81
82
83
# File 'lib/aston.rb', line 81

def attributes
  @attributes
end

#contentObject (readonly)

Returns the value of attribute content.



81
82
83
# File 'lib/aston.rb', line 81

def content
  @content
end

#nameObject (readonly)

Returns the value of attribute name.



81
82
83
# File 'lib/aston.rb', line 81

def name
  @name
end

Class Method Details

.parse_hash(hash) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
# File 'lib/aston.rb', line 128

def self.parse_hash(hash)
  name = hash.fetch('name', hash.fetch(:name, ''))
  attributes = hash.fetch('attributes', hash.fetch(:attributes, {}))
  content = hash.fetch('content', hash.fetch(:content, [])).map do |elem|
    case elem
    when String then elem
    when Hash then Aston.parse_hash(elem)
    end
  end
  Aston.new(name, attributes: attributes, content: content)
end

Instance Method Details

#<<(value) ⇒ Object



97
98
99
# File 'lib/aston.rb', line 97

def <<(value)
  tap { @content << value }
end

#==(other) ⇒ Object



101
102
103
# File 'lib/aston.rb', line 101

def ==(other)
  other.is_a?(Aston) && @name == other.name && @attributes == other.attributes && @content == other.content
end

#[](key) ⇒ Object



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

def [](key)
  @attributes[key]
end

#[]=(key, value) ⇒ Object



93
94
95
# File 'lib/aston.rb', line 93

def []=(key, value)
  @attributes[key] = value
end

#filter(name) ⇒ Object



144
145
146
# File 'lib/aston.rb', line 144

def filter(name)
  @content.data.select { |a| a.is_a?(Aston) && a.name == name }
end

#get(path) ⇒ Object



166
167
168
169
170
# File 'lib/aston.rb', line 166

def get(path)
  fix_path(path).reduce([self]) do |memo, e|
    memo.flat_map { |aston| aston.filter(e) }
  end
end

#pathsObject



140
141
142
# File 'lib/aston.rb', line 140

def paths
  do_paths([], [])
end

#put_attribute(path, name, value, create_intermediate: true) ⇒ Object



154
155
156
157
158
# File 'lib/aston.rb', line 154

def put_attribute(path, name, value, create_intermediate: true)
  tap { clamber(path, create_intermediate).each { |aston| aston[name] = value } }
rescue StandardError => e
  raise Error, e
end

#put_content(path, content, create_intermediate: true) ⇒ Object



160
161
162
163
164
# File 'lib/aston.rb', line 160

def put_content(path, content, create_intermediate: true)
  tap { clamber(path, create_intermediate).each { |aston| aston << content } }
rescue StandardError => e
  raise Error, e
end

#to_hash(remove_empty: false) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/aston.rb', line 111

def to_hash(remove_empty: false)
  {
    name: @name,
    attributes: @attributes.to_hash,
    content: @content.data.map { |e| e.is_a?(Aston) ? e.to_hash(remove_empty: remove_empty) : e }
  }.tap do |hash|
    if remove_empty
      hash.delete(:attributes) if hash[:attributes] == {}
      hash.delete(:content) if hash[:content] == []
    end
  end
end

#to_json(opts = nil) ⇒ Object



124
125
126
# File 'lib/aston.rb', line 124

def to_json(opts = nil)
  to_hash.to_json(opts)
end

#to_sObject



105
106
107
108
109
# File 'lib/aston.rb', line 105

def to_s
  comment = "<!-- Aston:#{__id__}:#{name} -->"
  opening = @attributes.data.empty? ? "<#{name}>" : ["<#{name}", *@attributes].join(' ') << '>'
  [comment, opening, *@content, "</#{name}>"].join("\n")
end

#update_in(path, create_intermediate: true, &block) ⇒ Object



148
149
150
151
152
# File 'lib/aston.rb', line 148

def update_in(path, create_intermediate: true, &block)
  clamber(path, create_intermediate).each(&block)
rescue StandardError => e
  raise Error, e
end